Click here to Skip to main content
Click here to Skip to main content

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

, 21 Jul 2014
Rate this:
Please Sign up or sign in to vote.
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
using (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);
    }
}

Note that the ExifReader class holds the image file open, so once you're finished with the reader, be sure to call its Dispose method, either explicitly, or implicitly through a using statement (as in the above example).

How it works

Exif data is stored in the JPEG header, inside the APP1 block. This block contains a number of IFDs (image file directories). These include the EXIF IFD and the GPS IFD, which contain the tags available for retrieval by this library.

The IFDs start with a catalogue of EXIF tags and the stream offsets to where the data for each tag is stored. On instantiation, the library creates a Dictionary of these tag offsets, which are accessed during retrieval of a specific tag's value. It's this lazy retrieval which gives the library its speed, as most EXIF use-cases only involve retrieving a handful of tag values, so there's little use in retrieving tag values before they're required.

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 12MP 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+.

Version 1.3

  • Added the ability to extract JPEG encoded thumbnails from images, thanks to a comment from StyrianOak. Note that uncompressed (i.e. TIFF) encoded thumbnails are not supported, but since any camera which supports the DCF standard will produce JPEG thumbnails, this is a minor limitation.

Version 1.4

  • Added a constructor overload to allow reading of JPEG data from any seekable stream
  • Modified code to allow compiling for Windows Phone and Silverlight. The NuGet package now includes Windows Phone and Silverlight DLLs.
  • Improved support for null DateTime values thanks to comments from schurig and BrandonOrding
  • undefined Exif fields are now returned as byte[] instead of uint[]
  • Fixed a bug in the thumbnail extractor where thumbnails with 0xFF padding were not being recognised, thanks to a comment from _d-fens_.
  • Added the option to retrieve a TIFF rational as an int[] {numerator, denominator} array (instead of double), thanks to a comment from Member 10226163.
  • Changed thumbnail padding detection code to accept 0x00 as well as 0xFF as padding bytes, thanks to a comment from Cruiser77
  • Added conditional compilation options for Windows Store app compatibility, thanks to a comment from _dieQueeQ.

Version 1.5

  • Fixed an exception when reading images containing tags without datatypes
  • Refactored to store tag data in separate dictionaries for each IFD
  • Added the ability to extract metadata from images which don't contain the EXIF sub IFD, thanks to a task raised in workspaces by Charlie Hess

Version 1.6

  • On frameworks prior to 4.5, an exception thrown while initialising the reader can result in the stream being closed. Added code for 4.5+ to prevent this from happening
  • Added a new constructor parameter for framework 4.5+ to allow the user to indicate that the supplied stream should be left open when the reader is disposed

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

Simon McKenzie has been working as a developer for 10 years, primarily in .NET, with interests in imaging and GIS, particularly on mobile platforms. He is the author of the award winning MapSnap GPS, a moving map application for Windows Phone. He's also the author of the popular (free) high-speed ExifLib EXIF extractor for .NET.


Comments and Discussions

 
QuestionNuGet Compatibility Issue PinmemberActualRandy25-Sep-12 14:33 
AnswerRe: NuGet Compatibility Issue PinmemberSimon McKenzie25-Sep-12 14:39 
GeneralRe: NuGet Compatibility Issue PinmemberActualRandy25-Sep-12 15:35 
QuestionAny plans to support EXIF for raw formats? PinmemberYuriGubanov4-Sep-12 0:55 
AnswerRe: Any plans to support EXIF for raw formats? PinmemberSimon McKenzie4-Sep-12 2:00 
GeneralRe: Any plans to support EXIF for raw formats? Pinmembermrdat22-Mar-13 5:22 
GeneralRe: Any plans to support EXIF for raw formats? PinmemberSimon McKenzie22-Mar-13 19:26 
QuestionExifReader can take a URL PinmemberMember 355869125-Jun-12 11:47 
AnswerRe: ExifReader can take a URL PinmemberSimon McKenzie25-Jun-12 13:24 
AnswerRe: ExifReader can take a URL PinmemberSimon McKenzie3-Jan-13 13:03 
QuestionSplendid job! PinmemberRavi Bhavnani6-Jun-12 16:55 
AnswerRe: Splendid job! PinmemberSimon McKenzie6-Jun-12 17:24 
QuestionImage Width & Height Pinmemberestramadour10-Mar-12 11:14 
AnswerRe: Image Width & Height PinmemberSimon McKenzie11-Mar-12 12:52 
Hi Marcus,
 
It may simply be that your images don't contain the image width and height. None of my cameras (Nikon D90, Canon A710, Fujifilm FinePix S9500) include these tags in images, probably because they're direct properties of the pixel data itself. Try running the exiftest utiity included in the source pack on one of your images to see which tags are available in your images.
 
If your images don't contain these tags, you can retrieve it via GDI+:
            Image image = Image.FromFile("image.jpg");
            Console.WriteLine("Dimensions: {0},{1}", image.Height, image.Width);
There's also a stackoverflow article which describes a faster, but less tested approach.
 
Cheers,
 
Simon
QuestionDelegate issue with VS 2008 PinmemberMember 868595029-Feb-12 5:29 
AnswerRe: Delegate issue with VS 2008 [modified] PinmemberSimon McKenzie29-Feb-12 11:53 
GeneralRe: Delegate issue with VS 2008 PinmemberYuriGubanov2-Sep-12 11:36 
GeneralRe: Delegate issue with VS 2008 [modified] PinmemberSimon McKenzie4-Sep-12 1:54 
SuggestionSeems like a great library - please create a NuGet package PinmemberLoraderon22-Feb-12 0:40 
GeneralRe: Seems like a great library - please create a NuGet package [modified] PinmemberSimon McKenzie22-Feb-12 13:10 
BugGot it to work, but problems with two tags [modified] PinmemberJaxa31318-Feb-12 7:48 
GeneralRe: Got it to work, but problems with two tags PinmemberSimon McKenzie19-Feb-12 13:23 
Questionhow to read Title, Keywords info? [modified] PinmemberJanJankovsky5-Feb-12 0:07 
AnswerRe: how to read Title, Keywords info? PinmemberSimon McKenzie5-Feb-12 11:26 
SuggestionAwesome tool needs more love! PinmemberJonh04425-Jan-12 4:13 
GeneralRe: Awesome tool needs more love! PinmemberSimon McKenzie22-Feb-12 13:31 
GeneralMy vote of 5 PinmemberIzanami3-Jan-12 18:30 
QuestionHow to get GPS coordinate PinmemberWrangly14-Dec-11 7:09 
AnswerRe: How to get GPS coordinate [modified] PinmemberSimon McKenzie14-Dec-11 12:02 
GeneralRe: How to get GPS coordinate PinmemberWrangly14-Dec-11 22:05 
QuestionCreation of ExifLib.dll PinmemberMember 842783123-Nov-11 7:32 
AnswerRe: Creation of ExifLib.dll PinmemberSimon McKenzie23-Nov-11 9:24 
Questionbug when converting tagdata to ushort (big endian) Pinmemberbartsy7-Nov-11 1:51 
AnswerRe: bug when converting tagdata to ushort (big endian) PinmemberSimon McKenzie7-Nov-11 13:28 
GeneralSlower than Image for reading all tags PinmemberThymine10-Jun-11 9:29 
GeneralRe: Slower than Image for reading all tags PinmemberSimon McKenzie13-Jun-11 13:44 
GeneralRe: Slower than Image for reading all tags PinmemberThymine22-Jul-11 6:37 
GeneralExifTags.FocalLengthIn35mmFilm is not returning a value [modified] Pinmembercraude5-Sep-10 4:47 
GeneralRe: ExifTags.FocalLengthIn35mmFilm is not returning a value PinmemberSimon McKenzie5-Sep-10 11:57 
GeneralRe: ExifTags.FocalLengthIn35mmFilm is not returning a value Pinmembercraude5-Sep-10 12:26 
GeneralRe: ExifTags.FocalLengthIn35mmFilm is not returning a value PinmemberSimon McKenzie5-Sep-10 13:38 
GeneralImageWidth is not returning anything :( [modified] PinmemberNeilFawcett19-Aug-10 6:19 
GeneralRe: ImageWidth is not returning anything :( PinmemberSimon McKenzie19-Aug-10 13:44 
GeneralReturn data type changing PinmemberNeilFawcett19-Aug-10 23:23 
GeneralRe: Return data type changing PinmemberSimon McKenzie20-Aug-10 18:13 
GeneralGetting all values... PinmemberNeilFawcett21-Aug-10 0:59 
GeneralRe: Getting all values... PinmemberSimon McKenzie21-Aug-10 15:18 
QuestionHow to call/use this in vb.NET? [modified] PinmemberNeilFawcett19-Aug-10 3:07 
AnswerExifLib - A Fast Exif Data Extractor for .NET and Silverlight PinmemberKevin Pammett7-Jul-10 7:17 
GeneralSuggestion Pinmemberfree-seeker31-Mar-10 8:25 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 22 Jul 2014
Article Copyright 2009 by Simon McKenzie
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid