How to: Read image metadata
. NET Framework 2.0Other Versions
- . NET Framework 4.5
- Visual Studio 2010
- . NET Framework 3.5
This topic has not been rated-rating this topic
Some image files contain metadata that you can read to determine image features. For example, a digital photo may contain metadata that allows you to read to determine the brand and model of the camera used to capture the image. Using GDI +, you can read existing metadata or write new metadata into an image file.
GDI + stores separate metadata segments in the PropertyItem object. You can read the PropertyItems attribute of an Image object to retrieve all metadata from a file.PropertyItemsProperty returnsPropertyItemObject array.
PropertyItemAn object has the following four attributes:Id,Value,LenAndType.
Id
Used to mark metadata items. The following table shows values that can be assigned IDs.
Hexadecimal value |
Description |
Zero x 0320 0x010F Zero x 0110 Zero x 9003 0x829A Zero x 5090 Zero x 5091 |
Image title Device manufacturer Device Model ExifDTOriginal Exif exposure time Brightness table Color Table |
Value
Array value. The format of these values is determined by the Type attribute.
Len
The array length (in bytes) of the Value to which the Value attribute points ).
Type
ValueThe data type of the value in the array to which the attribute points. The following table showsTypeFormat of attribute value indication
Value |
Description |
1 |
OneByte |
2 |
ASCII encodedByteArray of Objects |
3 |
16-digit integer |
4 |
32-bit integer |
5 |
Contains twoByteArray of Objects |
6 |
Unused |
7 |
Undefined |
8 |
Unused |
9 |
SLong |
10 |
SRational |
Example
The following code example reads and displays the seven-segment metadata in the FakePhoto.jpg file. The second (index 1) attribute in the list containsId0x010F (device manufacturer) andType2 (ASCII encoded byte array ). The code example shows the value of this attribute.
The Code generates 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.
C # VB Replication
// 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);
Compile code
The preceding example is designed to use a Windows form. It requires the PaintEventArgs e parameter of the Paint event handler. Replace FakePhoto.jpg with the valid image name and path on the system.