Use C # To Read The EXIF information of a photo (jpg image,

Source: Internet
Author: User

Use C # To Read The EXIF information of a photo (jpg image,

Introduction: EXIF, which is an English Exchangeable Image File
{
}
# Endregion # region data conversion structure
/// Summary>
/// Convert the Data Structure
/// Summary>
Public struct MetadataDetail
{
Public string Hex; // a hexadecimal string
Public string RawValueAsString; // Original Value string
Public string DisplayValue; // display value string
}
# Endregion # region EXIF element structure
/// Summary>
/// Structure: stores EXIF Element Information
/// Summary>
Public struct Metadata
{
Public MetadataDetail EquipmentMake;
Public MetadataDetail CameraModel;
Public MetadataDetail ExposureTime; // specifies the exposure time.
Public MetadataDetail Fstop;
Public MetadataDetail DatePictureTaken;
Public MetadataDetail ShutterSpeed; // shutter speed
Public MetadataDetail MeteringMode; // exposure mode
Public MetadataDetail Flash; // flashlight
Public MetadataDetail XResolution;
Public MetadataDetail YResolution;
Public MetadataDetail ImageWidth; // photo width
Public MetadataDetail ImageHeight; // photo height public MetadataDetail FNumber; // F value, aperture count
Public MetadataDetail ExposureProg; // exposure Program
Public MetadataDetail SpectralSense ;//
Public MetadataDetail ISOSpeed; // ISO sensitivity
Public MetadataDetail OECF ;//
Public MetadataDetail Ver; // EXIF version
Public MetadataDetail CompConfig; // color settings
Public MetadataDetail CompBPP; // compression ratio
Public MetadataDetail Aperture; // Aperture Value
Public MetadataDetail Brightness; // Brightness Value Ev
Public MetadataDetail ExposureBias; // Exposure Compensation
Public MetadataDetail MaxAperture; // maximum aperture value public MetadataDetail SubjectDist; // subject distance
Public MetadataDetail LightSource; // White Balance
Public MetadataDetail FocalLength; // focal length
Public MetadataDetail FPXVer; // FlashPix version
Public MetadataDetail ColorSpace; // Color Space
Public MetadataDetail Interop ;//
Public MetadataDetail FlashEnergy ;//
Public MetadataDetail SpatialFR ;//
Public MetadataDetail FocalXRes ;//
Public MetadataDetail FocalYRes ;//
Public MetadataDetail FocalResUnit ;//
Public MetadataDetail ExposureIndex; // exposure index
Public MetadataDetail SensingMethod; // Sensing Method
Public MetadataDetail SceneType ;//
Public MetadataDetail CfaPattern ;//
}
# Endregion # region Query the EXIF element value
Public string LookupEXIFValue (string Description, string Value)
{
String DescriptionValue = null; switch (Description)
{
Case "MeteringMode": # regionMeteringMode
{
Switch (Value)
{
Case "0 ":
DescriptionValue = "Unknown"; break;
Case "1 ":
DescriptionValue = "Average"; break;
Case "2 ":
DescriptionValue = "Center Weighted Average"; break;
Case "3 ":
DescriptionValue = "Spot"; break;
Case "4 ":
DescriptionValue = "Multi-spot"; break;
Case "5 ":
DescriptionValue = "Multi-segment"; break;
Case "6 ":
DescriptionValue = "Partial"; break;
Case "255 ":
DescriptionValue = "Other"; break;
}
}
# Endregionbreak;
Case "ResolutionUnit": # region ResolutionUnit
{
Switch (Value)
{
Case "1 ":
DescriptionValue = "No Units"; break;
Case "2 ":
DescriptionValue = "Inch"; break;
Case "3 ":
DescriptionValue = "Centimeter"; break;
}
} # Endregionbreak;
// Omitting N lines of similar code}
Return DescriptionValue;
}
# Endregion # region obtains the EXIF information of an image.
Public Metadata GetEXIFMetaData (string PhotoName)
{
// Create an image instance
System. Drawing. Image MyImage = System. Drawing. Image. FromFile (PhotoName );
// Create an integer array to store the ID of the attribute array in the image
Int [] MyPropertyIdList = MyImage. PropertyIdList;
// Create an instance of a closed image attribute array
PropertyItem [] MyPropertyItemList = new PropertyItem [MyPropertyIdList. Length];
// Create an instance structure object with image EXIT information, and assign the initial value # region to create an instance structure object with image EXIT information, and assign the Initial Value
Metadata MyMetadata = new Metadata ();
MyMetadata. EquipmentMake. Hex = "10f ";
MyMetadata. CameraModel. Hex = "110 ";
MyMetadata. DatePictureTaken. Hex = "9003 ";
MyMetadata. ExposureTime. Hex = "829a ";
MyMetadata. Fstop. Hex = "829d ";
MyMetadata. ShutterSpeed. Hex = "9201 ";
MyMetadata. MeteringMode. Hex = "9207 ";
MyMetadata. Flash. Hex = "9209 ";
MyMetadata. FNumber. Hex = "829d ";//
MyMetadata. ExposureProg. Hex = "";//
MyMetadata. SpectralSense. Hex = "8824 ";//
MyMetadata. ISOSpeed. Hex = "8827 ";//
MyMetadata. OECF. hex= "8828 ";//
MyMetadata. Ver. Hex = "9000 ";//
MyMetadata. CompConfig. hex= "9101 ";//
MyMetadata. CompBPP. hex= "9102 ";//
MyMetadata. Aperture. Hex = "9202 ";//
MyMetadata. Brightness. Hex = "9203 ";//
MyMetadata. ExposureBias. Hex = "9204 ";//
MyMetadata. MaxAperture. Hex = "9205 ";//
MyMetadata. SubjectDist. Hex = "9206 ";//
MyMetadata. LightSource. hex= "9208 ";//
MyMetadata. FocalLength. Hex = "920a ";//
MyMetadata. FPXVer. Hex = "a000 ";//
MyMetadata. ColorSpace. Hex = "a001 ";//
MyMetadata. FocalXRes. Hex = "a20e ";//
MyMetadata. FocalYRes. Hex = "a20f ";//
MyMetadata. FocalResUnit. Hex = "a210 ";//
MyMetadata. ExposureIndex. Hex = "a215 ";//
MyMetadata. SensingMethod. Hex = "a217 ";//
MyMetadata. SceneType. Hex = "a301 ";
MyMetadata. CfaPattern. Hex = "a302 ";
# Endregion // ASCII code
System. Text. ASCIIEncoding Value = new System. Text. ASCIIEncoding (); int index = 0;
Int MyPropertyIdListCount = MyPropertyIdList. Length;
If (MyPropertyIdListCount! = 0)
{
Foreach (int MyPropertyId in MyPropertyIdList)
{
String hexVal = "";
MyPropertyItemList [index] = MyImage. GetPropertyItem (MyPropertyId); # region initializes attribute values
String myPropertyIdString = MyImage. GetPropertyItem (MyPropertyId). Id. ToString ("x ");
Switch (myPropertyIdString)
{
Case "10f ":
{
MyMetadata. EquipmentMake. RawValueAsString = BitConverter. ToString (MyImage. GetPropertyItem (MyPropertyId). Value );
MyMetadata. EquipmentMake. DisplayValue = Value. GetString (MyPropertyItemList [index]. Value );
Break;
} Case "110 ":
{
MyMetadata. CameraModel. RawValueAsString = BitConverter. ToString (MyImage. GetPropertyItem (MyPropertyId). Value );
MyMetadata. CameraModel. DisplayValue = Value. GetString (MyPropertyItemList [index]. Value );
Break;} case "9003 ":
{
MyMetadata. DatePictureTaken. RawValueAsString = BitConverter. ToString (MyImage. GetPropertyItem (MyPropertyId). Value );
MyMetadata. DatePictureTaken. DisplayValue = Value. GetString (MyPropertyItemList [index]. Value );
Break;
}
// Omitting N lines of similar code
}
# Endregionindex ++;
}
} MyMetadata. XResolution. DisplayValue = MyImage. HorizontalResolution. ToString ();
MyMetadata. YResolution. DisplayValue = MyImage. VerticalResolution. ToString ();
MyMetadata. ImageHeight. DisplayValue = MyImage. Height. ToString ();
MyMetadata. ImageWidth. DisplayValue = MyImage. Width. ToString ();
MyImage. Dispose ();
Return MyMetadata;
}
# Endregion
}
} Then there is a call question. With this class, how can I read The EXIF information of the image? The Code is as follows:
EXIFMetaData em = new EXIFMetaData ();
String filePath = Server. MapPath ("Test.jpg"); // You can dynamically transfer
EXIFMetaData. Metadata m = em. GetEXIFMetaData (filePath); // This is called. Upload the absolute path of the image.
String exif = m. Ver. DisplayValue;
String camera = m. CameraModel. DisplayValue;
String model = m. CameraModel. DisplayValue;
String aperture = m. Aperture. DisplayValue;
String shutter = m. ShutterSpeed. DisplayValue;
String sensitive = m. ExposureIndex. DisplayValue; do you have any questions? Please refer to it. This code has been tested and works properly.

Reprinted from: http://www.aspnetjia.com

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.