/// <summary> /// /// </summary> /// <param name="image"></param> /// <param name="id"></param> public static void Remove(System.Drawing.Image image, int id) { if (image is System.Drawing.Image && ExifFormat.Contains(image.RawFormat)) { image.RemovePropertyItem(id); } }
/// <summary> /// /// </summary> /// <param name="image"></param> public static void Remove(System.Drawing.Image image) { if (image is System.Drawing.Image && ExifFormat.Contains(image.RawFormat)) { foreach (var item in image.PropertyItems) { image.RemovePropertyItem(item.Id); } } }
public static void SetPropertyItems(Image image, PropertyItem[] items) { PropertyItem[] pis = image.PropertyItems; foreach (PropertyItem pi in pis) { image.RemovePropertyItem(pi.Id); } foreach (PropertyItem pi in items) { image.SetPropertyItem(pi); } }
internal static Image ResizeImage(Image image, ref int width, ref int height) { if (image.Width <= width && image.Height <= height) { foreach (var p in image.PropertyIdList.Clone() as int[]) { image.RemovePropertyItem(p); } return image; } var nw = (float)image.Width; var nh = (float)image.Height; var factor = 1.0f; if (nw > nh) { factor = width / nw; } else { factor = height / nh; } nw = nw * factor; nh = nh * factor; var result = new Bitmap((int)nw, (int)nh); try { try { result.SetResolution(image.HorizontalResolution, image.VerticalResolution); } catch (Exception ex) { LogManager.GetLogger(typeof(ThumbnailMaker)).Debug("Failed to set resolution", ex); } using (Graphics graphics = Graphics.FromImage(result)) { if (result.Width > image.Width && result.Height > image.Height) { graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; } else { graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic; } graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; graphics.DrawImage(image, 0, 0, result.Width, result.Height); width = result.Width; height = result.Height; } return result; } catch (Exception) { result.Dispose(); throw; } }
public static System.Drawing.Image RotateImage(System.Drawing.Image imgSource) { if (Array.IndexOf(imgSource.PropertyIdList, 274) > -1) { var orientation = (int)imgSource.GetPropertyItem(274).Value[0]; switch (orientation) { case 1: // No rotation required. break; case 2: imgSource.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 3: imgSource.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 4: imgSource.RotateFlip(RotateFlipType.Rotate180FlipX); break; case 5: imgSource.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: imgSource.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 7: imgSource.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: imgSource.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } // This EXIF data is now invalid and should be removed. imgSource.RemovePropertyItem(274); } return(imgSource); }
private void CorrectRotation(Image img) { if (Array.IndexOf(img.PropertyIdList, 274) > -1) { var orientation = (int)img.GetPropertyItem(274).Value[0]; switch (orientation) { case 1: // No rotation required. break; case 2: img.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 3: img.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 4: img.RotateFlip(RotateFlipType.Rotate180FlipX); break; case 5: img.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: img.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 7: img.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: img.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } // This EXIF data is now invalid and should be removed. img.RemovePropertyItem(274); } }
/// <summary> /// 旋转图片到正确位置 /// </summary> /// <param name="image"></param> public static void OrientationImage(System.Drawing.Image image) { if (Array.IndexOf(image.PropertyIdList, 274) > -1) { var orientation = (int)image.GetPropertyItem(274).Value[0]; switch (orientation) { case 1: // No rotation required. break; case 2: image.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 3: image.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 4: image.RotateFlip(RotateFlipType.Rotate180FlipX); break; case 5: image.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: image.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 7: image.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: image.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } image.RemovePropertyItem(274); } }
/// <summary> /// Rotate the given bitmap according to Exif Orientation data /// </summary> /// <param name="img">source image</param> /// <param name="updateExifData">set it to TRUE to update image Exif data after rotation (default is TRUE)</param> /// <returns>The RotateFlipType value corresponding to the applied rotation. If no rotation occurred, RotateFlipType.RotateNoneFlipNone will be returned.</returns> public static RotateFlipType RotateImageByExifOrientationData(System.Drawing.Image img, bool updateExifData = true) { int orientationId = 0x0112; var fType = RotateFlipType.RotateNoneFlipNone; if (img.PropertyIdList.Contains(orientationId)) { var pItem = img.GetPropertyItem(orientationId); fType = GetRotateFlipTypeByExifOrientationData(pItem.Value[0]); if (fType != RotateFlipType.RotateNoneFlipNone) { img.RotateFlip(fType); // Remove Exif orientation tag (if requested) if (updateExifData) { img.RemovePropertyItem(orientationId); } } } return(fType); }
/// <summary> /// Rotate the given bitmap according to Exif Orientation data /// </summary> /// <param name="image">source image</param> /// <param name="updateExifData">Set it to <c>true</c> to update image Exif data after rotation /// (default is <c>false</c>)</param> /// <returns>Returns <c>true</c> when the image is rotated</returns> private bool RotateImageByExifOrientationData(Image image, bool updateExifData = true) { const int orientationId = 0x0112; if (!((IList)image.PropertyIdList).Contains(orientationId)) { return(false); } var item = image.GetPropertyItem(orientationId); RotateFlipType rotateFlipType; WriteToLog("Checking image rotation"); switch (item.Value[0]) { case 2: rotateFlipType = RotateFlipType.RotateNoneFlipX; break; case 3: rotateFlipType = RotateFlipType.Rotate180FlipNone; break; case 4: rotateFlipType = RotateFlipType.Rotate180FlipX; break; case 5: rotateFlipType = RotateFlipType.Rotate90FlipX; break; case 6: rotateFlipType = RotateFlipType.Rotate90FlipNone; break; case 7: rotateFlipType = RotateFlipType.Rotate270FlipX; break; case 8: rotateFlipType = RotateFlipType.Rotate270FlipNone; break; default: rotateFlipType = RotateFlipType.RotateNoneFlipNone; break; } if (rotateFlipType == RotateFlipType.RotateNoneFlipNone) { return(false); } image.RotateFlip(rotateFlipType); WriteToLog($"Image rotated with {rotateFlipType}"); // Remove Exif orientation tag (if requested) if (updateExifData) { image.RemovePropertyItem(orientationId); } return(true); }
private static string CheckRotationAndConvertToBase64(byte[] imageBytes) { string base64String = ""; System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageBytes)); //if (image.PropertyIdList.Contains(0x0112)) //0x0112 is available in photoshop images //{ // PropertyItem propOrientation = image.GetPropertyItem(0x0112); // short orientation = BitConverter.ToInt16(propOrientation.Value, 0); // if (orientation == 6) // { // image.RotateFlip(RotateFlipType.Rotate90FlipNone); // } // else if (orientation == 8) // { // image.RotateFlip(RotateFlipType.Rotate270FlipNone); // } // else // { // // Do nothing // } //} if (Array.IndexOf(image.PropertyIdList, 274) > -1) { var orientation = (int)image.GetPropertyItem(274).Value[0]; switch (orientation) { case 1: // No rotation required. break; case 2: image.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 3: image.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 4: image.RotateFlip(RotateFlipType.Rotate180FlipX); break; case 5: image.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: image.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 7: image.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: image.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } // This EXIF data is now invalid and should be removed. image.RemovePropertyItem(274); } //Convert to Base64 using (MemoryStream m = new MemoryStream()) { image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] imageBytesArray = m.ToArray(); // Convert byte[] to Base64 String base64String = Convert.ToBase64String(imageBytesArray); } return(base64String); }
/// <summary> /// Clears any EXIF metadata from the image /// </summary> /// <param name="image">The current image.</param> private void ClearExif(Image image) { if (image.PropertyItems.Any()) { foreach (KeyValuePair<int, PropertyItem> item in this.ExifPropertyItems) { image.RemovePropertyItem(item.Key); } } }
/// <summary> /// Clears any EXIF metadata from the image /// </summary> /// <param name="image">The current image.</param> private void ClearExif(Image image) { if (image.PropertyItems.Any()) { foreach (KeyValuePair<int, PropertyItem> item in this.ExifPropertyItems) { // The Gif decoder specifically requires these properties so we must not delete them. if (item.Key != (int)ExifPropertyTag.LoopCount && item.Key != (int)ExifPropertyTag.FrameDelay) { image.RemovePropertyItem(item.Key); } } } }
public Image RotateImage(Image bmp) { if (Array.IndexOf(bmp.PropertyIdList, 274) > -1) { var orientation = (int)bmp.GetPropertyItem(274).Value[0]; switch (orientation) { case 1: // No rotation required. break; case 2: bmp.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 3: bmp.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 4: bmp.RotateFlip(RotateFlipType.Rotate180FlipX); break; case 5: bmp.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: bmp.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 7: bmp.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: bmp.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } // This EXIF data is now invalid and should be removed. bmp.RemovePropertyItem(274); } return bmp; }
/// <summary> /// Clears any EXIF metadata from the image /// </summary> /// <param name="image">The current image.</param> private void ClearExif(Image image) { if (image.PropertyItems.Any()) { foreach (KeyValuePair<int, PropertyItem> item in this.ExifPropertyItems) { // Ensure that we do not try to remove any stripped out properties // e.g gif comments. if (image.PropertyIdList.Contains(item.Key)) { // The Gif decoder specifically requires these properties so we must not delete them. if (item.Key != (int)ExifPropertyTag.LoopCount && item.Key != (int)ExifPropertyTag.FrameDelay) { image.RemovePropertyItem(item.Key); } } } } }