How to: Read image metadata

Source: Internet
Author: User
Tags visual studio 2010
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.