Reading Metadata of Images

If you have ever worked with mechanical and engineering drawings or digital images, you are probably aware of metadata. Metadata is information about the image, that's not part of the image itself. When an engineer draws an image, metadata is often added, such as the following information: last updated, updated by, date, place, and names. A photograph might include metadata such as image title, manufacturer, and model.

In the .NET Framework library, the PropertyItem object is used as a placeholder for metadata. The PropertyItem class provides four properties: Id, Len, Type, and Value. All of these properties have both read and write access.

The Id property is a tag, which identifies the metadata item. Table 9.10 describes Id tag values.

The Value property is an array of values whose format is determined by the Type property. The Len property represents the length of the array of values in bytes. The Type property represents the data type of values stored in the array. Table 9.11 describes the format of the Type property values.

Table 9.10. Id values

Hexadecimal Value

Description

0x0320

Image title

0x010F

Equipment manufacturer

0x0110

Equipment model

0x9003

ExifDTOriginal

0x829A

EXIF exposure time

0x5090

Luminance table

0x5091

Chrominance table

Table 9.11. Format of Type property values

Numeric Value

Description

1

A Byte object

2

An array of Byte objects encoded as ASCII

3

A 16-bit integer

4

A 32-bitinteger

5

An array of two Byte objects that represent a rational number

6

Not used

7

Undefined

8

Not used

9

SLong

10

SRational

An Image object may contain more than one PropertyItem object. The PropertyItems property of the Image class represents an array of PropertyItem objects corresponding to an image. The PropertyIdList property of the Image class returns an array of property IDs stored in an image object. Listing 9.17 uses the PropertyItems property of the Image class and reads all property items of an image.

Listing 9.17 Reading the metadata of a bitmap

private void Form1_Load(object sender,
 System.EventArgs e)
{
 // Create an image from a file
 Graphics g = this.CreateGraphics();
 Image curImage = Image.FromFile("roses.jpg");
 Rectangle rect = new Rectangle(20, 20, 100, 100);
 g.DrawImage(curImage, rect);
 // Create an array of PropertyItem objects and read
 // items using PropertyItems
 PropertyItem[] propItems = curImage.PropertyItems;
 // Create values of PropertyItem members
 foreach (PropertyItem propItem in propItems)
 {
 System.Text.ASCIIEncoding encoder =
 new System.Text.ASCIIEncoding();
 string str = "ID ="+propItem.Id.ToString("x");
 str += ", Type ="+ propItem.Type.ToString();
 str += ", Length = "+ propItem.Len.ToString();
 str += ", Value ="
 + encoder.GetString(propItem.Value);
 MessageBox.Show(str);
 }
 // Dispose of object
 g.Dispose();
}

Figure 9.25 shows the output from Listing 9.17.

Figure 9.25. Reading the metadata of a bitmap

graphics/09fig25.jpg

GDI+: The Next-Generation Graphics Interface

Your First GDI+ Application

The Graphics Class

Working with Brushes and Pens

Colors, Fonts, and Text

Rectangles and Regions

Working with Images

Advanced Imaging

Advanced 2D Graphics

Transformation

Printing

Developing GDI+ Web Applications

GDI+ Best Practices and Performance Techniques

GDI Interoperability

Miscellaneous GDI+ Examples

Appendix A. Exception Handling in .NET



GDI+ Programming with C#
GDI+ Programming with C#
ISBN: 073561265X
EAN: N/A
Year: 2003
Pages: 145

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net