C # obtain the EXIF information of an image

Source: Internet
Author: User

C # retrieving EXIF information of images classification: self-developed 1793 people read comments (8) collect reports c # stringbyteimage manufacturing algorithm

Introduction to EXIF information.

1 EXIF is the abbreviation of the English Exchangeable Image File. EXIF is an image file format with the suffix jpg. EXIF information is a series of information collected by a digital camera during the shooting process, and then placed in the header of the jpg file, that is to say, EXIF information is a set of shooting parameters embedded in the JPEG image file format, including the aperture, shutter, ISO, date and time of photography and other information related to the photography conditions at that time, camera brand model, color encoding, recording sound and GPS information. Whether it's supporting software from hardware vendors or professional image scanning tools, all of these software are designed to help digital photographers conveniently save and view important information about photographic images.

2. How to obtain EXIF information

In. NET, we can use the PropertyItem object to obtain the EXIF information of jpg images. It is easy to get PropertyItem.

Image img = Image. FromFile ("Image file path supporting Exif ");

PropertyItem [] pt = img. PropertyItems;

In this way, the Exif information has been loaded into the PropertyItem array. The ID, Type, and Value Attributes of PropertyItem are the most important.

ID can uniquely indicate the meaning of the current PropertyItem. For example, 0x010F represents the camera manufacturer, 0x8827 represents the ISO speed, and 0x829D represents the camera.

Value is always a byte array, but the Value method of the specific content varies with the Type.

Type is an integer, indicating that the Type is described in MSDN as follows:

1. Specify Value as a byte array.

2. Specify the Value as null to terminate the ASCII string. If you set the type data member to ASCII type, you should set the Len attribute to include the string length of null termination. For example, the length of the string "Hello" is 6

3. Specify Value as an unsigned short (16-bit) integer array.

4. Specify the Value as an unsigned 32-bit long integer array.

5. Specify the Value data member as an unsigned long integer pair array. Each pair represents a score. The first integer is the numerator, and the second integer is the denominator.

6. Specify Value as a byte array that can contain values of any data type.

7. Specify Value as a signed 32-bit long integer array.

10 specify Value as a signed long integer pair array. Each pair represents a score. The first integer is the numerator, and the second integer is the denominator.

So get ExifInformation algorithms focus on howValueThe byte array is a value that humans can understand.

 

Public void FindExifinfo (string filePath)

{

Image img = Image. FromFile (filePath );

PropertyItem [] pt = img. PropertyItems;

For (int I = 0; I <pt. Length; I ++)

{

PropertyItem p = pt [I];

Switch (pt [I]. Id)

{// Device manufacturer 20.

Case 0x010F:

This. textBox1.Text = System. Text. ASCIIEncoding. ASCII. GetString (pt [I]. Value );

Break;

Case 0x0110: // device model 25.

This. textBox4.Text = GetValueOfType2 (p. Value );

Break;

Case 0x0132: // The shooting time is 30.

This. textBox2.Text = GetValueOfType2 (p. Value );

Break;

Case 0x829A: //. Time exposed

This. textBox3.Text = GetValueOfType5 (p. Value) + "sec ";

Break;

Case 0x8827: // ISO 40.

This. textBox5.Text = GetValueOfType3 (p. Value );

Break;

Case 0x010E: // image description info. description

This. textBox6.Text = GetValueOfType2 (p. Value );

Break;

Case 0x920a: // the focal length of the photo

This. textBox7.Text = GetValueOfType5A (p. Value) + "mm ";

Break;

Case 0x829D: // The photo circle Value

This. textBox8.Text = GetValueOfType5A (p. Value );

Break;

Default:

Break;

}

}

}

Public string GetValueOfType2 (byte [] B) // read the value of type = 2

{

Return System. Text. Encoding. ASCII. GetString (B );

}

Private static string GetValueOfType3 (byte [] B) // read the value of type = 3

{

If (B. Length! = 2) return "unknow ";

Return Convert. ToUInt16 (B [1] <8 | B [0]). ToString ();

}

Private static string GetValueOfType5 (byte [] B) // read the value of type = 5

{

If (B. Length! = 8) return "unknow ";

UInt32 fm, fz;

Fm = 0;

Fz = 0;

Fz = Convert. ToUInt32 (B [7] <24 | B [6] <16 | B [5] <8 | B [4]);

Fm = Convert. ToUInt32 (B [3] <24 | B [2] <16 | B [1] <8 | B [0]);

Return fm. ToString () + "/" + fz. ToString () + "sec ";

}

Private static string GetValueOfType5A (byte [] B) // obtain the aperture Value

{

If (B. Length! = 8) return "unknow ";

UInt32 fm, fz;

Fm = 0;

Fz = 0;

Fz = Convert. ToUInt32 (B [7] <24 | B [6] <16 | B [5] <8 | B [4]);

Fm = Convert. ToUInt32 (B [3] <24 | B [2] <16 | B [1] <8 | B [0]);

Double temp = (double) fm/fz;

Return (temp). ToString ();

}

Run the following command:

Related Article

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.