【问题标题】:Set image meta data before save保存前设置图像元数据
【发布时间】:2012-07-08 21:51:03
【问题描述】:

我正在尝试在退出调整大小时设置图像的元数据,该属性似乎已设置,但保存后什么都没有。

我很确定我在做一些愚蠢的想法。

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

【问题讨论】:

    标签: c# image gdi


    【解决方案1】:

    好吧,经过一些测试,这可行……但前提是原始图像中不存在属性 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");
    

    【讨论】:

    • 嗯,当我在 Windows 资源管理器中右键单击时,它没有显示在 Windows 资源管理器中。
    • @RubbleFord 你也许可以试试这个新版本:并不完美,但我可以在资源管理器中看到一些东西。
    • @RaphaëlAlthaus 你能看看我的问题吗?它类似,但与从图像中提取 PropertyItem 值有关,而不是设置 值。 stackoverflow.com/questions/13997101/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多