gpt4 book ai didi

c# - 如何向图像添加自定义 EXIF 标签

转载 作者:太空狗 更新时间:2023-10-29 23:13:28 28 4
gpt4 key购买 nike

我想为图像(JPG、PNG 或其他...)添加一个新标签(“LV95_original”)

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

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

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 : 这使用了不同的东西

SetPropert : 这更新了一个属性,我需要添加一个新的

Add EXIF info : 这会更新标准标签

Add new tags : 这是我试过的,没用

最佳答案

实际上是link您正在使用的工作正常。但是,您确实错过了重要的一点:

  • 您应该将Id设置为合适的值; 0x9286 是“UserComment”,当然是一个很好玩的。

您可以自己创建新的 ID,但 Exif 查看器可能不会选择这些 ID。

此外:您应该或者已知文件中获取有效的PropertyItem!那是一个你确定它有一个。 或者,如果您确定您的目标文件至少有一个 PropertyItem,您可以继续将它用作您要添加的原型(prototype),但您仍然需要更改其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 列表 to be found from here .

请注意,您将需要保存到一个新文件,因为您仍然保留旧文件。

您可以通过他们的 ID 检索:

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

并像这样获取字符串值:

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

关于c# - 如何向图像添加自定义 EXIF 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35295284/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com