Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am uploading images to my AWS S3 storage using asp.net website. 
I have a process before uploading the image. I downgrade the image quality before uploading it to the cloud. 
When I give it an image that its width is bigger than its height, the process works without any problem, but when I try it on an image that its height is bigger than its width, it rotates the image -90 degrees.

C#
public void image_Upload(){
 Image fixed_image = ResizeImage(Path.GetFileName(FileUpload1.FileName), myimg, new Size(800, 500));
}
public Image ResizeImage(string fileName, Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercentW = (size.Width / (float)sourceWidth);
            float nPercentH = (size.Height / (float)sourceHeight);

            float nPercent = nPercentH < nPercentW ? nPercentH : nPercentW;
           
          
            var destWidth = (int)(sourceWidth * nPercent);
            var destHeight = (int)(sourceHeight * nPercent);

            var src = imgToResize;

            using (var dst = new Bitmap(destWidth, destHeight, imgToResize.PixelFormat))
            {
               
                    dst.SetResolution(imgToResize.HorizontalResolution, imgToResize.VerticalResolution);
                 using (var g = Graphics.FromImage(dst))
                {
                    var mime = Path.GetExtension(fileName).ToLower();
                    ImageFormat format;
                    if (mime == ".bmp" || mime == ".png")
                    {
                        //convert all gif to png, better resize quality
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.DrawImage(src, 0, 0, dst.Width, dst.Height);
                        format = ImageFormat.Png;
                    }
                    else
                    {
                        //jpeg
                        g.CompositingQuality = CompositingQuality.HighQuality;
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.SmoothingMode = SmoothingMode.HighQuality;
                        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        format = ImageFormat.Jpeg;
                    }
                   
                        g.DrawImage(src,0,0, dst.Width, dst.Height);
                    
                    // At this point the new bitmap has no MimeType
                    // Need to output to memory stream
                    var m = new MemoryStream();
                    dst.Save(m, format);


                    var img = Image.FromStream(m);

                    return img;
                }
            }
        }



What I have tried:

I have tried changing some parameters such as new Size(), nPercent, g.DrawImage() it still doesn't solve the problem.

In fact changing nPercent parameter will results upside down images when the width is bigger
Posted
Updated 14-Dec-20 1:38am

1 solution

I have a similar issue and the problem is to do with the way the image data is stored. Take a look at Handling the orientation EXIF tag in images using C# - Articles and information on C# and .NET development topics • Cyotek[^] for more information.
 
Share this answer
 
Comments
Drust Taib 14-Dec-20 7:54am    
It is true, but I guess there should be command that forces the image to be stored when the height is bigger than the width, but I cannot figure it out
Richard MacCutchan 14-Dec-20 7:59am    
Go to the link I gave you and read the blog, it explains the problem and offers a solution. But it has nothing to do with whether the height is bigger than the width or vice versa.
Drust Taib 14-Dec-20 9:27am    
thank you for your response, I checked the solution, however there is a parameter that I don't have it and I don't understand. it is ExifOrientationTagId. could you please explain it for me?
Richard MacCutchan 14-Dec-20 10:38am    
It is a PropertyItem that is present in some images. It indicates what order the pixel data is stored in. I have no idea why they would do that, but I guess the Exif standards explain it. The code samples in the blog show what you need to do according to which value is in the image properties. Also remember that not all images will contain this property.

I found the following additional information on MSDN:
PropertyTagOrientation
Image orientation viewed in terms of rows and columns.

Tag: 0x0112

Type: PropertyTagTypeShort

Count: 1

Value:
1 - The 0th row is at the top of the visual image, and the 0th column is the visual left side.
2 - The 0th row is at the visual top of the image, and the 0th column is the visual right side.
3 - The 0th row is at the visual bottom of the image, and the 0th column is the visual right side.
4 - The 0th row is at the visual bottom of the image, and the 0th column is the visual left side.
5 - The 0th row is the visual left side of the image, and the 0th column is the visual top.
6 - The 0th row is the visual right side of the image, and the 0th column is the visual top.
7 - The 0th row is the visual right side of the image, and the 0th column is the visual bottom.
8 - The 0th row is the visual left side of the image, and the 0th column is the visual bottom.

So when you create the new image you can use this value to adjust it so it is correctly displayed.
Drust Taib 15-Dec-20 4:47am    
I tried looking at some more examples, also yours. The ExitOrentationTagId is more likely to be 274, since it is an image property Item. However, give it this property, it doesn't fix the problem, but it rotates some images that doesn't have to be rotated. Or maybe I am using the method in a wrong location. could you please tell me where should I put NormilizeOrentation() function In the example you provide? You can use the code I have written in the question I asked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900