Forum Moderators: open

Message Too Old, No Replies

Add/Modify EXIF info of PNG file

         

Gibble

7:17 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to add some exif info to a png, I have this code but it doesn't seem to save


if (File.Exists(pngPath))
{
using (Stream pngStream = File.Open(pngPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
PngBitmapDecoder pngDecoder = new PngBitmapDecoder(pngStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapFrame pngFrame = pngDecoder.Frames[0];
InPlaceBitmapMetadataWriter pngInplace = pngFrame.CreateInPlaceBitmapMetadataWriter();
if (pngInplace.TrySave() == true)
{
pngInplace.SetQuery("/Text/Description", "Testing");
}
pngStream.Close();
}
}

Please help!
Thanks

-c

Ocean10000

4:15 am on Jul 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



//This is to flush out changes before closing.
pngStream.flush();
pngStream.Close();

Gibble

2:06 pm on Jul 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll give that a try when I get home, thanks

marcel

7:48 am on Jul 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you manage to get it working?

Gibble

1:26 pm on Jul 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No. Just tried last night, same problem

marcel

2:06 pm on Jul 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was playing around with this code last night, I couldn't get it to work for PNGs but I was succesful with JPGs.

I can post the code for you tomorrow if you like, maybe it will help you with editing a PNG.

Gibble

2:55 pm on Jul 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That'd be great, thanks.

marcel

5:58 pm on Jul 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here you go, this works for JPGs, the BitmapMetadata class (metaData) bolded in the code below has a number of properties, I could add more, but intellisense will help you further :)

/// <summary>
/// Adds meta data to a jpeg file
/// </summary>
/// <param name="fileIn">The full path (including file name) to the source file.</param>
/// <param name="fileOut">The full path (including file name) of the output file.</param>
/// <example>ModifyJpegMetadata(@"C:\temp\image.jpg", @"C:\temp\newImage.jpg");</example>
public void ModifyJpegMetadata(string fileIn, string fileOut)
{
BitmapDecoder decoder = null;
if (File.Exists(fileIn))
{
using (Stream streamIn = File.Open(fileIn, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
decoder = new JpegBitmapDecoder(streamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
}
BitmapFrame frame = decoder.Frames[0];
if (frame != null)
{
InPlaceBitmapMetadataWriter writer = frame.CreateInPlaceBitmapMetadataWriter();
writer.Title = "test";
if (!writer.TrySave() == true)
{
BitmapMetadata metaData = (BitmapMetadata)frame.Metadata.Clone();
if (metaData != null)
{
metaData.SetQuery("/app1/ifd/PaddingSchema:Padding", (UInt32)4096);

metaData.Title = "My Shiny new Title";
metaData.Subject = "Subject lorem ipsum";
metaData.DateTaken = "01-01-2001";
metaData.Copyright = "me ;)";

if (metaData != null)
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(frame, frame.Thumbnail, metaData, frame.ColorContexts));
using (Stream jpegStreamOut = File.Open(fileOut, FileMode.Create, FileAccess.ReadWrite))
{
encoder.Save(jpegStreamOut);
}
}
}
}
}
}
}