Welcome to WebmasterWorld Guest from 18.206.168.65

Forum Moderators: ocean10000

Message Too Old, No Replies

Add/Modify EXIF info of PNG file

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts:662
votes: 0


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

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

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


//This is to flush out changes before closing.
pngStream.flush();
pngStream.Close();
2:06 pm on July 15, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts:662
votes: 0


I'll give that a try when I get home, thanks
7:48 am on July 17, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


Did you manage to get it working?
1:26 pm on July 17, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts:662
votes: 0


No. Just tried last night, same problem
2:06 pm on July 17, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


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.

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts:662
votes: 0


That'd be great, thanks.
5:58 pm on July 17, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


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