设置图像元数据之前保存 [英] Set image meta data before save

查看:130
本文介绍了设置图像元数据之前保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置从调整大小退出图像的元数据,属性似乎是确定,但没有保存后出现。



我。敢肯定我在做一些愚蠢的任何想法。

  VAR PI = createPropertyItem(); 

pi.Id = 40091;
pi.Len =SomeText则会。长度;
pi.Type = 2;
pi.Value = Encoding.UTF8.GetBytes(SomeText则会);
SrcImage.SetPropertyItem(PI);
SrcImage.Save(@C:\temp\withTag.jpg);

私人PropertyItem createPropertyItem()
{
变种CI = typeof运算(PropertyItem);
变种O = ci.GetConstructor(BindingFlags.NonPublic可| BindingFlags.Instance | BindingFlags.Public,空,新类型[] {},NULL);

回报(PropertyItem)o.Invoke(NULL);
}


解决方案

好了,一些测试后,这个工程......但是,只有当财产40091没有在原来的形象存在。如果它存在,它不会被替换(必须承认,我不知道为什么)。

  VAR图像= Image.FromFile(@ C:\Tools\test.jpg); 
VAR propertyItem = createPropertyItem();
变种文字=敬畏+ char.MinValue; //添加\0在你的字符串$ B $的B端propertyItem = createPropertyItem();
propertyItem.Id = 40091;
propertyItem.Value = Encoding.Unicode.GetBytes(文本); //改变为Unicode
propertyItem.Len = propertyItem.Value.Length;
propertyItem.Type = 1; //它不是2型!
image.SetPropertyItem(propertyItem);
image.Save(@C:\Tools\test2.jpg);


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

解决方案

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

这篇关于设置图像元数据之前保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆