///
/// Tested png,jpg
///
/// file full path
/// 使用ImageFile的const常量,需要修改的property的id
/// property value
/// PropertyItemType中的枚举值,具体参数参照summary中的解释
public static void SetImagePropertyValue(FileInfo fileinfo, int propertyid,
string value, PropertyItemType type)
{
//Also can use Bitmap
//http://stackoverflow.com/questions/15231161/value-of-image-property-c
Image image = Image.FromFile(fileinfo.FullName);
var data = Encoding.UTF8.GetBytes(value);
PropertyItem propItem = image.PropertyItems[0];
propItem.Type = (short)type;
propItem.Id = propertyid;
propItem.Len = data.Length;
propItem.Value = data;
image.SetPropertyItem(propItem);
Bitmap myImage1024 = new Bitmap(image);
foreach (PropertyItem item in image.PropertyItems)
{
myImage1024.SetPropertyItem(item);
}
Image newImage = myImage1024;
image.Dispose();
newImage.Save(fileinfo.FullName);
newImage.Dispose();
}
static void Main(string[] args)
{
string filePath = @"C:\Users\aaa\Desktop\6.jpg";
FileInfo tempFileinfo = new FileInfo(filePath);
//byte[] tempvalue={0,1};
//string str = System.Text.Encoding.Default.GetString(tempvalue);
ImageFile.SetImagePropertyValue(tempFileinfo, 40095, "subject1", ImageFile.PropertyItemType.Byte);
}
用了这个方法之后发现file的property都被置为空,file的property的id为subject,求大神指点,坑了我一天了,急,在线等