简体   繁体   English

如何将自定义EXIF标记添加到图像

[英]How to add custom EXIF tags to a image

I'd like to add a new tag ("LV95_original") to an image ( JPG, PNG or something else..) 我想向图像(JPG,PNG或其他内容)添加新标签(“ LV95_original”)。

How can I add a custom EXIF tag to a image? 如何向图像添加自定义EXIF标签?

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

This is what I've researched: 这是我研究的内容:

Add custom attributes : This uses something different 添加自定义属性 :这使用了一些不同的方法

SetPropert : This updates a property, I need to add a new one SetPropert :这将更新一个属性,我需要添加一个新属性

Add EXIF info : This updates standard tags 添加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: 但是,您确实错过了一个重要点:

  • You should set the Id to a suitable value; 您应该将Id设置为适当的值; 0x9286 is 'UserComment' and certainly a good one to play with. 0x9286是'UserComment',无疑是一个不错的选择。

You can make up new Ids of your own but Exif viewers may not pick those up.. 您可以自己制作新的ID,但Exif观看者可能不会选择这些ID。

Also: You should either grab a valid PropertyItem from a known file! 另外:您应该抓住有效PropertyItem已知的文件! 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 . 或者,如果您确定目标文件中确实有至少一个PropertyItem ,则可以继续将其用作要添加的PropertyItem的原型,但仍需要更改其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);
    }
}

There is are lists of IDs to be found from here . 从此处可以找到 ID列表。

Note that you will need to save to a new file since you are still holding onto the old one.. 请注意,由于仍要保留旧文件,因此需要保存到新文件。

You can retrieve by their ID: 您可以通过其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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM