ExifHelper.cs 10 KB

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