How to write image metadata (XAML)
We show you how to use a BitmapEncoder object to write image metadata. You can write metadata using Windows properties or the Windows Imaging Component (WIC) metadata query language.
For more info about metadata access using BitmapDecoder and BitmapEncoder, see How to read image metadata.
What you need to know
Technologies
- Create a "Hello, world" app (XAML)
- Windows.Graphics.Imaging
- WIC metadata query language
- WIC metadata query language
- WIC native image format metadata queries
- Supported Windows properties
Prerequisites
- We assume you know how to create a basic Windows Runtime app using C++, C#, or Visual Basic. For more info, see Create a "Hello, world" app (XAML).
- You should know how to encode an image using BitmapEncoder, For more info, see How to encode a new image.
Instructions
Step 1: Get an encoder object
Write the beginning a function that receives a BitmapEncoder object.
async void EditMetadata(Windows.Graphics.Imaging.BitmapEncoder encoder) {
The encoder lets you to access the image metadata. If you don't have a encoder object yet, see How to encode a new image.
Step 2: Create a collection of metadata to set
Use a BitmapPropertySet to store metadata items that you want to set on the encoder. Each metadata item is a key-value pair.
The key is a string that identifies the metadata item to set. BitmapEncoder accepts some Windows properties as well as queries 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.
The value is a BitmapTypedValue which lets you to associate the actual metadata value with an explicit data type (Windows.Foundation.PropertyType).
Set the System.Photo.Orientation metadata, which specifies the EXIF orientation, to a value of 1, which specifies a "normal" orientation, and assign it a data type of uint16.
var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet();
var orientationValue = new Windows.Graphics.Imaging.BitmapTypedValue(
1, // Defined as EXIF orientation = "normal"
Windows.Foundation.PropertyType.UInt16
);
propertySet.Add("System.Photo.Orientation", orientationValue);
Step 3: Set the metadata on the encoder
After you are done constructing all of the metadata items, set them on the encoder and continue with the encoding operation.
try
{
await encoder.BitmapProperties.SetPropertiesAsync(propertySet);
}
catch (Exception err)
{
switch (err.HResult)
{
case unchecked((int)0x88982F41): // WINCODEC_ERR_PROPERTYNOTSUPPORTED
// The file format does not support this property.
break;
default:
throw err;
}
}
// Continue the encoding operation.
}
Related topics
- Simple imaging sample
- Windows.Graphics.Imaging
- BitmapPropertiesView
- BitmapPropertySet
- BitmapTypedValue
- SetPropertiesAsync
- How to encode a new image
- How to read image metadata
- WIC metadata query language
- WIC native image format metadata queries
- Supported Windows properties
- System.Photo Properties