【问题标题】:System.Drawing.Graphics Drawimage rotates picture taken from IOS to 90 degreeSystem.Drawing.Graphics Drawimage 将IOS拍摄的图片旋转90度
【发布时间】:2018-03-06 17:35:13
【问题描述】:

我有以下代码,它获取上传的图像并使用 System.Drawing.Graphics 在 150 x 200 div 中绘制它,但是当从 IOS 设备拍摄图像时,图片向右旋转 90 度。

仅供参考:我在 JavaScript 画布中绘制图像时遇到了同样的问题,This solution 对我有用。因此,我正在寻找 C# 中的等效解决方案

     private System.Drawing.Image ResizeAndDraw(System.Drawing.Image objTempImage)
            {
                Size objSize = new Size(150, 200);
                Bitmap objBmp;
                objBmp = new Bitmap(objSize.Width, objSize.Height);

                Graphics g = Graphics.FromImage(objBmp);
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                //Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
                Rectangle rect = new Rectangle(0,0,150,200);
                //g.DrawImage(objTempImage, rect, 0, 0, objTempImage.Width, objTempImage.Height, GraphicsUnit.Pixel);
                g.DrawImage(objTempImage, rect);
                return objBmp;
            }

【问题讨论】:

    标签: c# ios image system.drawing bitmapimage


    【解决方案1】:

    我从 Here 发布的答案中解决了这个问题他写了一个简单的帮助类来完成所有这些:

    you can check the full source code here.

        private System.Drawing.Image ResizeAndDraw(System.Drawing.Image objTempImage)
            {
              // call image helper to fix the orientation issue 
                var temp = ImageHelper.RotateImageByExifOrientationData(objTempImage, true);
                Size objSize = new Size(150, 200);
                Bitmap objBmp;
                objBmp = new Bitmap(objSize.Width, objSize.Height);
    
                Graphics g = Graphics.FromImage(objBmp);
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                //Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
                Rectangle rect = new Rectangle(0,0,150,200);
                //g.DrawImage(objTempImage, rect, 0, 0, objTempImage.Width, objTempImage.Height, GraphicsUnit.Pixel);
                g.DrawImage(objTempImage, rect);
                return objBmp;
            }
    
    
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Linq;
    
    public static class ImageHelper
    {
        /// <summary>
        /// Rotate the given image file according to Exif Orientation data
        /// </summary>
        /// <param name="sourceFilePath">path of source file</param>
        /// <param name="targetFilePath">path of target file</param>
        /// <param name="targetFormat">target format</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(string sourceFilePath, string targetFilePath, ImageFormat targetFormat, bool updateExifData = true)
        {
            // Rotate the image according to EXIF data
            var bmp = new Bitmap(sourceFilePath);
            RotateFlipType fType = RotateImageByExifOrientationData(bmp, updateExifData);
            if (fType != RotateFlipType.RotateNoneFlipNone)
            {
                bmp.Save(targetFilePath, targetFormat);
            }
            return fType;
        }
    
        /// <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(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>
        /// Return the proper System.Drawing.RotateFlipType according to given orientation EXIF metadata
        /// </summary>
        /// <param name="orientation">Exif "Orientation"</param>
        /// <returns>the corresponding System.Drawing.RotateFlipType enum value</returns>
        public static RotateFlipType GetRotateFlipTypeByExifOrientationData(int orientation)
        {
            switch (orientation)
            {
                case 1:
                default:
                    return RotateFlipType.RotateNoneFlipNone;
                case 2:
                    return RotateFlipType.RotateNoneFlipX;
                case 3:
                    return RotateFlipType.Rotate180FlipNone;
                case 4:
                    return RotateFlipType.Rotate180FlipX;
                case 5:
                    return RotateFlipType.Rotate90FlipX;
                case 6:
                    return RotateFlipType.Rotate90FlipNone;
                case 7:
                    return RotateFlipType.Rotate270FlipX;
                case 8:
                    return RotateFlipType.Rotate270FlipNone;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-10
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      相关资源
      最近更新 更多