Click here to Skip to main content
Licence CPOL
First Posted 11 May 2009
Views 60,955
Downloads 2,300
Bookmarked 102 times

ExifLib - A Fast Exif Data Extractor for .NET 2.0+

By | 23 Feb 2012 | Article
Reads JPEG Exif data without the heavyweight and unnecessary instantiation of GDI+ objects.
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+.

NuGet Release

ExifLib is now available on nuget! Simply install from the Visual Studio Package Manager Console using Install-Package ExifLib.

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's also the author of the popular (free) high-speed ExifLib EXIF extractor for .NET.

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
QuestionImage Width & Height Pinmemberestramadour11:14 10 Mar '12  
AnswerRe: Image Width & Height PinmemberSimon McKenzie12:52 11 Mar '12  
QuestionDelegate issue with VS 2008 PinmemberMember 86859505:29 29 Feb '12  
AnswerRe: Delegate issue with VS 2008 [modified] PinmemberSimon McKenzie11:53 29 Feb '12  
SuggestionSeems like a great library - please create a NuGet package PinmemberLoraderon0:40 22 Feb '12  
GeneralRe: Seems like a great library - please create a NuGet package [modified] PinmemberSimon McKenzie13:10 22 Feb '12  
BugGot it to work, but problems with two tags [modified] PinmemberJaxa3137:48 18 Feb '12  
GeneralRe: Got it to work, but problems with two tags PinmemberSimon McKenzie13:23 19 Feb '12  
Questionhow to read Title, Keywords info? [modified] PinmemberJanJankovsky0:07 5 Feb '12  
AnswerRe: how to read Title, Keywords info? PinmemberSimon McKenzie11:26 5 Feb '12  
SuggestionAwesome tool needs more love! PinmemberJonh0444:13 25 Jan '12  
GeneralRe: Awesome tool needs more love! PinmemberSimon McKenzie13:31 22 Feb '12  
GeneralMy vote of 5 PinmemberIzanami18:30 3 Jan '12  
QuestionHow to get GPS coordinate PinmemberWrangly7:09 14 Dec '11  
AnswerRe: How to get GPS coordinate PinmemberSimon McKenzie12:02 14 Dec '11  
GeneralRe: How to get GPS coordinate PinmemberWrangly22:05 14 Dec '11  
QuestionCreation of ExifLib.dll PinmemberMember 84278317:32 23 Nov '11  
AnswerRe: Creation of ExifLib.dll PinmemberSimon McKenzie9:24 23 Nov '11  
Questionbug when converting tagdata to ushort (big endian) Pinmemberbartsy1:51 7 Nov '11  
AnswerRe: bug when converting tagdata to ushort (big endian) PinmemberSimon McKenzie13:28 7 Nov '11  
GeneralSlower than Image for reading all tags PinmemberThymine9:29 10 Jun '11  
GeneralRe: Slower than Image for reading all tags PinmemberSimon McKenzie13:44 13 Jun '11  
GeneralRe: Slower than Image for reading all tags PinmemberThymine6:37 22 Jul '11  
GeneralExifTags.FocalLengthIn35mmFilm is not returning a value [modified] Pinmembercraude4:47 5 Sep '10  
GeneralRe: ExifTags.FocalLengthIn35mmFilm is not returning a value PinmemberSimon McKenzie11:57 5 Sep '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
Web04 | 2.5.120517.1 | Last Updated 23 Feb 2012
Article Copyright 2009 by Simon McKenzie
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid