Job Runner ImageUsage Tracker Image
TransparentPadder

Share On FacebookShare On Facebook
 
Ben's Blog (57)Ben's Blog (57)
Development (13)Development (13)
Lisa's Blog (4)Lisa's Blog (4)
 
Show All (75)Show All (75)
 
Jan 2010 (3)Jan 2010 (3)
Nov 2009 (8)Nov 2009 (8)
Oct 2009 (4)Oct 2009 (4)
Sep 2009 (4)Sep 2009 (4)
Jul 2009 (6)Jul 2009 (6)
Jun 2009 (5)Jun 2009 (5)
May 2009 (7)May 2009 (7)
Mar 2009 (1)Mar 2009 (1)
Feb 2009 (5)Feb 2009 (5)
Jan 2009 (7)Jan 2009 (7)
Dec 2008 (6)Dec 2008 (6)
Oct 2008 (3)Oct 2008 (3)
Sep 2008 (3)Sep 2008 (3)
Aug 2008 (9)Aug 2008 (9)
Jul 2008 (3)Jul 2008 (3)
Jun 2008 (1)Jun 2008 (1)
 
Show All (75)Show All (75)
 
RSS & Atom FeedsRSS & Atom Feeds




TransparentPadder
Home  |  Shopping Cart (0)Blog  |  Highlights  |  Albums  |  Search  |  Contact  Atom & RSS Feeds

Copying Jpeg Metadata using C# and Windows Imaging Component by Ben Vincent

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.


Posted: Sun, 11 Jan 2009, 03:20:23 GMT (Updated:  Sun, 11 Jan 2009, 08:12:46 GMT) by  Ben Vincent  |  0 Comments
Category: Development
Tags: Windows Imaging Component, XMP, Exif

 

 
TransparentPadderTransparentPadderTransparentPadder
TransparentPadder

Facebook Status  Ben Email decimated, unfortunately more like the Roman definition than the modern one.

(Updated: Wed, 3 Feb 2010, 00:22:01)
TransparentPadder
Alcatraz . American Express . Australia . Bing . Blue Angels . bmi . Breathe Magazine . British Food . Camping . Canada . Christmas . Christmas Tree . Cleaner Shrimp . CodePlex . Cooking . Coral Sea . Cycling . Diving . DragonLance . Engagement . Exif . Expedia . Facebook . Fire Shrimp . Firemen . Fish Tank . Fleet Week . Flying . Foie Gras . Food . FotoFly . GeoTagging . Giving Campaign . Golden Gate National Recreation Area . Google . Goose Fat . Gordon Ramsay . Hollywood . Hotmail . IPTC . Michelin Star . Microsoft . Monterey Bay Aquarium . Mountain Biking . Photography . San Francisco . Search Engine Optimzation . SEO . Silverlight . Tassography.com . TransRockies . United . Usage . Windows 7 . Windows Imaging Component . Windows Live . Windows Live Calendar . Windows Live Photo Gallery . Wineries . XMP

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