System.Drawing 位图给出了错误的尺寸

System.Drawing Bitmap gives wrong dimensions

这在某些情况下会导致内存不足异常,谁能告诉我为什么会这样,最简单的解决方法是什么?

以防万一其他社区成员需要它,您只需访问图像属性并检查 id 元素 274 上的值。如果您想了解更多关于此问题的信息,请参阅 Problem reading JPEG Metadata (Orientation) 在我解决这个问题时帮助了我很多。

这可能无法很好地解决width/height问题,但至少图片的方向可以。

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