| Writing Photo Metadata Using Windows Imaging Component by Ben Vincent |
In this blog I’m going to build on my previous posting on Reading Metadata and explain how to write metadata stored in a jpg file using Windows Imaging Component. You can find all my blogs on Windows Imaging Component here. If you want to change any of the metadata, the first thing you have to do is make sure there’s room for your changes. This is done by adding padding to the metadata. If you don’t do this, I guess it’s possible to remove data or change it to the exact same size but you can’t add new data. The general recommendation appears to be around 5k, so I normally use 5120. You pass in the BitmapMetadata you retrieved from my ReadMetadata method. The method will then add padding to the EXIF, IFD & XMP sections of the metadata. | public static void AddMetadataPadding(BitmapMetadata bitmapMetadata, uint paddingAmount) { // Queries for the EXIF, IFD & XMP Padding string exifPadding = "/app1/ifd/exif/PaddingSchema:Padding"; string ifdPadding = "/app1/ifd/PaddingSchema:Padding"; string xmpPadding = "/xmp/PaddingSchema:Padding"; // Ensure we have enough EXIF padding if (Convert.ToInt32(bitmapMetadata.GetQuery(exifPadding)) < paddingAmount) { bitmapMetadata.SetQuery(exifPadding, paddingAmount); } // Ensure we have enough XMP padding if (Convert.ToInt32(bitmapMetadata.GetQuery(xmpPadding)) < paddingAmount) { bitmapMetadata.SetQuery(xmpPadding, paddingAmount); } // Ensure we have enough IFD padding if (Convert.ToInt32(bitmapMetadata.GetQuery(ifdPadding)) < paddingAmount) { bitmapMetadata.SetQuery(ifdPadding, paddingAmount); } } | Now that you’ve read the data and added padding, you are ready to make your changes and save. So lets skip to the save method before bringing it all together. The WriteMetadata method take three parameters. The file containing the original image data, the file you want to create and the BitmapMetadata you’ve changed. | public static void WriteMetadata(string sourceFile, string destinationFile, BitmapMetadata newMetadata) { BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile; // Open Source file (used to get Image and Thumbnail) using (Stream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read)) { // Decode the Source BitmapDecoder sourceDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.None); // Create a new jpeg frame, replacing the destination metadata with the source BitmapFrame destinationFrame = BitmapFrame.Create(sourceDecoder.Frames[0], sourceDecoder.Frames[0].Thumbnail, newMetadata, sourceDecoder.Frames[0].ColorContexts); // Create a new in-memory JPG file JpegBitmapEncoder destinationEncoder = new JpegBitmapEncoder(); destinationEncoder.Frames.Add(destinationFrame); // Open the destination file and save the jpg to it using (Stream destinationStream = File.Open(destinationFile, FileMode.Create, FileAccess.ReadWrite)) { destinationEncoder.Save(destinationStream); } } } | With that done we’re now ready to bring it all together in a simple example that changes the title of a photo. | // Open the destination file and retrieve the metadata BitmapMetadata bitmapMetadata = this.ReadMetadata("TestInput.jpg"); // Adding padding this.AddMetadataPadding(bitmapMetadata, 5012); // Change the title bitmapMetadata.Title = "Test Title"; // Save the metadata into a new file, using the image from the original file this.WriteMetadata("TestInput.jpg", "TestOutput.jpg", bitmapMetadata); | That’s it, nice and simple when you know how. One thing to note: WIC allows you to edit metadata without re-encoding the image, thus you do not lose any data. Thi is often a concern when handling jpg files. I spent a lot of time looking at this when I started using WIC and I can safely say, based on my experience, you don’t lose image quality. You may notice a small increase in file size and that’s due to the padding you’ve added. Secondly: It’s also worth noting that this code only works with JPG because the queries for tiffs are different. It’s a fairly simple matter to change all the queries if you want to support tiff. In my future blogs I’ll talk about how to make more complicated changes the BitmapMetadata object which aren’t exposed in it’s standard properties. Such as writing Windows Live Photo Gallery People Tags and GPS Coordinates. |
|
|
| Posted: Wed, 4 Nov 2009, 05:06:53 GMT (Updated: Wed, 4 Nov 2009, 05:42:37 GMT) by Ben Vincent | 0 Comments |
| Category: Development |
| Tags: Windows Imaging Component, XMP, Exif, IPTC, FotoFly |
|
| |
|
|  |  |
 |
|
Sunday, January 24, 2010 |
| - | 2010-01-23 Lisa's Birthday |
Sunday, January 17, 2010 |
| - | 2010-01-18 Things We Collect |
| - | 2010-01-17 Weekend in Half Moon Bay |
Saturday, January 16, 2010 |
| - | 2009-12-31 Holiday in Beaver Creek |
Sunday, October 18, 2009 |
| - | 2009-10-10 San Francisco Fleet Week 2009 |
Sunday, October 04, 2009 |
| - | 2009-10-04 Biking Around The Bay |
Sunday, August 30, 2009 |
| - | 2009-08-26 Caltrain |
| - | 2009-08-22 Biking And Fracturing a Rib in Mammoth Lakes |
Tuesday, August 11, 2009 |
| - | 2009-08-09 Biking on Pine Mountain |
Wednesday, August 05, 2009 |
| - | 2009-08-01 Lisa's Mum and Sister Visit |
|
|