用C#读取图片的EXIF信息的方法(转)

引言:

EXIF,是英文Exchangeable Image File(可交换图像文件)的缩写,最初由日本电子工业发展协会(JEIDA --Japan Electronic Industry Development Association) 制订,目前的版本是修改发表于1998年6月的2.1版。国际标准化组织(ISO)正在制订的相机文件设计标准(DCF -- Design role for Camera File system)就是以EXIF2.1格式为基础而设定的。记住,EXIF是一种图像文件格式,只是文件的后缀名还是沿用大家熟悉的jpg而已。实际上,EXIF信息就是由数码相机在拍摄过程中采集一系列的信息,然后把信息放置在我们熟知的jpg文件的头部,也就是说EXIF信息是镶嵌在JPEG图像文件格式内的一组拍摄参数,主要包括摄影时的光圈、快门、ISO、日期时间等各种与当时摄影条件相关的讯息,相机品牌型号,色彩编码,拍摄时录制的声音以及全球定位系统(GPS)等信息。简单的说,它就好像是傻瓜相机的日期打印功能一样,只不过EXIF信息所记录的资讯更为详尽和完备。不过,具有EXIF信息的JPEG图像文件要比普通的JPEG文件略大一点。就目前市场而言,新一代的数码相机都具有附加EXIF信息功能,大多数的数码相机厂商也都会随数码相机发售时附赠能够读取EXIF信息的软件,例如 Nikon 系列的数码相机就附赠 NikonView 软件,Agfa系列的相机则附赠 Photowize V1.8版,而富士相机附送的EXIF viewer软件更是这方面的领军人物(目前已在很多网站提供免费下载。还有一部分的数码相机会自动将EXIF信息转存成文档文件,例如:NIKON CoolPix 990和SONY FD系列。除了硬件厂商随数码相机附带的EXIF信息查看软件,很多专业的图像软件厂商在这方面也不甘示弱,相继推出自己公司看图软件的最新版来支持这种近乎完美的JPEG-EXIF图像信息附加技术,如最近刚推出的ACDSee 4.0版本,就对现在流行的各种数码相机有相当好的支持,在EXIF图像信息附加方面较之其3.0版本也有很大的进步。不管是硬件厂商的配套软件还是专业名门的看图工具,所有这些软件都是为了方便数码摄影者能更方便地保存查看摄影图像的重要信息。我们将这些读取EXIF信息的软件归纳后分为四类:专业EXIF信息查看工具(以富士的EXIF viewer为例) 、具有查看EXIF信息的强大通用看图工具(以ACDSee为例)、支持EXIF信息查看的操作系统(微软的Windows XP)以及可以修改EXIF信息的另类工具(EXIF Editer),而我们这里要讲的是通过C#在WEB上读取一个图片的EXIF信息。

二、相关类(GetEXIFMetaData):

因为代码比较长,有五百多行,我只能将关键代码的片段拿出来讲讲,这个类中构造了两个结构MetadataDetail 和 Metadata  ,前者是为了存储EXIF中某元素信息的三种格式,起到中间转化的作用,一个是十六进制的索引信息,一个是没用处理过的信息代码,一个是显示信息。后面的那个结构就是存储一张图片所有EXIF信息元素的。 有两个方法LookupEXIFValue 和 GetEXIFMetaData ,前一个方法是处理特殊元素的对应显示的,后者从图片中读取相关信息然后填充到MetaData结构中。

using System; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Collections; 
using System.ComponentModel; 
 
namespace Test.Image 

    /// <summary> 
    /// 功能:获得图片EXIF信息 
    /// 作者:Rexsp 
    /// 创建日期:2004-03-20 
    /// 作者MSN:yubo@x263.net 
    /// </summary> 
    public class EXIFMetaData 
    { 
        #region 构造函数 
        /// <summary> 
        /// 构造函数 
        /// </summary> 
        public EXIFMetaData() 
        { 
        } 
        #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;// added f值,光圈数 
            public MetadataDetail ExposureProg;// added 曝光程序 
            public MetadataDetail SpectralSense;// added  
            public MetadataDetail ISOSpeed;// added ISO感光度 
            public MetadataDetail OECF;// added  
            public MetadataDetail Ver;// added EXIF版本 
            public MetadataDetail CompConfig;// added 色彩设置 
            public MetadataDetail CompBPP;// added 压缩比率 
            public MetadataDetail Aperture;// added 光圈值 
            public MetadataDetail Brightness;// added 亮度值Ev 
            public MetadataDetail ExposureBias;// added 曝光补偿 
            public MetadataDetail MaxAperture;// added 最大光圈值 
 
            public MetadataDetail SubjectDist;// added主体距离 
            public MetadataDetail LightSource;// added 白平衡 
            public MetadataDetail FocalLength;// added 焦距 
            public MetadataDetail FPXVer;// added FlashPix版本 
            public MetadataDetail ColorSpace;// added 色彩空间 
            public MetadataDetail Interop;// added  
            public MetadataDetail FlashEnergy;// added  
            public MetadataDetail SpatialFR;// added  
            public MetadataDetail FocalXRes;// added  
            public MetadataDetail FocalYRes;// added  
            public MetadataDetail FocalResUnit;// added  
            public MetadataDetail ExposureIndex;// added 曝光指数 
            public MetadataDetail SensingMethod;// added 感应方式 
            public MetadataDetail SceneType;// added  
            public MetadataDetail CfaPattern;// added  
        } 
        #endregion 
 
        #region 查找EXIF元素值 
        public string LookupEXIFValue(string Description, string Value) 
        { 
            string DescriptionValue = null; 
 
            switch(Description) 
            { 
                case "MeteringMode": 
 
                    #region  MeteringMode 
                { 
                    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; 
                    } 
                } 
                    #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; 
                case "Flash": 
 
                    #region Flash 
                { 
                    switch(Value) 
                    { 
                        case "0": 
                            DescriptionValue = "未使用";break; 
                        case "1": 
                            DescriptionValue = "闪光";break; 
                        case "5": 
                            DescriptionValue = "Flash fired but strobe return light not detected";break; 
                        case "7": 
                            DescriptionValue = "Flash fired and strobe return light detected";break; 
                    } 
                } 
                    #endregion 
 
                    break; 
                case "ExposureProg": 
 
                    #region ExposureProg 
                { 
                    switch(Value) 
                    { 
                        case "0": 
                            DescriptionValue = "没有定义";break; 
                        case "1": 
                            DescriptionValue = "手动控制";break; 
                        case "2": 
                            DescriptionValue = "程序控制";break; 
                        case "3": 
                            DescriptionValue = "光圈优先";break; 
                        case "4": 
                            DescriptionValue = "快门优先";break; 
                        case "5": 
                            DescriptionValue = "夜景模式";break; 
                        case "6": 
                            DescriptionValue = "运动模式";break; 
                        case "7": 
                            DescriptionValue = "肖像模式";break; 
                        case "8": 
                            DescriptionValue = "风景模式";break; 
                        case "9": 
                            DescriptionValue = "保留的";break; 
                    } 
                } 
 
                    #endregion 
 
                    break; 
                case "CompConfig": 
 
                    #region CompConfig 
                { 
                    switch(Value) 
                    { 
                     
                        case "513": 
                            DescriptionValue = "YCbCr";break; 
                    } 
                } 
                    #endregion 
 
                    break; 
                case "Aperture": 
 
                    #region Aperture 
                    DescriptionValue = Value; 
                    #endregion 
 
                    break; 
                case "LightSource": 
 
                    #region LightSource 
                { 
                    switch(Value) 
                    { 
                        case "0": 
                            DescriptionValue = "未知";break; 
                        case "1": 
                            DescriptionValue = "日光";break; 
                        case "2": 
                            DescriptionValue = "荧光灯";break; 
                        case "3": 
                            DescriptionValue = "白炽灯";break; 
                        case "10": 
                            DescriptionValue = "闪光灯";break; 
                        case "17": 
                            DescriptionValue = "标准光A";break; 
                        case "18": 
                            DescriptionValue = "标准光B";break; 
                        case "19": 
                            DescriptionValue = "标准光C";break; 
                        case "20": 
                            DescriptionValue = "标准光D55";break; 
                        case "21": 
                            DescriptionValue = "标准光D65";break; 
                        case "22": 
                            DescriptionValue = "标准光D75";break; 
                        case "255": 
                            DescriptionValue = "其它";break; 
                    } 
                } 
 
 
                    #endregion 
                    break; 
 
            } 
            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"; //added  
            MyMetadata.ExposureProg.Hex = ""; //added  
            MyMetadata.SpectralSense.Hex = "8824"; //added  
            MyMetadata.ISOSpeed.Hex = "8827"; //added  
            MyMetadata.OECF.Hex = "8828"; //added  
            MyMetadata.Ver.Hex = "9000"; //added  
            MyMetadata.CompConfig.Hex = "9101"; //added  
            MyMetadata.CompBPP.Hex = "9102"; //added  
            MyMetadata.Aperture.Hex = "9202"; //added  
            MyMetadata.Brightness.Hex = "9203"; //added  
            MyMetadata.ExposureBias.Hex = "9204"; //added  
            MyMetadata.MaxAperture.Hex = "9205"; //added  
            MyMetadata.SubjectDist.Hex = "9206"; //added  
            MyMetadata.LightSource.Hex = "9208"; //added  
            MyMetadata.FocalLength.Hex = "920a"; //added  
            MyMetadata.FPXVer.Hex = "a000"; //added  
            MyMetadata.ColorSpace.Hex = "a001"; //added  
            MyMetadata.FocalXRes.Hex = "a20e"; //added  
            MyMetadata.FocalYRes.Hex = "a20f"; //added  
            MyMetadata.FocalResUnit.Hex = "a210"; //added  
            MyMetadata.ExposureIndex.Hex = "a215"; //added  
            MyMetadata.SensingMethod.Hex = "a217"; //added  
            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; 
                        } 
 
                        case "9207": 
                        { 
                            MyMetadata.MeteringMode.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.MeteringMode.DisplayValue = LookupEXIFValue("MeteringMode",BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString()); 
                            break; 
                        } 
 
                        case "9209": 
                        { 
                            MyMetadata.Flash.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.Flash.DisplayValue = LookupEXIFValue("Flash",BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString()); 
                            break; 
                        } 
 
                        case "829a": 
                        { 
                            MyMetadata.ExposureTime.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            string StringValue = ""; 
                            for (int Offset = 0; Offset < MyImage.GetPropertyItem (MyPropertyId).Len; Offset = Offset + 4)
                            { 
                                StringValue += BitConverter.ToInt32(MyImage.GetPropertyItem(MyPropertyId).Value,Offset).ToString() + "/"; 
                            } 
                            MyMetadata.ExposureTime.DisplayValue =StringValue.Substring(0,StringValue.Length-1); 
                            break; 
                        } 
                        case "829d": 
                        { 
                            MyMetadata.Fstop.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            int int1; 
                            int int2; 
                            int1 =BitConverter.ToInt32(MyImage.GetPropertyItem(MyPropertyId).Value,0); 
                            int2 = BitConverter.ToInt32(MyImage.GetPropertyItem(MyPropertyId).Value,4); 
                            MyMetadata.Fstop.DisplayValue = "F/" +(int1/int2); 
 
                            MyMetadata.FNumber.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.FNumber.DisplayValue = BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString(); 
 
                            break; 
                        } 
                        case "9201": 
                        { 
                            MyMetadata.ShutterSpeed.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            string StringValue = BitConverter.ToInt32(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString(); 
                            MyMetadata.ShutterSpeed.DisplayValue = "1/" + StringValue; 
                            break; 
                        } 
 
                        case "8822": 
                        { 
                            MyMetadata.ExposureProg.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.ExposureProg.DisplayValue = LookupEXIFValue("ExposureProg",BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString()); 
                            break; 
                        } 
 
                        case "8824": 
                        { 
                            MyMetadata.SpectralSense.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.SpectralSense.DisplayValue =Value.GetString(MyPropertyItemList[index].Value); 
                            break; 
                        } 
                        case "8827": 
                        { 
                            hexVal = ""; 
                            MyMetadata.ISOSpeed.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            hexVal = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value).Substring(0,2); 
                            MyMetadata.ISOSpeed.DisplayValue = Convert.ToInt32(hexVal,16).ToString();//Value.GetString(MyPropertyItemList[index].Value); 
                            break; 
                        } 
 
                        case "8828": 
                        { 
                            MyMetadata.OECF.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.OECF.DisplayValue =Value.GetString(MyPropertyItemList[index].Value); 
                            break; 
                        } 
 
                        case "9000": 
                        { 
                            MyMetadata.Ver.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.Ver.DisplayValue =Value.GetString(MyPropertyItemList[index].Value).Substring(1,1)+"."+Value.GetString(MyPropertyItemList[index].Value).Substring(2,2); 
                            break; 
                        } 
 
                        case "9101": 
                        { 
                            MyMetadata.CompConfig.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.CompConfig.DisplayValue = LookupEXIFValue("CompConfig",BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString()); 
                            break; 
                        } 
 
                        case "9102": 
                        { 
                            MyMetadata.CompBPP.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.CompBPP.DisplayValue = BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString(); 
                            break; 
                        } 
 
                        case "9202": 
                        { 
                            hexVal = ""; 
                            MyMetadata.Aperture.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            hexVal = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value).Substring(0,2); 
                            hexVal = Convert.ToInt32(hexVal,16).ToString(); 
                            hexVal = hexVal + "00"; 
                            MyMetadata.Aperture.DisplayValue = hexVal.Substring(0,1)+"."+hexVal.Substring(1,2); 
                            break; 
                        } 
 
                        case "9203": 
                        { 
                            hexVal = ""; 
                            MyMetadata.Brightness.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            hexVal = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value).Substring(0,2); 
                            hexVal = Convert.ToInt32(hexVal,16).ToString(); 
                            hexVal = hexVal + "00"; 
                            MyMetadata.Brightness.DisplayValue = hexVal.Substring(0,1)+"."+hexVal.Substring(1,2); 
                            break; 
                        } 
 
                        case "9204": 
                        { 
                            MyMetadata.ExposureBias.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.ExposureBias.DisplayValue = BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString(); 
                            break; 
                        } 
 
                        case "9205": 
                        { 
                            hexVal = ""; 
                            MyMetadata.MaxAperture.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            hexVal = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value).Substring(0,2); 
                            hexVal = Convert.ToInt32(hexVal,16).ToString(); 
                            hexVal = hexVal + "00"; 
                            MyMetadata.MaxAperture.DisplayValue = hexVal.Substring(0,1)+"."+hexVal.Substring(1,2); 
                            break; 
                        } 
 
                        case "9206": 
                        { 
                            MyMetadata.SubjectDist.RawValueAsString =BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.SubjectDist.DisplayValue =Value.GetString(MyPropertyItemList[index].Value); 
                            break; 
                        } 
 
                        case "9208": 
                        { 
                            MyMetadata.LightSource.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.LightSource.DisplayValue = LookupEXIFValue("LightSource",BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString()); 
                            break; 
                        } 
 
                        case "920a": 
                        { 
                            hexVal = ""; 
                            MyMetadata.FocalLength.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            hexVal = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value).Substring(0,2); 
                            hexVal = Convert.ToInt32(hexVal,16).ToString(); 
                            hexVal = hexVal + "00"; 
                            MyMetadata.FocalLength.DisplayValue = hexVal.Substring(0,1)+"."+hexVal.Substring(1,2); 
                            break; 
                        } 
 
                        case "a000": 
                        { 
                            MyMetadata.FPXVer.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.FPXVer.DisplayValue = Value.GetString(MyPropertyItemList[index].Value).Substring(1,1)+"."+Value.GetString(MyPropertyItemList[index].Value).Substring(2,2); 
                            break; 
                        } 
 
                        case "a001": 
                        { 
                            MyMetadata.ColorSpace.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            if (BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString() == "1") 
                                MyMetadata.ColorSpace.DisplayValue = "RGB"; 
                            if (BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString() == "65535") 
                                MyMetadata.ColorSpace.DisplayValue = "Uncalibrated"; 
                            break; 
                        } 
 
                        case "a20e": 
                        { 
                            MyMetadata.FocalXRes.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.FocalXRes.DisplayValue =BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString(); 
                            break; 
                        } 
 
                        case "a20f": 
                        { 
                            MyMetadata.FocalYRes.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.FocalYRes.DisplayValue = BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString(); 
                            break; 
                        } 
 
                        case "a210": 
                        { 
                            string aa; 
                            MyMetadata.FocalResUnit.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            aa = BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString();; 
                            if (aa == "1") MyMetadata.FocalResUnit.DisplayValue = "没有单位"; 
                            if (aa == "2") MyMetadata.FocalResUnit.DisplayValue = "英尺"; 
                            if (aa == "3") MyMetadata.FocalResUnit.DisplayValue = "厘米"; 
                            break; 
                        } 
 
                        case "a215": 
                        { 
                            MyMetadata.ExposureIndex.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.ExposureIndex.DisplayValue = Value.GetString(MyPropertyItemList[index].Value); 
                            break; 
                        } 
 
                        case "a217": 
                        { 
                            string aa; 
                            aa = BitConverter.ToInt16(MyImage.GetPropertyItem(MyPropertyId).Value,0).ToString(); 
                            MyMetadata.SensingMethod.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            if (aa == "2") MyMetadata.SensingMethod.DisplayValue = "1 chip color area sensor"; 
                            break; 
                        } 
 
                        case "a301": 
                        { 
                            MyMetadata.SceneType.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.SceneType.DisplayValue = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            break; 
                        } 
 
                        case "a302": 
                        { 
                            MyMetadata.CfaPattern.RawValueAsString = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            MyMetadata.CfaPattern.DisplayValue = BitConverter.ToString(MyImage.GetPropertyItem(MyPropertyId).Value); 
                            break; 
                        } 
 
 
 
                    } 
                    #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; 
原文地址:https://www.cnblogs.com/jinmingjie/p/2612148.html