简体   繁体   中英

System.Drawing Bitmap gives wrong dimensions

在此处输入图片说明

This would result in out of memory exception in some cases, can anyone tell me why is it happening and what would be the easiest workaround?

Just in case any other community member needs it, you can just simply access to image properties and check for the value on the id element 274. If you want to know quite more about this issue refer to Problem reading JPEG Metadata (Orientation) which helped me a lot while I was solving this.

This may not solve the width/height issue in a very good way, but at least the orientation of the picture will be ok.

using(var image = new Bitmap(filePath))
{
    PropertyItem propertie = image.PropertyItems.FirstOrDefault(p => p.Id == 274);
    if (propertie != null)
    {
        int orientation = propertie.Value[0];
        if (orientation == 6)
            image.RotateFlip(RotateFlipType.Rotate90FlipNone);
        if (orientation == 8)
            image.RotateFlip(RotateFlipType.Rotate270FlipNone);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM