Set image meta data before save

懵懂的女人 提交于 2019-12-09 01:42:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!