引言:EXIF,是英文Exchangeable Image File{}#endregion#region 數據轉換結構/// summary>/// 轉換數據結構/// /summary>public struct MetadataDetail{public string Hex;//十六進位字元
引言:EXIF,是英文Exchangeable Image File
{
}
#endregion#region 數據轉換結構
/// summary>
/// 轉換數據結構
/// /summary>
public struct MetadataDetail
{
public string Hex;//十六進位字元串
public string RawValueAsString;//原始值串
public string DisplayValue;//顯示值串
}
#endregion#region EXIF元素結構
/// summary>
/// 結構:存儲EXIF元素信息
/// /summary>
public struct Metadata
{
public MetadataDetail EquipmentMake;
public MetadataDetail CameraModel;
public MetadataDetail ExposureTime;//曝光時間
public MetadataDetail Fstop;
public MetadataDetail DatePictureTaken;
public MetadataDetail ShutterSpeed;// 快門速度
public MetadataDetail MeteringMode;//曝光模式
public MetadataDetail Flash;//閃光燈
public MetadataDetail XResolution;
public MetadataDetail YResolution;
public MetadataDetail ImageWidth;//照片寬度
public MetadataDetail ImageHeight;//照片高度public MetadataDetail FNumber;//f值,光圈數
public MetadataDetail ExposureProg;//曝光程式
public MetadataDetail SpectralSense;//
public MetadataDetail ISOSpeed;//ISO感光度
public MetadataDetail OECF;//
public MetadataDetail Ver;//EXIF版本
public MetadataDetail CompConfig;//色彩設置
public MetadataDetail CompBPP;//壓縮比率
public MetadataDetail Aperture;//光圈值
public MetadataDetail Brightness;//亮度值Ev
public MetadataDetail ExposureBias;//曝光補償
public MetadataDetail MaxAperture;//最大光圈值public MetadataDetail SubjectDist;// 主體距離
public MetadataDetail LightSource;//白平衡
public MetadataDetail FocalLength;//焦距
public MetadataDetail FPXVer;//FlashPix版本
public MetadataDetail ColorSpace;//色彩空間
public MetadataDetail Interop;//
public MetadataDetail FlashEnergy;//
public MetadataDetail SpatialFR;//
public MetadataDetail FocalXRes;//
public MetadataDetail FocalYRes;//
public MetadataDetail FocalResUnit;//
public MetadataDetail ExposureIndex;//曝光指數
public MetadataDetail SensingMethod;//感應方式
public MetadataDetail SceneType;//
public MetadataDetail CfaPattern;//
}
#endregion#region 查找EXIF元素值
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;
//省略N行相似代碼}
return DescriptionValue;
}
#endregion#region 取得圖片的EXIF信息
public Metadata GetEXIFMetaData(string PhotoName)
{
// 創建一個圖片的實例
System.Drawing.Image MyImage = System.Drawing.Image.FromFile(PhotoName);
// 創建一個整型數組來存儲圖像中屬性數組的ID
int[] MyPropertyIdList = MyImage.PropertyIdList;
//創建一個封閉圖像屬性數組的實例
PropertyItem[] MyPropertyItemList = new PropertyItem[MyPropertyIdList.Length];
//創建一個圖像EXIT信息的實例結構對象,並且賦初值#region 創建一個圖像EXIT信息的實例結構對象,並且賦初值
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編碼
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 初始化各屬性值
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;
}
//省略N行相似代碼
}
#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
}
}然後就是個調用的問題,有了這個類,我如何讀取圖片的EXIF信息呢?代碼如下:
EXIFMetaData em = new EXIFMetaData();
string filePath=Server.MapPath("Test.jpg");//這裡可以動態傳遞圖片路徑的
EXIFMetaData.Metadata m = em.GetEXIFMetaData(filePath);//這裡就是調用,傳圖片絕對路徑
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;有什麼不明白的,歡迎提出來,此代碼經過測試,功能正常。