How to: Read Image Metadata

Some image files contain metadata that you can read to determine features of the image. For example, a digital photograph might contain metadata that you can read to determine the make and model of the camera used to capture the image. With GDI+, you can read existing metadata, and you can also write new metadata to image files.

GDI+ stores an individual piece of metadata in a PropertyItem object. You can read the PropertyItems property of an Image object to retrieve all the metadata from a file. The PropertyItems property returns an array of PropertyItem objects.

A PropertyItem object has the following four properties: Id, Value, Len, and Type.

A tag that identifies the metadata item. Some values that can be assigned to Id are shown in the following table.

Hexadecimal value

Description

0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091

Image title

Equipment manufacturer

Equipment model

ExifDTOriginal

Exif exposure time

Luminance table

Chrominance table

An array of values. The format of the values is determined by the Type property.

The length (in bytes) of the array of values pointed to by the Value property.

The data type of the values in the array pointed to by the Value property. The formats indicated by the Type property values are shown in the following table

Numeric value

Description

1

A Byte

2

An array of Byte objects encoded as ASCII

3

A 16-bit integer

4

A 32-bit integer

5

An array of two Byte objects that represent a rational number

6

Not used

7

Undefined

8

Not used

9

SLong

10

SRational

The following code example reads and displays the seven pieces of metadata in the file FakePhoto.jpg. The second (index 1) property item in the list has Id 0x010F (equipment manufacturer) and Type 2 (ASCII-encoded byte array). The code example displays the value of that property item.

The code produces output similar to the following:

Property Item 0

id: 0x320

type: 2

length: 16 bytes

Property Item 1

id: 0x10f

type: 2

length: 17 bytes

Property Item 2

id: 0x110

type: 2

length: 7 bytes

Property Item 3

id: 0x9003

type: 2

length: 20 bytes

Property Item 4

id: 0x829a

type: 5

length: 8 bytes

Property Item 5

id: 0x5090

type: 3

length: 128 bytes

Property Item 6

id: 0x5091

type: 3

length: 128 bytes

The equipment make is Northwind Camera.

// Create an Image object. 
Image image = new Bitmap(@"c:\FakePhoto.jpg");

// Get the PropertyItems property from image.
PropertyItem[] propItems = image.PropertyItems;

// Set up the display.
Font font = new Font("Arial", 12);
SolidBrush blackBrush = new SolidBrush(Color.Black);
int X = 0;
int Y = 0;

// For each PropertyItem in the array, display the ID, type, and  
// length. 
int count = 0;
foreach (PropertyItem propItem in propItems)
{
    e.Graphics.DrawString(
    "Property Item " + count.ToString(),
    font,
    blackBrush,
    X, Y);

    Y += font.Height;

    e.Graphics.DrawString(
       "   iD: 0x" + propItem.Id.ToString("x"),
       font,
       blackBrush,
       X, Y);

    Y += font.Height;

    e.Graphics.DrawString(
       "   type: " + propItem.Type.ToString(),
       font,
       blackBrush,
       X, Y);

    Y += font.Height;

    e.Graphics.DrawString(
       "   length: " + propItem.Len.ToString() + " bytes",
       font,
       blackBrush,
       X, Y);

    Y += font.Height;

    count++;
}
// Convert the value of the second property to a string, and display  
// it.
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string manufacturer = encoding.GetString(propItems[1].Value);

e.Graphics.DrawString(
   "The equipment make is " + manufacturer + ".",
   font,
   blackBrush,
   X, Y);

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. Handle the form's Paint event and paste this code into the paint event handler. You must replace FakePhoto.jpg with an image name and path valid on your system and import the System.Drawing.Imaging namespace.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft