ExifHelper.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System;
  2. using System.Drawing;
  3. using System.Text;
  4. namespace Y.Utils.ImageUtils
  5. {
  6. public class ExifHelper : IDisposable
  7. {
  8. private Bitmap Image;
  9. private Encoding Encoding = Encoding.UTF8;
  10. private string DefaultValue = "";
  11. public ExifHelper(string fileName)
  12. {
  13. Image = (Bitmap)Bitmap.FromFile(fileName);
  14. }
  15. public ExifHelper(string fileName, string defaultValue)
  16. {
  17. Image = (Bitmap)Bitmap.FromFile(fileName);
  18. DefaultValue = defaultValue;
  19. }
  20. public enum TagNames : int
  21. {
  22. ExifIFD = 0x8769,
  23. GpsIFD = 0x8825,
  24. NewSubfileType = 0xFE,
  25. SubfileType = 0xFF,
  26. ImageWidth = 0x100,
  27. ImageHeight = 0x101,
  28. BitsPerSample = 0x102,
  29. Compression = 0x103,
  30. PhotometricInterp = 0x106,
  31. ThreshHolding = 0x107,
  32. CellWidth = 0x108,
  33. CellHeight = 0x109,
  34. FillOrder = 0x10A,
  35. DocumentName = 0x10D,
  36. ImageDescription = 0x10E,
  37. EquipMake = 0x10F,
  38. EquipModel = 0x110,
  39. StripOffsets = 0x111,
  40. Orientation = 0x112,
  41. SamplesPerPixel = 0x115,
  42. RowsPerStrip = 0x116,
  43. StripBytesCount = 0x117,
  44. MinSampleValue = 0x118,
  45. MaxSampleValue = 0x119,
  46. XResolution = 0x11A,
  47. YResolution = 0x11B,
  48. PlanarConfig = 0x11C,
  49. PageName = 0x11D,
  50. XPosition = 0x11E,
  51. YPosition = 0x11F,
  52. FreeOffset = 0x120,
  53. FreeByteCounts = 0x121,
  54. GrayResponseUnit = 0x122,
  55. GrayResponseCurve = 0x123,
  56. T4Option = 0x124,
  57. T6Option = 0x125,
  58. ResolutionUnit = 0x128,
  59. PageNumber = 0x129,
  60. TransferFuncition = 0x12D,
  61. SoftwareUsed = 0x131,
  62. DateTime = 0x132,
  63. Artist = 0x13B,
  64. HostComputer = 0x13C,
  65. Predictor = 0x13D,
  66. WhitePoint = 0x13E,
  67. PrimaryChromaticities = 0x13F,
  68. ColorMap = 0x140,
  69. HalftoneHints = 0x141,
  70. TileWidth = 0x142,
  71. TileLength = 0x143,
  72. TileOffset = 0x144,
  73. TileByteCounts = 0x145,
  74. InkSet = 0x14C,
  75. InkNames = 0x14D,
  76. NumberOfInks = 0x14E,
  77. DotRange = 0x150,
  78. TargetPrinter = 0x151,
  79. ExtraSamples = 0x152,
  80. SampleFormat = 0x153,
  81. SMinSampleValue = 0x154,
  82. SMaxSampleValue = 0x155,
  83. TransferRange = 0x156,
  84. JPEGProc = 0x200,
  85. JPEGInterFormat = 0x201,
  86. JPEGInterLength = 0x202,
  87. JPEGRestartInterval = 0x203,
  88. JPEGLosslessPredictors = 0x205,
  89. JPEGPointTransforms = 0x206,
  90. JPEGQTables = 0x207,
  91. JPEGDCTables = 0x208,
  92. JPEGACTables = 0x209,
  93. YCbCrCoefficients = 0x211,
  94. YCbCrSubsampling = 0x212,
  95. YCbCrPositioning = 0x213,
  96. REFBlackWhite = 0x214,
  97. ICCProfile = 0x8773,
  98. Gamma = 0x301,
  99. ICCProfileDescriptor = 0x302,
  100. SRGBRenderingIntent = 0x303,
  101. ImageTitle = 0x320,
  102. Copyright = 0x8298,
  103. ResolutionXUnit = 0x5001,
  104. ResolutionYUnit = 0x5002,
  105. ResolutionXLengthUnit = 0x5003,
  106. ResolutionYLengthUnit = 0x5004,
  107. PrintFlags = 0x5005,
  108. PrintFlagsVersion = 0x5006,
  109. PrintFlagsCrop = 0x5007,
  110. PrintFlagsBleedWidth = 0x5008,
  111. PrintFlagsBleedWidthScale = 0x5009,
  112. HalftoneLPI = 0x500A,
  113. HalftoneLPIUnit = 0x500B,
  114. HalftoneDegree = 0x500C,
  115. HalftoneShape = 0x500D,
  116. HalftoneMisc = 0x500E,
  117. HalftoneScreen = 0x500F,
  118. JPEGQuality = 0x5010,
  119. GridSize = 0x5011,
  120. ThumbnailFormat = 0x5012,
  121. ThumbnailWidth = 0x5013,
  122. ThumbnailHeight = 0x5014,
  123. ThumbnailColorDepth = 0x5015,
  124. ThumbnailPlanes = 0x5016,
  125. ThumbnailRawBytes = 0x5017,
  126. ThumbnailSize = 0x5018,
  127. ThumbnailCompressedSize = 0x5019,
  128. ColorTransferFunction = 0x501A,
  129. ThumbnailData = 0x501B,
  130. ThumbnailImageWidth = 0x5020,
  131. ThumbnailImageHeight = 0x502,
  132. ThumbnailBitsPerSample = 0x5022,
  133. ThumbnailCompression = 0x5023,
  134. ThumbnailPhotometricInterp = 0x5024,
  135. ThumbnailImageDescription = 0x5025,
  136. ThumbnailEquipMake = 0x5026,
  137. ThumbnailEquipModel = 0x5027,
  138. ThumbnailStripOffsets = 0x5028,
  139. ThumbnailOrientation = 0x5029,
  140. ThumbnailSamplesPerPixel = 0x502A,
  141. ThumbnailRowsPerStrip = 0x502B,
  142. ThumbnailStripBytesCount = 0x502C,
  143. ThumbnailResolutionX = 0x502D,
  144. ThumbnailResolutionY = 0x502E,
  145. ThumbnailPlanarConfig = 0x502F,
  146. ThumbnailResolutionUnit = 0x5030,
  147. ThumbnailTransferFunction = 0x5031,
  148. ThumbnailSoftwareUsed = 0x5032,
  149. ThumbnailDateTime = 0x5033,
  150. ThumbnailArtist = 0x5034,
  151. ThumbnailWhitePoint = 0x5035,
  152. ThumbnailPrimaryChromaticities = 0x5036,
  153. ThumbnailYCbCrCoefficients = 0x5037,
  154. ThumbnailYCbCrSubsampling = 0x5038,
  155. ThumbnailYCbCrPositioning = 0x5039,
  156. ThumbnailRefBlackWhite = 0x503A,
  157. ThumbnailCopyRight = 0x503B,
  158. LuminanceTable = 0x5090,
  159. ChrominanceTable = 0x5091,
  160. FrameDelay = 0x5100,
  161. LoopCount = 0x5101,
  162. PixelUnit = 0x5110,
  163. PixelPerUnitX = 0x5111,
  164. PixelPerUnitY = 0x5112,
  165. PaletteHistogram = 0x5113,
  166. ExifExposureTime = 0x829A,
  167. ExifFNumber = 0x829D,
  168. ExifExposureProg = 0x8822,
  169. ExifSpectralSense = 0x8824,
  170. ExifISOSpeed = 0x8827,
  171. ExifOECF = 0x8828,
  172. ExifVer = 0x9000,
  173. ExifDTOrig = 0x9003,
  174. ExifDTDigitized = 0x9004,
  175. ExifCompConfig = 0x9101,
  176. ExifCompBPP = 0x9102,
  177. ExifShutterSpeed = 0x9201,
  178. ExifAperture = 0x9202,
  179. ExifBrightness = 0x9203,
  180. ExifExposureBias = 0x9204,
  181. ExifMaxAperture = 0x9205,
  182. ExifSubjectDist = 0x9206,
  183. ExifMeteringMode = 0x9207,
  184. ExifLightSource = 0x9208,
  185. ExifFlash = 0x9209,
  186. ExifFocalLength = 0x920A,
  187. ExifMakerNote = 0x927C,
  188. ExifUserComment = 0x9286,
  189. ExifDTSubsec = 0x9290,
  190. ExifDTOrigSS = 0x9291,
  191. ExifDTDigSS = 0x9292,
  192. ExifFPXVer = 0xA000,
  193. ExifColorSpace = 0xA001,
  194. ExifPixXDim = 0xA002,
  195. ExifPixYDim = 0xA003,
  196. ExifRelatedWav = 0xA004,
  197. ExifInterop = 0xA005,
  198. ExifFlashEnergy = 0xA20B,
  199. ExifSpatialFR = 0xA20C,
  200. ExifFocalXRes = 0xA20E,
  201. ExifFocalYRes = 0xA20F,
  202. ExifFocalResUnit = 0xA210,
  203. ExifSubjectLoc = 0xA214,
  204. ExifExposureIndex = 0xA215,
  205. ExifSensingMethod = 0xA217,
  206. ExifFileSource = 0xA300,
  207. ExifSceneType = 0xA301,
  208. ExifCfaPattern = 0xA302,
  209. GpsVer = 0x0,
  210. GpsLatitudeRef = 0x1,
  211. GpsLatitude = 0x2,
  212. GpsLongitudeRef = 0x3,
  213. GpsLongitude = 0x4,
  214. GpsAltitudeRef = 0x5,
  215. GpsAltitude = 0x6,
  216. GpsGpsTime = 0x7,
  217. GpsGpsSatellites = 0x8,
  218. GpsGpsStatus = 0x9,
  219. GpsGpsMeasureMode = 0xA,
  220. GpsGpsDop = 0xB,
  221. GpsSpeedRef = 0xC,
  222. GpsSpeed = 0xD,
  223. GpsTrackRef = 0xE,
  224. GpsTrack = 0xF,
  225. GpsImgDirRef = 0x10,
  226. GpsImgDir = 0x11,
  227. GpsMapDatum = 0x12,
  228. GpsDestLatRef = 0x13,
  229. GpsDestLat = 0x14,
  230. GpsDestLongRef = 0x15,
  231. GpsDestLong = 0x16,
  232. GpsDestBearRef = 0x17,
  233. GpsDestBear = 0x18,
  234. GpsDestDistRef = 0x19,
  235. GpsDestDist = 0x1A
  236. }
  237. public string GetPropertyString(Int32 pid)
  238. {
  239. if (IsPropertyDefined(pid))
  240. return GetString(Image.GetPropertyItem(pid).Value);
  241. else
  242. return DefaultValue;
  243. }
  244. public double GetPropertyDouble(Int32 pid)
  245. {
  246. double result = 0;
  247. if (IsPropertyDefined(pid))
  248. {
  249. byte[] value = Image.GetPropertyItem(pid).Value;
  250. if (value.Length == 24)
  251. {
  252. double d = BitConverter.ToUInt32(value, 0) * 1.0d / BitConverter.ToUInt32(value, 4);
  253. double m = BitConverter.ToUInt32(value, 8) * 1.0d / BitConverter.ToUInt32(value, 12);
  254. double s = BitConverter.ToUInt32(value, 16) * 1.0d / BitConverter.ToUInt32(value, 20);
  255. result = (((s / 60 + m) / 60) + d);
  256. }
  257. }
  258. return result;
  259. }
  260. public char GetPropertyChar(Int32 pid)
  261. {
  262. char result = ' ';
  263. if (IsPropertyDefined(pid))
  264. {
  265. byte[] value = Image.GetPropertyItem(pid).Value;
  266. result = BitConverter.ToChar(value, 0);
  267. }
  268. return result;
  269. }
  270. private string GetString(byte[] bt)
  271. {
  272. string result = Encoding.GetString(bt);
  273. if (result.EndsWith("\0"))
  274. result = result.Substring(0, result.Length - 1);
  275. return result;
  276. }
  277. private bool IsPropertyDefined(Int32 pid)
  278. {
  279. return (Array.IndexOf(Image.PropertyIdList, pid) > -1);
  280. }
  281. public void Dispose()
  282. {
  283. Image.Dispose();
  284. }
  285. }
  286. }