C# 保存前设置图像元数据

C# 保存前设置图像元数据,c#,image,gdi,C#,Image,Gdi,我试图在退出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:\te

我试图在退出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。如果它存在,它不会被替换(必须承认我不知道为什么)


嗯,当我在windows资源管理器中右键单击时,它不会显示在windows资源管理器中。@RubbleFord你可以尝试这个新版本:并不像注意到的那样完美,但我可以在资源管理器中看到一些东西。@RaphaëlAlthaus你能看看我的问题吗?这与此类似,但与从图像中提取PropertyItem值有关,而不是设置值。
 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");