using System; using System.Drawing; using System.Drawing.Imaging; namespace mikejorgensen.Photography { /// /// Provides access to EXIF data /// public class EXIFData { private string _Equipment, _ShutterSpeed, _Aperture, _ISO, _FocalLength, _ExposureDate, _Flash; private bool _IsEXIF = false; public EXIFData( System.Drawing.Image image) { this.ExtractEXIF(image); } public EXIFData( string filePath ) { try { System.Drawing.Image image = System.Drawing.Image.FromFile(filePath, true); this.ExtractEXIF(image); } catch (Exception exp) { throw new ArgumentException("The file does not exist", exp); } } #region Private Helper Methods private void ExtractEXIF( System.Drawing.Image image) { foreach(System.Drawing.Imaging.PropertyItem Property in image.PropertyItems) { switch (Property.Id) { case (272): //Canon EOS 10D _Equipment = System.Text.Encoding.ASCII.GetString(image.GetPropertyItem(272).Value); _IsEXIF = true; break; case (36867): //Exposure Date _ExposureDate = System.Text.Encoding.ASCII.GetString(image.GetPropertyItem(36867).Value); _IsEXIF = true; break; case (33434): //Shutter _ShutterSpeed = (System.BitConverter.ToInt32(image.GetPropertyItem(33434).Value,0)).ToString() + "/" + (System.BitConverter.ToInt32(image.GetPropertyItem(33434).Value,4)).ToString(); _IsEXIF = true; break; case (33437): //Aperture _Aperture = (Convert.ToDecimal(System.BitConverter.ToInt32(image.GetPropertyItem(33437).Value,0))/Convert.ToDecimal(System.BitConverter.ToInt32(image.GetPropertyItem(33437).Value,4))).ToString(); _IsEXIF = true; break; case (37386): //Focal Length _FocalLength = (System.BitConverter.ToInt32(image.GetPropertyItem(37386).Value,0)/System.BitConverter.ToInt32(image.GetPropertyItem(37386).Value,4)).ToString() + "mm"; _IsEXIF = true; break; case (34855): //ISO _ISO = System.BitConverter.ToUInt16(image.GetPropertyItem(34855).Value,0).ToString(); _IsEXIF = true; break; case (37385): //Flash _Flash = System.BitConverter.ToUInt16(image.GetPropertyItem(37385).Value,0).ToString(); break; } } image.Dispose(); } #endregion #region Properties public bool IsEXIF { get { return _IsEXIF; } } public string Equipment { get { return _Equipment.Remove(_Equipment.Length-1,1); } } public string ShutterSpeed { get { return _ShutterSpeed; } } public string Aperture { get { return _Aperture; } } public string ISO { get { return _ISO; } } public string Flash { get { return _Flash; } } public string FocalLength { get { return _FocalLength; } } public string ExposureDate { get { return _ExposureDate.Substring(5,2) + "/" + _ExposureDate.Substring(8,2) + "/" + _ExposureDate.Substring(0,4) + " " + _ExposureDate.Substring(11,5); } } #endregion } }