获取图像方向并根据方向旋转
本文关键字:方向 旋转 图像 获取 | 更新日期: 2023-09-27 18:36:54
使用以下
代码获取图像方向时遇到问题
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;
}
}
}
获得正确的方向后,我曾经像旋转图像一样
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;
}
}
但问题出在第一个代码中
prop.Id
我总是得到[20625]
prop.Id == 20625
所以不是每次都满足条件如果有任何问题或其他选择,请告诉我
谢谢
其他答案和注释中可能有足够的信息来将它们放在一起,但这里有一个工作代码示例。
此扩展方法将采用System.Drawing
Image
,读取其Exif方向标签(如果存在),然后翻转/旋转它(如有必要)。
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);
img.RemovePropertyItem(exifOrientationID);
}
}
使用以下命令:
-
img.PropertyIdList.Contains(orientationId)
检查是否存在 Exif 标记。 -
img.GetPropertyItem(orientationId)
得到它(在上述检查之后,否则你会得到一个ArgumentException
)。 -
img.SetPropertyItem(pItem)
设置它。
我写了一个简单的帮助程序类来完成所有这些工作:你可以在这里检查完整的源代码。
其他信息和快速案例研究也可以在我博客上的以下帖子中找到:
在 NET C# 中更改 iPhone 和/或 Android 图片的图像方向
您可以使用
此链接 http://regex.info/exif.cgi 检查图像嵌入的元数据。如果在 EXIF 表中找不到"0x0112",则图像不包含旋转属性。
也许您调整了图像的大小,此时您丢失了exif,并且想根据exif制作旋转图像,在这种情况下,您可以将exif复制到调整大小的图像中:
const string inputFileName = "input.jpg";
const string outputFileName = "output.jpg";
var newSize = new Size(640, 480);
using (var bmpInput = Image.FromFile(inputFileName))
{
using (var bmpOutput = new Bitmap(bmpInput, newSize))
{
foreach (int exif in bmpInput.PropertyIdList)
bmpOutput.SetPropertyItem(bmpInput.GetPropertyItem(exif));
bmpOutput.Save(outputFileName, ImageFormat.Jpeg);
}
}
这是我
使用的:
// pass in an image
ChkImage.ImageOrientation image = ChkImage.GetOrientation(picCrop.Image);
public enum ImageOrientation
{
/// <summary>
/// Image is correctly oriented
/// </summary>
Original = 1,
/// <summary>
/// Image has been mirrored horizontally
/// </summary>
MirrorOriginal = 2,
/// <summary>
/// Image has been rotated 180 degrees
/// </summary>
Half = 3,
/// <summary>
/// Image has been mirrored horizontally and rotated 180 degrees
/// </summary>
MirrorHalf = 4,
/// <summary>
/// Image has been mirrored horizontally and rotated 270 degrees clockwise
/// </summary>
MirrorThreeQuarter = 5,
/// <summary>
/// Image has been rotated 270 degrees clockwise
/// </summary>
ThreeQuarter = 6,
/// <summary>
/// Image has been mirrored horizontally and rotated 90 degrees clockwise.
/// </summary>
MirrorOneQuarter = 7,
/// <summary>
/// Image has been rotated 90 degrees clockwise.
/// </summary>
OneQuarter = 8
}
public static PropertyItem SafeGetPropertyItem(Image image, int propid)
{
try
{
return image.GetPropertyItem(propid);
}
catch (ArgumentException)
{
return null;
}
}
public static ImageOrientation GetOrientation(this Image image)
{
PropertyItem pi = SafeGetPropertyItem(image, 0x112);
if (pi == null || pi.Type != 3)
{
return ImageOrientation.Original;
}
return (ImageOrientation)BitConverter.ToInt16(pi.Value, 0);
}