1

I have below code in order to resize images to proper dimensions(600,600) it works fine, but for some images (720*1280) like this, the result is rotated (90 deg) how can I know which images would rotate? and how Can I prevent this ?

StackOverflow Roates the image too

enter image description here

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

At first I want to prevent this, if this is not possible I want to re-rotate image to real shape

programmer
  • 83
  • 10
  • copy the exeif rotation info from your source image – fubo Feb 18 '19 at 07:03
  • The information is in the original image - have a look at https://www.howtogeek.com/254830/why-your-photos-dont-always-appear-correctly-rotated/ – fubo Feb 18 '19 at 07:18
  • @fubo you mean this ?`image.PropertyItems` – programmer Feb 18 '19 at 07:18
  • here is a implemetation of the reading of orientation information https://stackoverflow.com/questions/27835064/get-image-orientation-and-rotate-as-per-orientation – fubo Feb 18 '19 at 07:20
  • @fubo I add that code to my code but after resizing I debugged it and it does not contain this property `exifOrientationID` – programmer Feb 18 '19 at 07:51
  • that's the reason why your image has the wrong rotation. So you have to copy the orientation property or you have to rotate your image depending on sources orientation property – fubo Feb 18 '19 at 08:09

1 Answers1

0

I had this problem. To solve it I had to rotate the image according to the exeif rotation info has suggested in this answer. But in my case I had to rotate the original image before it is resized as done 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;
    }
Martin D.
  • 1,812
  • 3
  • 21
  • 30