Click here to Skip to main content
Licence CPOL
First Posted 11 May 2009
Views 48,271
Downloads 1,477
Bookmarked 84 times

ExifLib - A Fast Exif Data Extractor for .NET 2.0

By Simon McKenzie | 9 Nov 2011
Reads JPEG Exif data without the heavyweight and unnecessary instantiation of GDI+ objects.

1

2
1 vote, 7.1%
3
4 votes, 28.6%
4
9 votes, 64.3%
5
4.65/5 - 14 votes
1 removed
μ 4.54, σa 1.16 [?]
Exif Lib - test application

Introduction

ExifLib simply reads Exif tags (i.e., camera model, GPS data, date picture taken, shutter speed etc.) from JPEG files, without the overhead introduced by using the GDI+ classes located in System.Drawing.Imaging, and with less lines of code for the developer.

Background

I've been using a simple command line application to move my photos into subdirectories based on the date on which they were created. As with all other .NET Exif implementations I've seen, I was using the PropertyItem class located in System.Drawing.Imaging. While this does the job, I often found myself processing thousands of images at a time, and the .NET classes were just too slow for the job. ExifLib goes back to the JPEG/TIFF standard itself, and only reads the essentials, using little more than the file input classes in System.IO.

Using the Code

ExifLib is very simple, with only one class and one enum in the namespace. Just add a reference to ExifLib.dll, and you're good to go! An example follows:

using ExifLib;
...
...
...
// Instantiate the reader
ExifReader reader = new ExifReader(@"C:\temp\testImage.jpg");

// Extract the tag data using the ExifTags enumeration
DateTime datePictureTaken;
if (reader.GetTagValue<DateTime>(ExifTags.DateTimeDigitized, 
                                    out datePictureTaken))
{
    // Do whatever is required with the extracted information
    MessageBox.Show(this, string.Format("The picture was taken on {0}", 
       datePictureTaken), "Image information", MessageBoxButtons.OK);
}

Points of Interest

Something strange that I learned while writing this library is that while JPEG stipulates "Big Endian" encoding (i.e., numbers read from left to right), the TIFF standard allows Big or Little Endian encoding. Since the Exif tags are encoded using TIFF encoding, often the JPEG will be read using "Big Endian" encoding until the TIFF section is reached, at which point the encoding reverses and the rest of the document is read using "Little Endian" encoding.

During coding, I realised from a comment on the ExifWorks CodeProject article that it's possible to increase performance when using System.Drawing.Image by setting the constructor's validateImageData parameter to false. However, even when using this enhancement, ExifLib still performs 50% faster, possibly because it does not read the tag values until they're requested. I have also noticed that ExifLib performs similarly with small (<1MP) images, but scales better when loading larger images. The screenshot at the top of this page was produced using a 5.5MP image.

History

Version 1.1

  • Array extraction has been added, thanks to a comment from Justin Carasick. This is used in various fields, including GPS coordinates and Exif versioning. The previous version of ExifLib would only return the first element from an array.

Version 1.2

  • Fixed bug when retrieving data for fields shorter than 4 bytes, thanks to a comment from bartsy. The previous version of ExifLib would lose important data from these fields when processing big-endian encoded files.
  • Updated the project to Visual Studio 2010, refactored a little of the code. The project is still .NET 2.0+.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Simon McKenzie

Software Developer
BreastScreen Victoria
Australia Australia

Member
Simon McKenzie has been working as a developer for 8 years, primarily in .NET and Java, with interests in imaging and GIS, particularly on mobile platforms. He is the author of MapSnap GPS, a moving map application for Windows Mobile 6.5 and Windows Phone 7.
 
He recently won the $10,000 RedGate mobile software development competition for MapSnap GPS

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow to read Title, Keywords info? [modified] PinmemberJanJankovsky1:07 5 Feb '12  
AnswerRe: how to read Title, Keywords info? PinmemberSimon McKenzie12:26 5 Feb '12  
SuggestionAwesome tool needs more love! PinmemberJonh0445:13 25 Jan '12  
GeneralMy vote of 5 PinmemberIzanami19:30 3 Jan '12  
QuestionHow to get GPS coordinate PinmemberWrangly8:09 14 Dec '11  
AnswerRe: How to get GPS coordinate PinmemberSimon McKenzie13:02 14 Dec '11  
GeneralRe: How to get GPS coordinate PinmemberWrangly23:05 14 Dec '11  
QuestionCreation of ExifLib.dll PinmemberMember 84278318:32 23 Nov '11  
AnswerRe: Creation of ExifLib.dll PinmemberSimon McKenzie10:24 23 Nov '11  
Questionbug when converting tagdata to ushort (big endian) Pinmemberbartsy2:51 7 Nov '11  
AnswerRe: bug when converting tagdata to ushort (big endian) PinmemberSimon McKenzie14:28 7 Nov '11  
GeneralSlower than Image for reading all tags PinmemberThymine10:29 10 Jun '11  
GeneralRe: Slower than Image for reading all tags PinmemberSimon McKenzie14:44 13 Jun '11  
GeneralRe: Slower than Image for reading all tags PinmemberThymine7:37 22 Jul '11  
GeneralExifTags.FocalLengthIn35mmFilm is not returning a value [modified] Pinmembercraude5:47 5 Sep '10  
GeneralRe: ExifTags.FocalLengthIn35mmFilm is not returning a value PinmemberSimon McKenzie12:57 5 Sep '10  
GeneralRe: ExifTags.FocalLengthIn35mmFilm is not returning a value Pinmembercraude13:26 5 Sep '10  
GeneralRe: ExifTags.FocalLengthIn35mmFilm is not returning a value PinmemberSimon McKenzie14:38 5 Sep '10  
GeneralImageWidth is not returning anything :( [modified] PinmemberNeilFawcett7:19 19 Aug '10  
GeneralRe: ImageWidth is not returning anything :( PinmemberSimon McKenzie14:44 19 Aug '10  
GeneralReturn data type changing PinmemberNeilFawcett0:23 20 Aug '10  
GeneralRe: Return data type changing PinmemberSimon McKenzie19:13 20 Aug '10  
GeneralGetting all values... PinmemberNeilFawcett1:59 21 Aug '10  
GeneralRe: Getting all values... PinmemberSimon McKenzie16:18 21 Aug '10  
QuestionHow to call/use this in vb.NET? [modified] PinmemberNeilFawcett4:07 19 Aug '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120209.1 | Last Updated 9 Nov 2011
Article Copyright 2009 by Simon McKenzie
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid