// 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); }