【问题标题】:Image is rotated after Resizing调整大小后图像旋转
【发布时间】:2019-07-11 12:31:37
【问题描述】:

我有下面的代码,以便将图像调整为适当的尺寸(600,600) 它工作正常,但对于像这样的某些图像(720 * 1280),结果旋转(90度) 我怎么知道哪些图像会旋转?以及如何防止这种情况发生?

StackOverflow 也可以旋转图像

     public static System.Drawing.Image FixedSize(Image image, int Width,   int Height, bool needToFill)
    {
        #region calculations
        int sourceWidth = image.Width;
        int sourceHeight = image.Height;
        int sourceX = 0;
        int sourceY = 0;
        double destX = 0;
        double destY = 0;

        double nScale = 0;
        double nScaleW = 0;
        double nScaleH = 0;

        nScaleW = ((double)Width / (double)sourceWidth);
        nScaleH = ((double)Height / (double)sourceHeight);
        if (!needToFill)
        {
            nScale = Math.Min(nScaleH, nScaleW);
        }
        else
        {
            nScale = Math.Max(nScaleH, nScaleW);
            destY = (Height - sourceHeight * nScale) / 2;
            destX = (Width - sourceWidth * nScale) / 2;
        }

        if (nScale > 1)
            nScale = 1;

        int destWidth = (int)Math.Round(sourceWidth * nScale);
        int destHeight = (int)Math.Round(sourceHeight * nScale);
        #endregion

        System.Drawing.Bitmap bmPhoto = null;
        try
        {
            bmPhoto = new System.Drawing.Bitmap(destWidth + (int)Math.Round(2 * destX), destHeight + (int)Math.Round(2 * destY));
        }
        catch (Exception ex)
        {
            throw new ApplicationException(string.Format("destWidth:{0}, destX:{1}, destHeight:{2}, desxtY:{3}, Width:{4}, Height:{5}",
                destWidth, destX, destHeight, destY, Width, Height), ex);
        }
        using (System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto))
        {
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.CompositingQuality = CompositingQuality.HighQuality;
            grPhoto.SmoothingMode = SmoothingMode.HighQuality;

            Rectangle to =  new System.Drawing.Rectangle((int)Math.Round(destX), (int)Math.Round(destY), destWidth, destHeight);
            Rectangle from = new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight);
            //Console.WriteLine("From: " + from.ToString());
            //Console.WriteLine("To: " + to.ToString());
            grPhoto.DrawImage(image, to, from, System.Drawing.GraphicsUnit.Pixel);

            return bmPhoto;
        }
    }

起初我想防止这种情况,如果不可能,我想将图像重新旋转为真实形状

【问题讨论】:

  • 复制源图片中的 exeif 旋转信息
  • 信息在原图-看看howtogeek.com/254830/…
  • @fubo 你是说这个吗?image.PropertyItems
  • 这里是读取方向信息的一个实现stackoverflow.com/questions/27835064/…
  • @fubo 我将该代码添加到我的代码中,但在调整大小后我对其进行了调试,它不包含此属性exifOrientationID

标签: c# resize crop


【解决方案1】:

我遇到了这个问题。为了解决这个问题,我必须根据answer 中建议的 exeif 旋转信息来旋转图像。但在我的情况下,我必须先旋转原始图像,然后才能像 here 那样调整大小。

private const int OrientationKey = 0x0112;
    private const int NotSpecified = 0;
    private const int NormalOrientation = 1;
    private const int MirrorHorizontal = 2;
    private const int UpsideDown = 3;
    private const int MirrorVertical = 4;
    private const int MirrorHorizontalAndRotateRight = 5;
    private const int RotateLeft = 6;
    private const int MirorHorizontalAndRotateLeft = 7;
    private const int RotateRight = 8;

public static Image FixedSize(Image image, ...)
    {
        // Fix orientation if needed. It appears the code isn't compensating for exif data for the image.
        // So if a photo was taken with the camera sideways, the image was displaying sideways,
        // while the computer was reading that exif data and automatically rotating it in windows to display it as it's suppose to be.
        // This code reads the exif data and rotates accordingly.
        if (image.PropertyIdList.Contains(OrientationKey))
        {
            var orientation = (int)image.GetPropertyItem(OrientationKey).Value[0];
            switch (orientation)
            {
                case NotSpecified: // Assume it is good.
                case NormalOrientation:
                    // No rotation required.
                    break;
                case MirrorHorizontal:
                    image.RotateFlip(RotateFlipType.RotateNoneFlipX);
                    break;
                case UpsideDown:
                    image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    break;
                case MirrorVertical:
                    image.RotateFlip(RotateFlipType.Rotate180FlipX);
                    break;
                case MirrorHorizontalAndRotateRight:
                    image.RotateFlip(RotateFlipType.Rotate90FlipX);
                    break;
                case RotateLeft:
                    image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    break;
                case MirorHorizontalAndRotateLeft:
                    image.RotateFlip(RotateFlipType.Rotate270FlipX);
                    break;
                case RotateRight:
                    image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    break;
                default:
                    throw new NotImplementedException("An orientation of " + orientation + " isn't implemented.");
            }
        }

        // resize image code ...
        
        return bmPhoto;
    }

【讨论】:

    猜你喜欢
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多