// Operation: scale an image to the given dimensions. public static byte[] scaleImageTo(byte[] inBytes, int outWidth, int outHeight) { using (StreamImage inImage = byteArrayToImage(inBytes)) { int x, y, w, h; int iWoH = inImage.mImage.Width * outHeight; int iHoW = inImage.mImage.Height * outWidth; if (iWoH < iHoW) { w = (int)((double)(iWoH) / inImage.mImage.Height); h = outHeight; x = (int)((outWidth - w) / 2.0); y = 0; } else { w = outWidth; h = (int)((double)(iHoW) / inImage.mImage.Width); x = 0; y = (int)((outHeight - h) / 2.0); } using (Bitmap outImage = new Bitmap(outWidth, outHeight)) { drawImage(inImage.mImage, outImage, new Rectangle(x, y, w, h)); return(imageToByteArray(outImage)); } } }
// Operation: rotate an image by 90, 180, or 270 degrees. public static byte[] rotateImage(byte[] inBytes, int rotateType) { RotateFlipType rotateFlipType; switch (rotateType) { case 1: rotateFlipType = RotateFlipType.Rotate90FlipNone; break; case 2: rotateFlipType = RotateFlipType.Rotate180FlipNone; break; case 3: rotateFlipType = RotateFlipType.Rotate270FlipNone; break; default: throw new ArgumentException("Invalid type"); } using (StreamImage inImage = byteArrayToImage(inBytes)) { inImage.mImage.RotateFlip(rotateFlipType); return(imageToByteArray(inImage.mImage)); } }
// Operation: scale an image by a scale factor. public static byte[] scaleImageBy(byte[] inBytes, float scaleFactor) { using (StreamImage inImage = byteArrayToImage(inBytes)) { int outWidth = (int)(inImage.mImage.Width * scaleFactor); int outHeight = (int)(inImage.mImage.Height * scaleFactor); using (Bitmap outImage = new Bitmap(outWidth, outHeight)) { drawImage(inImage.mImage, outImage, new Rectangle(0, 0, outWidth, outHeight)); return(imageToByteArray(outImage)); } } }
// Operation: return the value of an image property (provided it's a string or integer). // Use property ID 8298 for copyright. // See https://msdn.microsoft.com/en-us/library/ms534416.aspx for additional IDs. public static string getImageProperty(byte[] inBytes, int propertyId) { using (StreamImage inImage = byteArrayToImage(inBytes)) { foreach (PropertyItem propItem in inImage.mImage.PropertyItems) { if (propItem.Id == propertyId) { return((propItem.Type == 2) ? System.Text.Encoding.UTF8.GetString(propItem.Value) : propItem.Value.ToString()); } } } return(null); }
public override IRow Process(IRow input, IUpdatableRow output) { var img = input.Get <byte[]>("image_data"); // load image only once into memory per row using (StreamImage inImage = new StreamImage(img)) { output.SetColumnIfExists("equipment_make", inImage.getStreamImageProperty(ImageProperties.equipment_make)); output.SetColumnIfExists("equipment_model", inImage.getStreamImageProperty(ImageProperties.equipment_model)); output.SetColumnIfExists("description", inImage.getStreamImageProperty(ImageProperties.description)); output.SetColumnIfExists("copyright", inImage.getStreamImageProperty(ImageProperties.copyright)); output.SetColumnIfExists("thumbnail", inImage.scaleStreamImageTo(150, 150)); } return(output.AsReadOnly()); }
public override IRow Process(IRow input, IUpdatableRow output) { var img = input.Get<byte[]>("image_data"); // load image only once into memory per row using (StreamImage inImage = new StreamImage(img)) { output.SetColumnIfExists("equipment_make", inImage.getStreamImageProperty(ImageProperties.equipment_make)); output.SetColumnIfExists("equipment_model", inImage.getStreamImageProperty(ImageProperties.equipment_model)); output.SetColumnIfExists("description", inImage.getStreamImageProperty(ImageProperties.description)); output.SetColumnIfExists("copyright", inImage.getStreamImageProperty(ImageProperties.copyright)); output.SetColumnIfExists("thumbnail", inImage.scaleStreamImageTo(150, 150)); } return output.AsReadOnly(); }
public override IEnumerable <IRow> Extract(IUnstructuredReader input, IUpdatableRow output) { byte[] img = ImageOps.GetByteArrayforImage(input.BaseStream); // load image only once into memory per row using (StreamImage inImage = new StreamImage(img)) { output.SetColumnIfExists("image", img); output.SetColumnIfExists("equipment_make", inImage.getStreamImageProperty(ImageProperties.equipment_make)); output.SetColumnIfExists("equipment_model", inImage.getStreamImageProperty(ImageProperties.equipment_model)); output.SetColumnIfExists("description", inImage.getStreamImageProperty(ImageProperties.description)); output.SetColumnIfExists("copyright", inImage.getStreamImageProperty(ImageProperties.copyright)); output.SetColumnIfExists("thumbnail", inImage.scaleStreamImageTo(this._scaleWidth, this._scaleHeight)); } yield return(output.AsReadOnly()); }