PropertyItem 上的 Value 中的内容是基于该字段对应的 Type 的字节数组。对于您已经可以执行的文本字段,该字节数组采用以空结尾的ASCII 字节数组的形式。我在TagTypes 下评论过的其他类型。对于曝光时间字段,这是一个 8 字节数组,由两个无符号 32 位整数组成 - 分子,后跟分母。我们可以使用BitConverter.GetBytes() 方法将uint(无符号32 位整数)转换为4 字节表示形式——然后简单地与另一个字节数组连接以获得分子和分母对。
以下是一些扩展,它们展示了如何使用除了 Type 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