[英]How to write EXIF data to an image in C#?
我很難將EXIF數據寫入圖像。 到目前為止,我已經設法編寫了需要字符串作為輸入的數據。 但是,對於需要使用不同類型的元素(例如曝光時間,快門速度)的方法,我一無所知。
我正在遵循本指南,但是除了字符串之外,沒有其他示例。 該網站說明了我必須使用哪種數據類型,並且Microsoft文檔提供了相應的數值。 不幸的是,我無法用它來解決我的問題。 為了找出哪個ID與哪個值相對應,我正在使用此列表和官方文檔 。
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數據的幫助!
PropertyItem
Value
中包含一個基於該字段的相應Type
的字節數組。 對於您已經可以執行的文本字段,該字節數組采用以空值終止的ASCII
字節數組的形式。 我在下面的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.