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:
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:
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:
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.Metadata.Clone() as BitmapMetadata;
7: string q = "/xmp/exif:UserComment/x-default";
8: if (metadata.ContainsQuery(q))
10: metadata.SetQuery(q, comment);
14: metadata.SetQuery("/xmp/exif:UserComment", new BitmapMetadata("xmpalt"));
15: metadata.SetQuery(q, comment);
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):
Hopefully this is useful to some folks out there. Enjoy!