【问题标题】:Is there any way to write EXIF data in PNG using c#有没有办法使用c#在PNG中写入EXIF数据
【发布时间】:2015-05-07 16:42:29
【问题描述】:

我知道 PNG 不支持 EXIF 元数据(根据 W3C),但我有一个工具可以在 zTXt 块中的 PNG 中注入 EXIF 元数据,我到处搜索,但无论如何都没有找到如何编写 EXIF 数据在 PNG 中使用 c# 使用 sqtquery 或任何其他方式。

【问题讨论】:

  • 因此,您的目标是从 C# 编写 zTXt 块。根据this answer 的说法,本机库无法做到这一点。但是这个答案有一些选择,包括链接到非托管代码和使用 libpng,它确实支持你需要的东西。您可能还会发现 sharpapng 库很有用。
  • 您可以编写代码来注入块。 png 文件格式真的很简单。
  • 可能是 System.Windows.Media 中的 PngBitmapDecoder 和 PngBitmapEncoder
  • 我已经尝试过 pngbitmapdecoder,我可以使用简单的文本块编写但不知道如何编写 exif 元数据。
  • 查理,我觉得你是对的,我会试试你的建议,然后告诉你。

标签: c# png exif


【解决方案1】:

只要您可以将数据写入文本,这是一个简单的解决方案:

    public void WriteTextInPngFile(string filename, string description, string otherText)
{
    Stream pngStream = new System.IO.FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
    PngBitmapDecoder pngDecoder = new PngBitmapDecoder(pngStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
    BitmapFrame pngFrame = pngDecoder.Frames[0];
    InPlaceBitmapMetadataWriter pngInplace = pngFrame.CreateInPlaceBitmapMetadataWriter();
    if (pngInplace.TrySave() == true)
    { 
        pngInplace.SetQuery("/Text/Description", description); 
        pngInplace.SetQuery("/Text/YourField", otherText); 
    }
    pngStream.Close();
}

您必须设置以下引用:

  • PresentationCore
  • System.Xaml
  • WindowsBase

这个例子是基于BitmapFrame Class的帮助

【讨论】:

    猜你喜欢
    • 2011-01-09
    • 2014-08-15
    • 2011-11-24
    • 1970-01-01
    • 2014-09-09
    • 2020-01-02
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多