问题
I'm trying to set the metadata of an image on exit from a resize, the property appears to be set but after the save nothing is there.
I'm pretty sure I'm doing something stupid any ideas.
var pi = createPropertyItem();
pi.Id = 40091;
pi.Len = "SomeText".Length;
pi.Type = 2;
pi.Value = Encoding.UTF8.GetBytes("SomeText");
SrcImage.SetPropertyItem(pi);
SrcImage.Save(@"C:\temp\withTag.jpg");
private PropertyItem createPropertyItem()
{
var ci = typeof (PropertyItem);
var o = ci.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public , null, new Type[] {} , null);
return (PropertyItem)o.Invoke(null);
}
回答1:
Well, after some tests, this works... But only if property 40091 doesn't exist in the original Image. If it exists, it's not replaced (must admit I don't know why).
var image = Image.FromFile(@"C:\Tools\test.jpg");
var propertyItem = createPropertyItem();
var text = "awe" + char.MinValue;//add \0 at the end of your string
propertyItem = createPropertyItem();
propertyItem.Id = 40091;
propertyItem.Value = Encoding.Unicode.GetBytes(text);//change to Unicode
propertyItem.Len = propertyItem.Value.Length;
propertyItem.Type = 1;//it's not type 2 !
image.SetPropertyItem(propertyItem);
image.Save(@"C:\Tools\test2.jpg");
来源:https://stackoverflow.com/questions/11396050/set-image-meta-data-before-save