保存前设置图像元数据

我正在尝试设置退出resize时图像的元数据,该属性似乎已设置但在保存之后没有任何内容。

我很确定我做的任何想法都是愚蠢的。

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); } 

好吧,经过一些测试,这个工作……但只有在原始图像中不存在属性40091时。 如果它存在,它不会被替换(必须承认我不知道为什么)。

  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");