// 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: 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) { switch (propItem.Type) { case 5: return(string.Join(",", propItem.Value.Select(x => x.ToString()))); //ExifGpsToFloat(propItem) case 2: return(System.Text.Encoding.UTF8.GetString(propItem.Value)); default: return($"{propItem.Type}: {propItem.Value.ToString()}"); } } } } return(null); }
// 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)); } } }
public static string getImageGPSProperty(byte[] inBytes, int propertyId, int referencePropertyId) { using (StreamImage inImage = byteArrayToImage(inBytes)) { var prop = inImage.mImage.PropertyItems.Where(x => x.Id == propertyId).Take(1); var propRef = inImage.mImage.PropertyItems.Where(x => x.Id == referencePropertyId).Take(1); if (!prop.Any() || !propRef.Any()) { // abort return(null); } //return ExifGpsToFloat(prop.Single(), propRef.Single()).ToString(); return(ExifGpsToDouble(propRef.Single(), prop.Single()).ToString()); } }