WIC metadata in .NET: Getting around "Property cannot be found" / System.ArgumentException /"Exception from HRESULT: 0x88982F40"

Here’s a bit of an obscure error message I’ve run into while working with WIC (Windows Imaging Component) on .NET/WPF, trying to write XMP metadata into an image:

image

 

 

 

 

 

 

 

 

 

 

So far, a search on Google has yielded few usable results. I hope this post will change that, yielding at least one useful thing.

The Exception message tells you that you’re trying to write data to a property that doesn’t exist in the file. Microsoft has a tool called WICExplorer that can show you the metadata like this:

WIC_Before

The above shows an image in WICExplorer directly after coming off of my digital camera. Notice that there is no “XMP Reader” node, which would represent XMP metadata. So if you want to put new XMP metadata into the picture, you need to know two things: how to create a new metadata node and what the correct syntax is for doing so.

What helped me figure those two things out were these pages:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/8f297b11-99ba-459c-b085-1ff3ae526487/

http://msdn.microsoft.com/en-us/library/bb643802.aspx

The first one showed how one can create new metadata objects using WIC, the second showed the needed syntax for some of the queries.

So here’s an example of how to create XMP metadata in an image that doesn’t have XMP metadata embedded yet:

   1: string file = "C:\Temp\IMG_1687.JPG";
   2: BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile;
   3: Stream originalFile = File.Open(file, FileMode.Open, FileAccess.ReadWrite)
   4: BitmapDecoder original = BitmapDecoder.Create(originalFile, createOptions, BitmapCacheOption.None);
   5: BitmapMetadata metadata = original.Frames[0].Metadata.Clone() as BitmapMetadata;
   6:  
   7: string q = "/xmp/exif:UserComment/x-default";
   8: if (metadata.ContainsQuery(q))
   9: {
  10:   metadata.SetQuery(q, comment);
  11: }
  12: else
  13: {
  14:   metadata.SetQuery("/xmp/exif:UserComment", new BitmapMetadata("xmpalt"));
  15:   metadata.SetQuery(q, comment);
  16: }

The keys are in line 7, where the initial metadata query is shown and in line 8, where the code tests if the metadata is already present. If not, line 14 shows how to create a new XMP metadata node, which then gets populated in line 15. The writing of the data can be done using a JpegBitmapEncoder according to Robert Wlodarczyk’s blog post (without the InPlaceBitmapMetadataWriter piece.)

After running code like this, you’ll now have the metadata in the file, as shown by this WICExplorer screenshot (I added “App0 Reader” metadata to this image as well):

WIC_After

Hopefully this is useful to some folks out there. Enjoy!

4 Comments

  1. Gnome Rules

    Very help in filling in the XPComment used in windows explorer. I usually gotten the Property Not found bit even though it was listed in the Photo Metadata Policy on msdn. I had to do a live search for /xmp/<xmpalt:exif:UserComment to find this page.

  2. Yeah, sorry, I guess I was assuming people would pick up Robert W.’s code to see those details. You have some great articles on this topic! Thanks for sharing.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.