TASSOGRAPHY

Photography and More
by Ben Vincent
TransparentPadder

Share On FacebookShare On Facebook
 
Ben's Blog (71)Ben's Blog (71)
Development (16)Development (16)
Lisa's Blog (6)Lisa's Blog (6)
 
Show All (94)Show All (94)
 
Jun 2010 (4)Jun 2010 (4)
May 2010 (2)May 2010 (2)
Apr 2010 (7)Apr 2010 (7)
Mar 2010 (5)Mar 2010 (5)
Feb 2010 (1)Feb 2010 (1)
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 (94)Show All (94)
 
RSS & Atom FeedsRSS & Atom Feeds




TransparentPadder
Home Job Runner Image Usage Tracker Image Blog   |   Highlights   |   Photography   |   Contact   |   About    Atom & RSS Feeds  

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 Geotagging.


Posted: Wed, 4 Nov 2009, 05:06:53 GMT Updated: Wed, 4 Nov 2009, 05:42:37 GMT by Ben Vincent
Category: Development
Tags: Windows Imaging Component, XMP, Exif, IPTC, Fotofly


TransparentPadder TransparentPadder TransparentPadder

Tweet  Ben - Cool! A nice end to the week, my mum was waiting for me in St Louis, she kept it a secret!

(Updated: Fri, 30 Jul 2010, 23:10:30)

Alcatraz . American Express . Amusing Photos . ASP.Net MVC . Australia . Bing . Blue Angels . BMi . Breathe Magazine . British Food . British Virgin Islands . California Highway Patrol . Camping . Canada . Christmas . Christmas Tree . Cleaner Shrimp . codeplex . Cooking . Coral Sea . Diving . Dragonlance . Engagement . Exif . Expedia . Facebook . Fish Tank . Flying . Food . Fotofly . Geotagging . Golden Gate National Recreation Area . Google . Gordon Ramsay . Hotmail . IPTC . Michelin Star . Microsoft . Microsoft Giving Campaign . Monterey Bay Aquarium . Mountain Biking . Nudibranch . San Francisco . Search Engine Optimzation (SEO) . Ship Wrecks . Silverlight . Starbucks . Tassography . The Sea Nettle Jelly . TransRockies . TV Commercial . United Airlines . Usage . Windows 7 . Windows Imaging Component . Windows Live . Windows Live Calendar . Windows Live Photo Gallery . Wineries . XMP


This website, all photography & other content is Copyright © Ben Vincent. Unauthorised use of images is strictly prohibited.
Catalog Last Updated: Mon, 19 Jul 2010, 22:48:43    |   Version v4.0.3861.38195
Powered by
Fotofly Logo