Windows Dev Center

Language: HTML | XAML

How to read image metadata (XAML)

We show you how to use a BitmapDecoder retrieve metadata from an image file. Image formats like JPEG, TIFF and PNG support a variety of embedded metadata that describe things like keywords, GPS location, and camera info. You can read metadata using supported Windows properties or the Windows Imaging Component (WIC) metadata query language.

There are two classes to access imaging metadata, depending on your scenario: Windows.Storage.FileProperties.ImageProperties and Windows.Graphics.Imaging.BitmapPropertiesView.

Note  You can use the Windows.Storage.FileProperties APIs to get and set basic properties on a Windows.StorageFile without opening a stream or getting a decoder. See How to get image properties for more info. Accessing native metadata using BitmapDecoder is a more advanced scenario, but gives you access to more metadata than what is provided by the Windows property system.

What you need to know

Technologies

Prerequisites

Instructions

Step 1: Get a decoder object

Write the beginning of a function that receives a BitmapDecoder object.

Note  This example assumes that the decoder was opened on a JPEG image. Metadata items are generally specific to each image format.

async void GetMetadata(Windows.Graphics.Imaging.BitmapDecoder decoder)
{


Use the decoder to access the image metadata. If you don't have a decoder object yet, see How to decode an image. Declare the variables here to keep them in scope.

Step 2: Request one or more metadata items

Every Windows property and metadata item is identified by a string name or a query constructed using the WIC metadata query language. For a list of supported Windows properties, see Supported Windows properties. For a summary of supported WIC metadata queries, see WIC native image format metadata queries.

Create a collection of strings containing the Windows properties or metadata items you want to request. Then pass the collection to GetPropertiesAsync. In this example, we are requesting System.Photo.Orientation, which specifies the EXIF orientation, as well as /xmp/dc:creator, which specifies the image’s creator as defined in the Dublin Core XMP schema.

Note  This example assumes that the decoder was opened on a JPEG image. Metadata items are generally specific to each image format.

    var requests = new System.Collections.Generic.List<string>();
    requests.Add("System.Photo.Orientation"); // Windows property key for EXIF orientation
    requests.Add("/xmp/dc:creator"); // WIC metadata query for Dublin Core creator

    try
    {
        var retrievedProps = await decoder.BitmapProperties.GetPropertiesAsync(requests);


Step 3: Retrieve the returned metadata values and handle errors

GetPropertiesAsync asynchronously returns a BitmapPropertySet which is a collection of key-value pairs. Each key is one of the requested query strings from Step 2. Each associated value is a BitmapTypedValue – this contains the actual metadata value as well as an enumeration indicating its data type (Windows.Foundation.PropertyType).

First, check for the existence of the requested metadata item. If the image doesn’t have this item then it won't be in the returned BitmapPropertySet.

Next, lookup the item and get the value from the returned BitmapTypedValue.

GetPropertiesAsync fails if the image format doesn’t support the metadata you requested. For example, GIF does not support XMP metadata. Some image formats, such as BMP, don't support any metadata at all. Also, as mentioned earlier, metadata items are generally specific to each image format. Like, the metadata query "/xmp/dc:creator" that we used in Step 2 is specific to JPEG. While TIFF also supports XMP metadata, the equivalent query would be "/ifd/xmp/dc:creator". See WIC image format native metadata queries for more info.


        ushort orientation;
        if (retrievedProps.ContainsKey("System.Photo.Orientation"))
        {
            orientation = (ushort)retrievedProps["System.Photo.Orientation"].Value;
        }

        string creator;
        if (retrievedProps.ContainsKey("/xmp/dc:creator"))
        {
            creator = (string)retrievedProps["/xmp/dc:creator"].Value;
        }
    }
    catch (Exception err)
    {
        switch (err.HResult)
        {
            case unchecked((int)0x88982F41): // WINCODEC_ERR_PROPERTYNOTSUPPORTED
                // The file format does not support the requested metadata.
                break;
            case unchecked ((int) 0x88982F81): // WINCODEC_ERR_UNSUPPORTEDOPERATION
                // The file format does not support any metadata.
            default:
                throw err;
        }
    }
}

Related topics

Simple imaging sample
Windows.Graphics.Imaging
BitmapPropertiesView
BitmapPropertySet
BitmapTypedValue
How to decode an image
How to get image properties
How to write image metadata
Metadata Query Language Overview
System.Photo Properties
Photo Metadata Policies

 

 

Show:
© 2015 Microsoft