Search code examples
c#system.drawing

System.Drawing Bitmap gives wrong dimensions


enter image description here

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?


Solution

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