EXIF data getting lost during scaling

beginer
beginer
216 Points
63 Posts

I'm trying following code to scaling image:

public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
{
    var ratioX = (double)maxWidth / image.Width;
    var ratioY = (double)maxHeight / image.Height;
    var ratio = Math.Min(ratioX, ratioY);

    var newWidth = (int)(image.Width * ratio);
    var newHeight = (int)(image.Height * ratio);

    var newImage = new Bitmap(image, newWidth, newHeight);

    //Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
    return newImage;
}

Able to scale the image but in few cases image getting rotated. Checked and found that in the above method some how EXIF data getting lost during drawing new bitmap.

What is the way to keep the rotation of the original images?

Views: 80
Total Answered: 1
Total Marked As Answer: 1
Posted On: 16-Mar-2021 08:13

Share:   fb twitter linkedin
Answers
Priya
Priya
Member
132 Points
36 Posts
         

I think, issue is in following line:

var newImage = new Bitmap(image, newWidth, newHeight);

Add following lines of code after above:

foreach (var id in image.PropertyIdList)
            newImage.SetPropertyItem(image.GetPropertyItem(id));
Posted On: 17-Mar-2021 03:54
Thanks. Worked for me.
 - beginer  19-Mar-2021 02:27
 Log In to Chat