Brian Ritchie's Blog

My ramblings on .NET & other development topics

News



Twitter

Blog Roll

Connect with me

Extracting EXIF metadata from JPG images

Drew Noakes has written a great EXIF metadata extractor for JPG images in Java which was ported to C# by Ferret Renaud.  This allows you to pull out the EXIF data like camera model & date taken.  You can also access the width & height of the image without loading it into a .NET image object.

Here is some sample code on how to use the library:

   1:  FileInfo f = new FileInfo(file);
   2:  var metadata = com.drew.imaging.jpg.JpegMetadataReader.ReadMetadata(f);
   3:  var exif = metadata.GetDirectory(typeof(com.drew.metadata.exif.ExifDirectory).ToString()) as ExifDirectory;
   4:  var jpg = metadata.GetDirectory(typeof(com.drew.metadata.jpeg.JpegDirectory).ToString()) as JpegDirectory;
   5:  ImageModel model = new ImageModel() { 
   6:      path = f.Name, 
   7:      height = jpg.GetImageHeight(),
   8:      width = jpg.GetImageWidth(),
   9:      taken = exif.ContainsTag(ExifDirectory.TAG_DATETIME_ORIGINAL) ? (DateTime?) exif.GetDate(ExifDirectory.TAG_DATETIME_ORIGINAL) : null,
  10:      maker = exif.GetString(ExifDirectory.TAG_MAKE),
  11:      model = exif.GetString(ExifDirectory.TAG_MODEL)
  12:  };


Comments

How to add a watermark to a foto or picture » Blog Archive » How do I watermark from jpg images? said:

Pingback from  How to add a watermark to a foto or picture  » Blog Archive   » How do I watermark from jpg images?

# November 1, 2010 5:10 PM

web design hampshire said:

Good post,When I use your function to read exif from jpeg file, I encounter an infinite loop problem with a jpeg picture. It seems occured in Exif::ReadIfds of exif.cc. I send this jpeg picture to your email, could you please give me some suggestion about this problem? Thanks a lot.

# November 25, 2010 1:12 AM

london design agency said:

I also had the same problem.

# January 31, 2011 3:22 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)