Blog | Photography | Highlights | Contact | About | Atom & RSS Feeds
Copying Jpeg Metadata using C# and Windows Imaging Component
  • Blog
  • Copying Jpeg Metadata using C# and Windows Imaging Component

It’s a scenario I keep finding myself in, I have two copies of the same photo but I want all metadata changes from one file in the other. For example, I want update my backup with changes I’ve made to metadata but don’t want to touch the source image, or I have the original and a small version of the same file. I haven’t found any reliable way to do this, so I finally sat down and wrote my own using plenty of excellent data on Robert Wlodarczyk’s Blog and the Windows Imaging Component. First, let’s look at the metadata stored in my two files using WICExplorer:  
image image
  As you can see the source file contains significantly more metadata than the destination. Most of the destination metadata is defaults and not really that useful. The code is pretty simple to use, just make sure you have PresentationCore registered in your project and the System.Windows.Media.Imaging namespace.  
public void CopyMetadata(string sourceFile, string destinationFile)
{
   BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile;
   // Create backup of the file so you can read and write to different files
   string tempFile = destinationFile + ".tmp";
   File.Copy(destinationFile, tempFile, true);
   // Open the source file
   using (Stream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read))
   {
      BitmapDecoder sourceDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.None);
      // Check source is has valid frames
      if (sourceDecoder.Frames[0] != null && sourceDecoder.Frames[0].Metadata != null)
      {
         // Get a clone copy of the metadata
         BitmapMetadata sourceMetadata = sourceDecoder.Frames[0].Metadata.Clone() as BitmapMetadata;
         // Open the temp file
         using (Stream tempStream = File.Open(tempFile, FileMode.Open, FileAccess.Read))
         {
            BitmapDecoder tempDecoder = BitmapDecoder.Create(tempStream, createOptions, BitmapCacheOption.None);
            // Check temp file has valid frames
            if (tempDecoder.Frames[0] != null && tempDecoder.Frames[0].Metadata != null)
            {
               // Open the destination file
               using (Stream destinationStream = File.Open(destinationFile, FileMode.Open, FileAccess.ReadWrite))
               {
                  // Create a new jpeg frame, replacing the destination metadata with the source
                  BitmapFrame destinationFrame = BitmapFrame.Create(tempDecoder.Frames[0],
                        tempDecoder.Frames[0].Thumbnail,
                        sourceMetadata,
                        tempDecoder.Frames[0].ColorContexts);
                  // Save the file
                  JpegBitmapEncoder destinationEncoder = new JpegBitmapEncoder();
                  destinationEncoder.Frames.Add(destinationFrame);
                  destinationEncoder.Save(destinationStream);
               }
            }
         }
      }
   }
   // Delete the temp file
   File.Delete(tempFile);
}
After running it through the code my Destination file now has all the same data as the Source file: image Well that’s it for my first dev related Blog posting, expect a few more in the future.

This website, all photography & other content is Copyright © Ben Vincent. Unauthorised use of images is strictly prohibited.
Last Updated: Wed, 14 Dec 2011, 16:30:58    |    Website Version v4.0.4138.41239    |    Content v7.002