您似乎忘记了所查找的方向ID值是十六进制的。如果使用112,则应使用0x112。

读取JPEG元数据时出现问题(方向)

共 4 个回答
高赞
时间
活跃
0

0

我结合了给出的答案和评论,并得出以下结论:
MemoryStream stream = new MemoryStream(data);
Image image = Image.FromStream(stream);
foreach (var prop in image.PropertyItems) {
if ((prop.Id == 0x0112 || prop.Id == 5029 || prop.Id == 274)) {
var value = (int)prop.Value[0];
if (value == 6) {
image.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
} else if (value == 8) {
image.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
} else if (value == 3) {
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
}
}
}
0

从这篇文章看来,您需要检查ID 274
foreach (PropertyItem p in properties) {
if (p.Id == 274) {
Orientation = (int)p.Value[0];
if (Orientation == 6)
oldImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
if (Orientation == 8)
oldImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
}
}
0

这是解决8个方向值的代码段。
首先要注意以下几点:
EXIF ID 0x0112用于定向。这是有用的EXIF ID参考http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html
0x0112是274的十六进制等效项。 PropertyItem.Id
的数据类型是int
,这意味着274是有用的。
此外,5029有可能被认为是0x5029或20521这关联到ThumbnailOrientation,虽然可能不是这里需要的。
东方图片:
注: img
是System.Drawing.Image
或从它继承,像System.Drawing.Bitmap
。
if (Array.IndexOf(img.PropertyIdList, 274) > -1)
{
var orientation = (int)img.GetPropertyItem(274).Value[0];
switch (orientation)
{
case 1:
// No rotation required.
break;
case 2:
img.RotateFlip(RotateFlipType.RotateNoneFlipX);
break;
case 3:
img.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case 4:
img.RotateFlip(RotateFlipType.Rotate180FlipX);
break;
case 5:
img.RotateFlip(RotateFlipType.Rotate90FlipX);
break;
case 6:
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
break;
case 7:
img.RotateFlip(RotateFlipType.Rotate270FlipX);
break;
case 8:
img.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
}
// This EXIF data is now invalid and should be removed.
img.RemovePropertyItem(274);
}
新手导航
- 社区规范
- 提出问题
- 进行投票
- 个人资料
- 优化问题
- 回答问题
0
我有一个在iPhone上拍摄的JPEG图像。在我的台式机(Windows Photo Viewer,Google Chrome等)上,方向不正确。
我正在使用ASP.NET MVC 3 Web应用程序,需要在其中上传照片(当前使用plupload)。
我有一些服务器端代码来处理图像,包括读取EXIF数据。
我尝试读取EXIF元数据中的
PropertyTagOrientation
字段(使用GDI-Image.PropertyItems
),但是该字段不存在。因此,它可能是一些特定的iPhone元数据,也可能是其他一些元数据。
我使用了另一个工具,例如Aurigma Photo Uploader,它可以正确读取元数据并旋转图像。它是如何做到的?
是否有人知道Aurigma使用的其他JPEG元数据还可以包含所需的信息以便知道需要旋转吗?
这是我用来读取EXIF数据的代码:
有任何想法吗?