如何将自定义EXIF标记添加到图像 [英] How to add custom EXIF tags to a image

查看:503
本文介绍了如何将自定义EXIF标记添加到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在图像(JPG,PNG或其他内容)上添加新标签("LV95_original")

I'd like to add a new tag ("LV95_original") to an image ( JPG, PNG or something else..)

如何向图像添加自定义EXIF标签?

How can I add a custom EXIF tag to a image?

这是我到目前为止尝试过的:

This is what I've tried so far:

using (var file = Image.FromFile(path))
{
    PropertyItem propItem = file.PropertyItems[0];
    propItem.Type = 2;
    propItem.Value = System.Text.Encoding.UTF8.GetBytes(item.ToString() + "\0");
    propItem.Len = propItem.Value.Length;
    file.SetPropertyItem(propItem);
}

这是我研究的内容:

添加自定义属性:这使用了一些不同的方法

Add custom attributes: This uses something different

SetPropert: This updates a property, I need to add a new one

添加EXIF信息:这将更新标准标签

添加新标签:这就是我尝试过的,没做过的

Add new tags: This is what I've tried, didn work

推荐答案

实际上链接正常.但是,您确实错过了一个要点:

Actually the link you are using is working just fine. You did however miss one important point:

  • 您应该将Id设置为合适的值; 0x9286是'UserComment',无疑是一个很好的玩法.
  • You should set the Id to a suitable value; 0x9286 is 'UserComment' and certainly a good one to play with.

您可以自己制作新的ID,但Exif观众可能不会选择这些ID.

You can make up new Ids of your own but Exif viewers may not pick those up..

也:您应该任一已知文件中获取有效的PropertyItem!那是您确定它有一个的那一个. 或者,如果您确定,您的目标文件确实至少有一个PropertyItem,则可以继续将其用作要添加的文件的原型,但是您仍然需要更改其Id.

Also: You should either grab a valid PropertyItem from a known file! That is one you are sure that it has one. Or, if you are sure your target file does have ar least one PropertyItem you can go ahead and use it as a proptotype for the one you want to add, but you still need to change its Id.

public Form1()
{
    InitializeComponent();
    img0 = Image.FromFile(aDummyFileWithPropertyIDs);
}

Image img0 = null;

private void button1_Click(object sender, EventArgs e)
{
    PropertyItem propItem = img0.PropertyItems[0];
    using (var file = Image.FromFile(yourTargetFile))
    {
        propItem.Id = 0x9286;  // this is called 'UserComment'
        propItem.Type = 2;
        propItem.Value = System.Text.Encoding.UTF8.GetBytes(textBox1.Text + "\0");
        propItem.Len = propItem.Value.Length;
        file.SetPropertyItem(propItem);
        // now let's see if it is there: 
        PropertyItem propItem1 = file.PropertyItems[file.PropertyItems.Count()-1];
        file.Save(newFileName);
    }
}

有一些ID列表,可从此处找到.

请注意,由于您仍要保留旧文件,因此需要保存到新文件.

Note that you will need to save to a new file since you are still holding onto the old one..

您可以通过其ID进行检索:

You can retrieve by their ID:

PropertyItem getPropertyItemByID(Image img, int Id)
{
    return img.PropertyItems.Select(x => x).FirstOrDefault(x => x.Id == Id);
}

并获得这样的字符串值:

and get at string values like this:

PropertyItem pi = getPropertyItemByID(file, 0x9999);  // ! A fantasy Id for testing!
if (pi != null)
{
    Console.WriteLine( System.Text.Encoding.Default.GetString(pi.Value));
}

这篇关于如何将自定义EXIF标记添加到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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