如果图片包含exif dataPropertyItems应该包含orientation标签。
它编码正确显示图像所需的旋转/翻转:
PropertyTagOrientation
按行和列查看图像方向。
标记 0x0112
1 - 第 0 行位于顶部
视觉图像,第0列是视觉左侧。
2 - 第 0 个
行位于图像的可视顶部,第 0 列是
视觉右侧。
3 - 第 0 行位于可视底部
图片,第0列是视觉右侧。
4 - 第 0 行
位于图像的视觉底部,第 0 列是视觉
左侧。
5 - 第 0 行是图像的视觉左侧,并且
第 0 列是视觉顶部。
6 - 第 0 行是视觉右侧
图像的一侧,第 0 列是视觉顶部。
7 - 第0个
行是图像的视觉右侧,第 0 列是
视觉底部。
8 - 第 0 行是图像的视觉左侧,
第0列是视觉底部。
这是一个检索PropertyItem的函数:
PropertyItem getPropertyItemByID(Image img, int Id)
{
return
img.PropertyItems.Select(x => x).FirstOrDefault(x => x.Id == Id);
}
以下是使用 GDI+ RotateFlip 方法动态调整图像的示例:
void Rotate(Bitmap bmp)
{
PropertyItem pi = bmp.PropertyItems.Select(x => x)
.FirstOrDefault(x => x.Id == 0x0112);
if (pi == null) return;
byte o = pi.Value[0];
if (o==2) bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);
if (o==3) bmp.RotateFlip(RotateFlipType.RotateNoneFlipXY);
if (o==4) bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
if (o==5) bmp.RotateFlip(RotateFlipType.Rotate90FlipX);
if (o==6) bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
if (o==7) bmp.RotateFlip(RotateFlipType.Rotate90FlipY);
if (o==8) bmp.RotateFlip(RotateFlipType.Rotate90FlipXY);
}
它将图像更改为正确旋转的版本..
我已经用this nice set of sample images 测试了值。
注意:代码仅在图像实际包含方向标签时才有效。如果他们不这样做,可能是因为他们是扫描,那么它将什么都不做。
注2你写的我检查了原始图像旋转。这不是那么简单:资源管理器将显示已经旋转的图像,所以在这里它们看起来都正确并且即使检查属性也不会显示方向!
通常,当不存在 exif 数据时,PropertyTagOrientation 标签存在,但只有默认值 1..
更新:
如果图片没有有PropertyTagOrientation,您可以通过以下方式添加:
using System.Runtime.Serialization;
..
pi = (PropertyItem)FormatterServices
.GetUninitializedObject(typeof(PropertyItem));
pi.Id = 0x0112; // orientation
pi.Len = 2;
pi.Type = 3;
pi.Value = new byte[2] { 1, 0 };
pi.Value[0] = yourOrientationByte;
yourImage.SetPropertyItem(pi);
向@ne1410s 的优秀answer here!致敬。
注意在图片中添加PropertyItems不会添加exif数据;两者是不同的标签集!