[转]数码照片的JPG文件高级信息(用C#读取图片的EXIF信息)

数码相机的照片很多,而且文件名也不直观,所以写了个程序批量改名,就是把文件名都改成相机的拍照日期,呵呵,谁知道拍照日期不是那么好取,在csdn里开了一帖,谢谢网友帮忙,终于搞定!把代码放在下面了,说不定谁会用得着。
原帖地址:http://community.csdn.net/Expert/topic/4295/4295730.xml?temp=.662182
原文地址:http://www.codeproject.com/csharp/EXIFextractor.asp

调用
string strFile="fffff.jpg";//文件名
System.Drawing.Bitmap bmp = new Bitmap(strFile);
EXIF.EXIFextractor er = new EXIF.EXIFextractor(ref bmp, "\n");
Console.Write(strFile + "\r\n");
Console.Write(er["DTDigitized"]);//拍照日期
foreach (EXIF.Pair pr in er)
{
Console.Write(pr.First + ":" + pr.Second + "\r\n");
}
//
///下面的两个文件处理JPG的EXIF信息

Code 1/**////EXIFextractor.cs2///
3usingSystem;4usingSystem.Text;5usingSystem.Collections;6usingSystem.Drawing.Imaging;7usingSystem.Reflection;8usingSystem.IO;910namespaceEXIF11{12/**////13///EXIFextractor Class14///15///
16publicclassEXIFextractor : IEnumerable17{18/**////19///Get the individual property value by supplying property name20///These are the valid property names :21///22///"Exif IFD"23///"Gps IFD"24///"New Subfile Type"25///"Subfile Type"26///"Image Width"27///"Image Height"28///"Bits Per Sample"29///"Compression"30///"Photometric Interp"31///"Thresh Holding"32///"Cell Width"33///"Cell Height"34///"Fill Order"35///"Document Name"36///"Image Description"37///"Equip Make"38///"Equip Model"39///"Strip Offsets"40///"Orientation"41///"Samples PerPixel"42///"Rows Per Strip"43///"Strip Bytes Count"44///"Min Sample Value"45///"Max Sample Value"46///"X Resolution"47///"Y Resolution"48///"Planar Config"49///"Page Name"50///"X Position"51///"Y Position"52///"Free Offset"53///"Free Byte Counts"54///"Gray Response Unit"55///"Gray Response Curve"56///"T4 Option"57///"T6 Option"58///"Resolution Unit"59///"Page Number"60///"Transfer Funcition"61///"Software Used"62///"Date Time"63///"Artist"64///"Host Computer"65///"Predictor"66///"White Point"67///"Primary Chromaticities"68///"ColorMap"69///"Halftone Hints"70///"Tile Width"71///"Tile Length"72///"Tile Offset"73///"Tile ByteCounts"74///"InkSet"75///"Ink Names"76///"Number Of Inks"77///"Dot Range"78///"Target Printer"79///"Extra Samples"80///"Sample Format"81///"S Min Sample Value"82///"S Max Sample Value"83///"Transfer Range"84///"JPEG Proc"85///"JPEG InterFormat"86///"JPEG InterLength"87///"JPEG RestartInterval"88///"JPEG LosslessPredictors"89///"JPEG PointTransforms"90///"JPEG QTables"91///"JPEG DCTables"92///"JPEG ACTables"93///"YCbCr Coefficients"94///"YCbCr Subsampling"95///"YCbCr Positioning"96///"REF Black White"97///"ICC Profile"98///"Gamma"99///"ICC Profile Descriptor"100///"SRGB RenderingIntent"101///"Image Title"102///"Copyright"103///"Resolution X Unit"104///"Resolution Y Unit"105///"Resolution X LengthUnit"106///"Resolution Y LengthUnit"107///"Print Flags"108///"Print Flags Version"109///"Print Flags Crop"110///"Print Flags Bleed Width"111///"Print Flags Bleed Width Scale"112///"Halftone LPI"113///"Halftone LPIUnit"114///"Halftone Degree"115///"Halftone Shape"116///"Halftone Misc"117///"Halftone Screen"118///"JPEG Quality"119///"Grid Size"120///"Thumbnail Format"121///"Thumbnail Width"122///"Thumbnail Height"123///"Thumbnail ColorDepth"124///"Thumbnail Planes"125///"Thumbnail RawBytes"126///"Thumbnail Size"127///"Thumbnail CompressedSize"128///"Color Transfer Function"129///"Thumbnail Data"130///"Thumbnail ImageWidth"131///"Thumbnail ImageHeight"132///"Thumbnail BitsPerSample"133///"Thumbnail Compression"134///"Thumbnail PhotometricInterp"135///"Thumbnail ImageDescription"136///"Thumbnail EquipMake"137///"Thumbnail EquipModel"138///"Thumbnail StripOffsets"139///"Thumbnail Orientation"140///"Thumbnail SamplesPerPixel"141///"Thumbnail RowsPerStrip"142///"Thumbnail StripBytesCount"143///"Thumbnail ResolutionX"144///"Thumbnail ResolutionY"145///"Thumbnail PlanarConfig"146///"Thumbnail ResolutionUnit"147///"Thumbnail TransferFunction"148///"Thumbnail SoftwareUsed"149///"Thumbnail DateTime"150///"Thumbnail Artist"151///"Thumbnail WhitePoint"152///"Thumbnail PrimaryChromaticities"153///"Thumbnail YCbCrCoefficients"154///"Thumbnail YCbCrSubsampling"155///"Thumbnail YCbCrPositioning"156///"Thumbnail RefBlackWhite"157///"Thumbnail CopyRight"158///"Luminance Table"159///"Chrominance Table"160///"Frame Delay"161///"Loop Count"162///"Pixel Unit"163///"Pixel PerUnit X"164///"Pixel PerUnit Y"165///"Palette Histogram"166///"Exposure Time"167///"F-Number"168///"Exposure Prog"169///"Spectral Sense"170///"ISO Speed"171///"OECF"172///"Ver"173///"DTOrig"174///"DTDigitized"175///"CompConfig"176///"CompBPP"177///"Shutter Speed"178///"Aperture"179///"Brightness"180///"Exposure Bias"181///"MaxAperture"182///"SubjectDist"183///"Metering Mode"184///"LightSource"185///"Flash"186///"FocalLength"187///"Maker Note"188///"User Comment"189///"DTSubsec"190///"DTOrigSS"191///"DTDigSS"192///"FPXVer"193///"ColorSpace"194///"PixXDim"195///"PixYDim"196///"RelatedWav"197///"Interop"198///"FlashEnergy"199///"SpatialFR"200///"FocalXRes"201///"FocalYRes"202///"FocalResUnit"203///"Subject Loc"204///"Exposure Index"205///"Sensing Method"206///"FileSource"207///"SceneType"208///"CfaPattern"209///"Gps Ver"210///"Gps LatitudeRef"211///"Gps Latitude"212///"Gps LongitudeRef"213///"Gps Longitude"214///"Gps AltitudeRef"215///"Gps Altitude"216///"Gps GpsTime"217///"Gps GpsSatellites"218///"Gps GpsStatus"219///"Gps GpsMeasureMode"220///"Gps GpsDop"221///"Gps SpeedRef"222///"Gps Speed"223///"Gps TrackRef"224///"Gps Track"225///"Gps ImgDirRef"226///"Gps ImgDir"227///"Gps MapDatum"228///"Gps DestLatRef"229///"Gps DestLat"230///"Gps DestLongRef"231///"Gps DestLong"232///"Gps DestBearRef"233///"Gps DestBear"234///"Gps DestDistRef"235///"Gps DestDist"236///
237publicobjectthis[stringindex]238{239get240{241returnproperties[index];242}243}244//245privateSystem.Drawing.Bitmap bmp;246//247privatestringdata;248//249privatetranslation myHash;250//251privateHashtable properties;252//253internalintCount254{255get256{257returnthis.properties.Count;258}259}260//261stringsp;262/**////263///264///265///266///267///268///
269publicvoidsetTag(intid,stringdata)270{271Encoding ascii=Encoding.ASCII;272this.setTag(id, data.Length,0x2, ascii.GetBytes(data));273}274/**////275///276///277///278///279///280///
281publicvoidsetTag(intid,intlen,shorttype,byte[] data)282{283PropertyItem p=CreatePropertyItem(type, id, len, data);284this.bmp.SetPropertyItem(p);285buildDB(this.bmp.PropertyItems);286}287/**////288///289///290///291///292///293///294///
295privatestaticPropertyItem CreatePropertyItem(shorttype,inttag,intlen,byte[] value)296{297PropertyItem item;298299//Loads a PropertyItem from a Jpeg image stored in the assembly as a resource.300Assembly assembly=Assembly.GetExecutingAssembly();301Stream emptyBitmapStream=assembly.GetManifestResourceStream("EXIFextractor.decoy.jpg");302System.Drawing.Image empty=System.Drawing.Image.FromStream(emptyBitmapStream);303304item=empty.PropertyItems[0];305306//Copies the data to the property item.307item.Type=type;308item.Len=len;309item.Id=tag;310item.Value=newbyte[value.Length];311value.CopyTo(item.Value,0);312313returnitem;314}315/**////316///317///318///319///
320publicEXIFextractor(refSystem.Drawing.Bitmap bmp,stringsp)321{322properties=newHashtable();323//324this.bmp=bmp;325this.sp=sp;326//327myHash=newtranslation();328buildDB(this.bmp.PropertyItems);329}330stringmsp="";331publicEXIFextractor(refSystem.Drawing.Bitmap bmp,stringsp,stringmsp)332{333properties=newHashtable();334this.sp=sp;335this.msp=msp;336this.bmp=bmp;337//338myHash=newtranslation();339this.buildDB(bmp.PropertyItems);340341}342publicstaticPropertyItem[] GetExifProperties(stringfileName)343{344FileStream stream=newFileStream(fileName, FileMode.Open, FileAccess.Read);345System.Drawing.Image image=System.Drawing.Image.FromStream(stream,346/**//*useEmbeddedColorManagement =*/true,347/**//*validateImageData =*/false);348returnimage.PropertyItems;349}350publicEXIFextractor(stringfile,stringsp,stringmsp)351{352properties=newHashtable();353this.sp=sp;354this.msp=msp;355356myHash=newtranslation();357//358this.buildDB(GetExifProperties(file));359360}361362/**////363///364///
365privatevoidbuildDB(System.Drawing.Imaging.PropertyItem[] parr)366{367properties.Clear();368//369data="";370//371Encoding ascii=Encoding.ASCII;372//373foreach(System.Drawing.Imaging.PropertyItem pinparr)374{375stringv="";376stringname=(string)myHash[p.Id];377//tag not found. skip it378if(name==null)continue;379//380data+=name+":";381//382//1 = BYTE An 8-bit unsigned integer.,383if(p.Type==0x1)384{385v=p.Value[0].ToString();386}387//2 = ASCII An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.,388elseif(p.Type==0x2)389{390//string391v=ascii.GetString(p.Value);392}393//3 = SHORT A 16-bit (2 -byte) unsigned integer,394elseif(p.Type==0x3)395{396//orientation//lookup table397switch(p.Id)398{399case0x8827://ISO400v="ISO-"+convertToInt16U(p.Value).ToString();401break;402case0xA217://sensing method403{404switch(convertToInt16U(p.Value))405{406case1: v="Not defined";break;407case2: v="One-chip color area sensor";break;408case3: v="Two-chip color area sensor";break;409case4: v="Three-chip color area sensor";break;410case5: v="Color sequential area sensor";break;411case7: v="Trilinear sensor";break;412case8: v="Color sequential linear sensor";break;413default: v="reserved";break;414}415}416break;417case0x8822://aperture418switch(convertToInt16U(p.Value))419{420case0: v="Not defined";break;421case1: v="Manual";break;422case2: v="Normal program";break;423case3: v="Aperture priority";break;424case4: v="Shutter priority";break;425case5: v="Creative program (biased toward depth of field)";break;426case6: v="Action program (biased toward fast shutter speed)";break;427case7: v="Portrait mode (for closeup photos with the background out of focus)";break;428case8: v="Landscape mode (for landscape photos with the background in focus)";break;429default: v="reserved";break;430}431break;432case0x9207://metering mode433switch(convertToInt16U(p.Value))434{435case0: v="unknown";break;436case1: v="Average";break;437case2: v="CenterWeightedAverage";break;438case3: v="Spot";break;439case4: v="MultiSpot";break;440case5: v="Pattern";break;441case6: v="Partial";break;442case255: v="Other";break;443default: v="reserved";break;444}445break;446case0x9208://light source447{448switch(convertToInt16U(p.Value))449{450case0: v="unknown";break;451case1: v="Daylight";break;452case2: v="Fluorescent";break;453case3: v="Tungsten";break;454case17: v="Standard light A";break;455case18: v="Standard light B";break;456case19: v="Standard light C";break;457case20: v="D55";break;458case21: v="D65";break;459case22: v="D75";break;460case255: v="other";break;461default: v="reserved";break;462}463}464break;465case0x9209:466{467switch(convertToInt16U(p.Value))468{469case0: v="Flash did not fire";break;470case1: v="Flash fired";break;471case5: v="Strobe return light not detected";break;472case7: v="Strobe return light detected";break;473default: v="reserved";break;474}475}476break;477default:478v=convertToInt16U(p.Value).ToString();479break;480}481}482//4 = LONG A 32-bit (4 -byte) unsigned integer,483elseif(p.Type==0x4)484{485//orientation//lookup table486v=convertToInt32U(p.Value).ToString();487}488//5 = RATIONAL Two LONGs. The first LONG is the numerator and the second LONG expresses the//denominator.,489elseif(p.Type==0x5)490{491//rational492byte[] n=newbyte[p.Len/2];493byte[] d=newbyte[p.Len/2];494Array.Copy(p.Value,0, n,0, p.Len/2);495Array.Copy(p.Value, p.Len/2, d,0, p.Len/2);496uinta=convertToInt32U(n);497uintb=convertToInt32U(d);498Rational r=newRational(a, b);499//500//convert here501//502switch(p.Id)503{504case0x9202://aperture505v="F/"+Math.Round(Math.Pow(Math.Sqrt(2), r.ToDouble()),2).ToString();506break;507case0x920A:508v=r.ToDouble().ToString();509break;510case0x829A:511v=r.ToDouble().ToString();512break;513case0x829D://F-number514v="F/"+r.ToDouble().ToString();515break;516default:517v=r.ToString("/");518break;519}520521}522//7 = UNDEFINED An 8-bit byte that can take any value depending on the field definition,523elseif(p.Type==0x7)524{525switch(p.Id)526{527case0xA300:528{529if(p.Value[0]==3)530{531v="DSC";532}533else534{535v="reserved";536}537break;538}539case0xA301:540if(p.Value[0]==1)541v="A directly photographed image";542else543v="Not a directly photographed image";544break;545default:546v="-";547break;548}549}550//9 = SLONG A 32-bit (4 -byte) signed integer (2's complement notation),551elseif(p.Type==0x9)552{553v=convertToInt32(p.Value).ToString();554}555//10 = SRATIONAL Two SLONGs. The first SLONG is the numerator and the second SLONG is the556//denominator.557elseif(p.Type==0xA)558{559560//rational561byte[] n=newbyte[p.Len/2];562byte[] d=newbyte[p.Len/2];563Array.Copy(p.Value,0, n,0, p.Len/2);564Array.Copy(p.Value, p.Len/2, d,0, p.Len/2);565inta=convertToInt32(n);566intb=convertToInt32(d);567Rational r=newRational(a, b);568//569//convert here570//571switch(p.Id)572{573case0x9201://shutter speed574v="1/"+Math.Round(Math.Pow(2, r.ToDouble()),2).ToString();575break;576case0x9203:577v=Math.Round(r.ToDouble(),4).ToString();578break;579default:580v=r.ToString("/");581break;582}583}584//add it to the list585if(properties[name]==null)586properties.Add(name, v);587//cat it too588data+=v;589data+=this.sp;590}591592}593594/**////595///596///597///
598publicoverridestringToString()599{600returndata;601}602/**////603///604///605///606///
607intconvertToInt32(byte[] arr)608{609if(arr.Length!=4)610return0;611else612returnarr[3];613614}

615

posted on 2009-01-17 10:37 Jrong 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Jrong/archive/2009/01/17/1377414.html

[转]数码照片的JPG文件高级信息(用C#读取图片的EXIF信息)相关推荐

  1. PHP如何读取照片的exif信息

    什么是Exif? Exif是一种图象文件格式,它的数据存储与JPEG格式是完全相同的.实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍摄时的光圈.快门.白平衡.ISO.焦距.日期时 ...

  2. php exif信息,php通过exif_read_data函数获取图片的exif信息

    php获取图片的exif信息,php自带一个exif_read_data函数可以用来读取图片的exif信息,代码来自php手册 echo "test1.jpg: \n"; $exi ...

  3. php exif信息,php通过exif_read_data函数获取图片的exif信息_php技巧

    php获取图片的exif信息,php自带一个exif_read_data函数可以用来读取图片的exif信息,代码来自php手册 \n"; $exif = exif_read_data('te ...

  4. php exif信息,php通过exif_read_data函数获取图片的exif信息 PHP

    php获取图片的exif信息,php自带一个exif_read_data函数可以用来读取图片的exif信息,代码来自php手册 echo 'test1.jpg: \n'; $exif = exif_r ...

  5. XML文件转TXT,XML无图片宽高信息

    XML文件转TXT,XML无图片宽高信息 网络上有很多xml转txt的文章,不过有的xml文件不包括size信息,即图片本身的宽高,如先前提到的湛江水下目标检测大赛数据集的label文件.因此在前辈的 ...

  6. MATLAB批量读取航摄相片EXIF信息和GNSS信息以及MATLAB批量经纬度坐标转换空间直角坐标

    前言 没有前言,创作不易,希望家人们点点赞,如有错误评论留言,感谢支持! 文章目录 前言 一.MATLAB批量读取航摄相片的EXIF信息,并批量提取其GNSS经纬度信息 (1)前述: (2)代码实现和 ...

  7. 通过Exiv2读取照片的Exif信息获取GPS,焦距等信息

    这里简单介绍一下如何通过开源C++工具包Exiv2读取图片中的属性信息 其实做这项工作之前也想偷懒百度一下的,大都是Java写的安卓的 ExifInterface 类,这和我的工作扯不上一点关系啊,最 ...

  8. 在Android下通过ExifInterface类操作图片的Exif信息

    什么是Exif 先来了解什么是Exif.Exif是一种图像文件格式,它的数据存储于JPEG格式是完全相同的,实际上Exif格式就是JPEG格式头插入了 数码照片的信息,包括拍摄的光圈.快门.平衡白.I ...

  9. C# 获取图片的EXIF 信息

    关于 EXIF 信息的介绍. 1  EXIF,是英文Exchangeable Image File(可交换图像文件)的缩写.EXIF是一种图像文件格式,只是文件的后缀名为jpg.EXIF信息是由数码相 ...

最新文章

  1. mxnet deepspeech网络结构打印
  2. Lync server 2013 之office web apps server 搭建步骤
  3. mysql数据库导出mdf文件_数据库 导出mdf
  4. Linux中autoduck批量对接,求助:关于autodock模拟分子对接
  5. 1-4-14:计算邮资
  6. ListView(2)
  7. 什么样的环境才是最理想的工作环境呢?
  8. Java8中的 Stream 那么彪悍,你知道它的原理是什么吗?
  9. 文件打包下载 (ZipArchive)
  10. 【百问网智能家居---基于单片机最小系统STM32F103C8T6_MINI的入门学习】
  11. edp和edt哪个好_不懂香水EDP和EDT?Dior真我系列完美诠释
  12. Parallels Desktop启动后黑屏无法进入Windows系统
  13. linux自带查看端口流量命令,iftop命令查看linux系统网卡流量的命令
  14. js获取视频长度的3种方法
  15. 独立产品灵感周刊 DecoHack #029 - 随便逛逛谷歌街景
  16. [论文阅读]Structure-from-Motion Revisited
  17. Parameter number 2 is not an OUT parameter 问题的解决方法
  18. 服务器接收 App Store 苹果商店内购项目IAP的退款通知
  19. Win11账号被锁定无法登录怎么办?Win11账号被锁定无法登录
  20. 互联网寒冬,7面阿里,终获Offer,定级P6+

热门文章

  1. 购物兔入驻百度开放平台,极度方便用户!
  2. 【爬虫】案例(爬取豆瓣top250)[完整+详细]
  3. RabbitMQ-topic主题模式
  4. kettle提示:ArrayIndexOutOfBoundsException
  5. 荣耀V10连点三次android版本,华为又要出新机,这次选择和奥迪合作
  6. 股市江湖-高手的层次
  7. AutoLisp从入门到放弃(一)
  8. ngnix跨域问题整理
  9. 关于wangeditor取消自动获取焦点
  10. 77种互联网盈利创新模式(10)