C # retrieving image Exif information

Source: Internet
Author: User
C # retrieving image Exif information
Source: yocin time: 9:05:02 topic: technical support CTR: 501
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Drawing;
Using System. Drawing. Imaging;

Namespace Console_Application
{
Class Program
{
Public static void Main (string [] args)
{
Dictionary <string, string> exif = new Dictionary <string, string> ();
Image image = Image. FromFile (@ "e: \ www \ test.jpg ");
PropertyItem [] properties = image. PropertyItems;
Foreach (PropertyItem property in properties)
{
Switch (property. Id)
{
// Exif
Case 0x010E:
Exif ["ImageTitle"] = ASCIIToString (property. Value );
Break;
Case 0x010F:
Exif ["Make"] = ASCIIToString (property. Value );
Break;
Case 0x0110:
Exif ["Model"] = ASCIIToString (property. Value );
Break;
Case 0x0112:
Exif ["Orientation"] = jsontostring (property. Value, 0 );
Break;
Case 0x011A:
Exif ["XResolution"] = RationalToSingle (property. Value, 0 );
Break;
Case 0x011B:
Exif ["YResolution"] = RationalToSingle (property. Value, 0 );
Break;
Case 0x0128:
Exif ["ResolutionUnit"] = jsontostring (property. Value, 0 );
Break;
Case 0x0131:
Exif ["Software"] = ASCIIToString (property. Value );
Break;
Case 0x0132:
Exif ["DateTime"] = ASCIIToString (property. Value );
Break;
// GPS
Case 0x0002:
Exif ["GPSLatitude"] = string. Format ("{0} ° {1} '{2 }″",
RationalToSingle (property. Value, 0 ),
RationalToSingle (property. Value, 8 ),
RationalToSingle (property. Value, 16)
);
Break;
Case 0x0004:
Exif ["gpslongpolling"] = string. Format ("{0} ° {1} '{2 }″",
RationalToSingle (property. Value, 0 ),
RationalToSingle (property. Value, 8 ),
RationalToSingle (property. Value, 16)
);
Break;
Case 0x0006:
Exif ["GPSAltitude"] = RationalToSingle (property. Value, 0 );
Break;
}
}
Foreach (KeyValuePair <string, string> kvp in exif)
Console. WriteLine (kvp. Key + ":" + kvp. Value );

Console. ReadKey (true );
}

// Method
Static string ByteToString (byte [] B, int startindex)
{
If (startindex + 1 <= B. Length)
Return (char) B [startindex]). ToString ();
Else
Return string. Empty;
}
Static string comment tostring (byte [] B, int startindex)
{
If (startindex + 2 <= B. Length)
Return BitConverter. ToInt16 (B, startindex). ToString ();
Else
Return string. Empty;
}
Static string RationalToSingle (byte [] B, int startindex)
{
If (startindex + 8 <= B. Length)
Return (BitConverter. ToSingle (B, startindex)/BitConverter. ToSingle (B, startindex + 4). ToString ();
Else
Return string. Empty;
}
Static string ASCIIToString (byte [] B)
{
Return Encoding. ASCII. GetString (B );
}
}

}

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.