问题描述
使用以下代码获取图像方向时出现问题
having problem in getting image orientation with below code
string fileName = @"D:\...\...\01012015004435.jpeg"; int rotate = 0; using (var image = System.Drawing.Image.FromFile(fileName)) { foreach (var prop in image.PropertyItems) { if (prop.Id == 0x112) { if (prop.Value[0] == 6) rotate = 90; if (prop.Value[0] == 8) rotate = -90; if (prop.Value[0] == 3) rotate = 180; prop.Value[0] = 1; } } }
在获得正确的方向后用于旋转图像,例如
and after get proper orientation i used to rotate image like
private static RotateFlipType OrientationToFlipType(string orientation) { switch (int.Parse(orientation)) { case 1: return RotateFlipType.RotateNoneFlipNone; break; case 2: return RotateFlipType.RotateNoneFlipX; break; case 3: return RotateFlipType.Rotate180FlipNone; break; case 4: return RotateFlipType.Rotate180FlipX; break; case 5: return RotateFlipType.Rotate90FlipX; break; case 6: return RotateFlipType.Rotate90FlipNone; break; case 7: return RotateFlipType.Rotate270FlipX; break; case 8: return RotateFlipType.Rotate270FlipNone; break; default: return RotateFlipType.RotateNoneFlipNone; } }
但问题出在第一个代码中
but problem is in first code
prop.Id 我总是得到[20625]
prop.Id i always get [20625]
prop.Id == 20625
因此每次$ b都不满足条件$ b请让我知道是否有任何问题或其他选择
so not satisfy the condition every timeplease let me know if any problem or other option
谢谢
推荐答案
其他答案和注释中可能有足够的信息可以将所有内容组合在一起,但这是一个有效的代码示例。
There's probably enough information in the other answers and comments to put this all together, but here's a working code example.
此扩展方法将使用 System.Drawing 图片,读取其Exif方向标签(如果有),然后翻转/旋转(如果需要)。
This extension method will take a System.Drawing Image, read its Exif Orientation tag (if present), and flip/rotate it (if necessary).
private const int exifOrientationID = 0x112; //274 public static void ExifRotate(this Image img) { if (!img.PropertyIdList.Contains(exifOrientationID)) return; var prop = img.GetPropertyItem(exifOrientationID); int val = BitConverter.ToUInt16(prop.Value, 0); var rot = RotateFlipType.RotateNoneFlipNone; if (val == 3 || val == 4) rot = RotateFlipType.Rotate180FlipNone; else if (val == 5 || val == 6) rot = RotateFlipType.Rotate90FlipNone; else if (val == 7 || val == 8) rot = RotateFlipType.Rotate270FlipNone; if (val == 2 || val == 4 || val == 5 || val == 7) rot |= RotateFlipType.RotateNoneFlipX; if (rot != RotateFlipType.RotateNoneFlipNone) img.RotateFlip(rot); }
这篇关于获取图像方向并按照方向旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!