- ·上一篇文章:C#语言简介
- ·下一篇文章:关于C#中DateTime的使用
用C#读取图片的EXIF信息的方法
iptionValue = "Spot";break;
case "4":
DescriptionValue = "Multi-spot";break;
case "5":
DescriptionValue = "Multi-segment";break;
case "6":
DescriptionValue = "Partial";break;
case "255":
DescriptionValue = "Other";break;
}
}
#endregion
break;
case "ResolutionUnit":
#region ResolutionUnit
{
switch(Value)
{
case "1":
DescriptionValue = "No Units";break;
case "2":
DescriptionValue = "Inch";break;
case "3":
DescriptionValue = "Centimeter";break;
}
}
#endregion
break;
//省略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行相似代码
}
#endregion
index++;
}
}
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
}
}
!
case "4":
DescriptionValue = "Multi-spot";break;
case "5":
DescriptionValue = "Multi-segment";break;
case "6":
DescriptionValue = "Partial";break;
case "255":
DescriptionValue = "Other";break;
}
}
#endregion
break;
case "ResolutionUnit":
#region ResolutionUnit
{
switch(Value)
{
case "1":
DescriptionValue = "No Units";break;
case "2":
DescriptionValue = "Inch";break;
case "3":
DescriptionValue = "Centimeter";break;
}
}
#endregion
break;
//省略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行相似代码
}
#endregion
index++;
}
}
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;
!
用C#读取图片的EXIF信息的方法