0

I know that EXIF MetaData is not supported in PNG (as per W3C), but i have a tool which can inject EXIF MetaData in PNG in zTXt block, i have searched everywhere but did'nt find anyway how to write EXIF data in PNG using c# using sqtquery or any other way.

Vaibhav J
  • 1,316
  • 8
  • 15
  • So, your goal is to write the zTXt blocks from C#. According to [this answer](http://stackoverflow.com/a/4520842/645511), there isn't a way to do this with the native libraries. But that answer has some alternatives, including linking to unmanaged code and using libpng, which does support what you need. You may also find the [sharpapng](https://code.google.com/p/sharpapng/) library useful. – Katie Kilian Mar 04 '14 at 16:22
  • You could just write code to inject the block. The png file format is really simple. – Millie Smith Mar 04 '14 at 16:29
  • Perhaps PngBitmapDecoder and PngBitmapEncoder from System.Windows.Media – Bauss Mar 04 '14 at 16:33
  • I have tried pngbitmapdecoder, I can write simple text blocks using but don't know how to write exif metadata. – Vaibhav J Mar 04 '14 at 16:52
  • Charlie, I think you are right, I will try your suggestion and will let you know. – Vaibhav J Mar 04 '14 at 16:54
  • You can also try PngCs https://code.google.com/p/pngcs/ – leonbloy Mar 04 '14 at 18:45
  • I also come across pngcs during googling, but didn't find any example/hint related to exif. if you have any example pls share. – Vaibhav J Mar 05 '14 at 03:39

1 Answers1

1

As long as you can write your data as text, this is a straightforward solution:

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

You have to set the following references:

  • PresentationCore
  • System.Xaml
  • WindowsBase

This example is based on the help of BitmapFrame Class