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
  • 11
  • 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

2 Answers2

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,950
  • 3
  • 23
  • 33
0

This issue is solved. I think the error occurs in your image file type extension. When I rename the abc.png file then file must be in png format, then the image will not rotate anymore after resize.

Please see the following code:

var image = Image.FromStream(formFile.OpenReadStream());
using (FileStream fs = System.IO.File.Create(fulPath))
{

    TempData.Keep();
    var resized = new Bitmap(image, new Size(160,160));
    var imageStream = new MemoryStream();
    resized.Save(fs, ImageFormat.Png); // <-you must change here for the png or any other file extension here, like your file name is abc.png then the file formate must be png. 


    formFile.CopyTo(fs);

    fs.Flush();
    successmsg = "1";
    return Json(successmsg);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129