我很难将EXIF数据写入图像。到目前为止,我已经设法编写了需要字符串作为输入的数据。但是,对于需要使用不同类型的元素(例如曝光时间,快门速度)的方法,我一无所知。
我正在遵循this指南,但是除了字符串之外,没有其他示例。该website解释了我必须使用哪种数据类型,而Microsoft documentation提供了相应的数值。不幸的是,我无法用它来解决我的问题。 为了找出哪个ID与哪个值相对应,我使用了this列表和官方documentation。
System.Drawing.Image imgEXIF = System.Drawing.Image.FromFile("D:/def.jpg");
System.Drawing.Image imgDummy = System.Drawing.Image.FromFile("D:/IMG_3214.jpg");
System.Drawing.Imaging.PropertyItem item = imgDummy.PropertyItems[0];
item.Id = 0x9286;
item.Type = 2; //String
item.Value = System.Text.Encoding.UTF8.GetBytes("Hello World\r\nthis is a test\0");
item.Len = item.Value.Length;
imgEXIF.SetPropertyItem(item);
imgEXIF.Save("D:/ghi.jpg");
对于如何写不是字符串的EXIF数据的任何帮助,将不胜感激!
答案 0 :(得分:2)
Value
上的PropertyItem
中是基于该字段的相应Type
的字节数组。对于您已经可以执行的文本字段,该字节数组采用ASCII
字节数组的形式,该数组以null结尾。我在下面的TagTypes
下评论了其他类型。对于“曝光时间”字段,这是一个8字节的数组,由两个无符号的32位整数组成-分子,后跟分母。我们可以使用BitConverter.GetBytes()
方法将uint
(无符号的32位整数)转换为4个字节的表示形式-然后只需与另一个字节数组结合即可获得一对分子和分母。
这里有一些扩展,它们显示了如何在类型2字符串字段之外使用Short / type 3字段和Rational / type 5字段:
public static class ImageMetaExtensions
{
public static void SetMaxAperture(this Image image, uint numerator, uint denominator)
{
SetMetaDataItem(image, MAX_APERTURE, (short)TagTypes.RATIONAL, GetPairUnsigned32Integer(numerator, denominator));
}
public static void SetExposureTime(this Image image, uint numerator, uint denominator)
{
SetMetaDataItem(image, EXPOSURE_TIME, (short)TagTypes.RATIONAL, GetPairUnsigned32Integer(numerator, denominator));
}
public static void SetUserComment(this Image image, string text)
{
SetMetaDataItem(image, USER_COMMENT, (short)TagTypes.ASCII, GetNullTerminatedString(text));
}
public static void Set35mmFocalLength(this Image image, short focalLength)
{
SetMetaDataItem(image, FOCALLENGTH_35MM, (short)TagTypes.SHORT, BitConverter.GetBytes(focalLength));
}
public enum TagTypes : short
{
BYTE = 1, // 8 bit unsigned integer
ASCII = 2,
SHORT = 3, // 16-bit unsigned integer
LONG = 4, // 32-bit unsigned integer
RATIONAL = 5, // two unsigned longs - first numerator, second denominator
UNDEFINED = 6, // any value depending on field definition
SLONG = 7, // signed 32-bit
SRATIONAL = 10 // signed pair of 32-bit numerator/denominator
}
private static void SetMetaDataItem(Image image, int id, short type, byte[] data)
{
PropertyItem anyItem = image.PropertyItems[0];
anyItem.Id = id;
anyItem.Len = data.Length;
anyItem.Type = type;
anyItem.Value = data;
image.SetPropertyItem(anyItem);
}
private static byte[] GetPairUnsigned32Integer(uint numerator, uint denominator)
{
return BitConverter.GetBytes(numerator).Concat(BitConverter.GetBytes(denominator)).ToArray();
}
private static byte[] GetNullTerminatedString(string text)
{
return Encoding.ASCII.GetBytes(text + "\0");
}
private const int EXPOSURE_TIME = 0x829A;
private const int USER_COMMENT = 0x9286;
private const int MAX_APERTURE = 0x9205;
private const int FOCALLENGTH_35MM = 0xA405;
}
用法:
System.Drawing.Image myImage = System.Drawing.Image.FromFile(@"c:\temp\someimage.jpg");
myImage.SetExposureTime(1, 30); // 1/30sec
myImage.SetUserComment("Hello, world");
myImage.Set35mmFocalLength(5);
myImage.Save(@"c:\temp\someotherimage.jpg"); // save somewhere else