Click here to Skip to main content
15,860,943 members
Articles / Mobile Apps / Windows Phone 7

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

Rate me:
Please Sign up or sign in to vote.
4.90/5 (97 votes)
17 Aug 2015CPOL5 min read 1.3M   21.1K   325   366
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., camhera 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:

C#
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 an issue raised in workspaces by Charlie Hess

Version 1.6

  •  
  • 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
  • Thanks to a comment from disore, fixed ArgumentExceptions thrown when the end of the stream is reached during instantiation; wrapped some instantiation exceptions in ExifLibExceptions.

Version 1.7

  • Updated tag support to EXIF 2.3
  • Updated IFD selection to handle out-of-sequence tag designations (to support non-standard Microsoft XP tags)
  • Added support for unicode-encoded strings, thanks to comments from lightfinder and Forcasual things.

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)


Written By
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

 
QuestionGet MakerNote tag of datatype string Pin
Sean Schult5-Feb-23 7:10
Sean Schult5-Feb-23 7:10 
AnswerRe: Get MakerNote tag of datatype string Pin
Simon McKenzie5-Feb-23 22:41
Simon McKenzie5-Feb-23 22:41 
AnswerRe: Get MakerNote tag of datatype string Pin
Simon McKenzie5-Feb-23 23:31
Simon McKenzie5-Feb-23 23:31 
AnswerRe: Get MakerNote tag of datatype string Pin
Simon McKenzie5-Feb-23 23:52
Simon McKenzie5-Feb-23 23:52 
GeneralRe: Get MakerNote tag of datatype string Pin
Sean Schult8-Feb-23 6:21
Sean Schult8-Feb-23 6:21 
GeneralRe: Get MakerNote tag of datatype string Pin
Simon McKenzie8-Feb-23 15:47
Simon McKenzie8-Feb-23 15:47 
QuestionHow about get PNG Exif information Pin
AndyHo4-Jan-21 4:36
professionalAndyHo4-Jan-21 4:36 
AnswerRe: How about get PNG Exif information Pin
Simon McKenzie9-Jan-21 22:11
Simon McKenzie9-Jan-21 22:11 
GeneralRe: How about get PNG Exif information Pin
AndyHo10-Jan-21 8:07
professionalAndyHo10-Jan-21 8:07 
GeneralRe: How about get PNG Exif information Pin
Simon McKenzie10-Jan-21 21:52
Simon McKenzie10-Jan-21 21:52 
QuestionException when no EXIF data Pin
Lojikl8-Oct-20 17:51
Lojikl8-Oct-20 17:51 
AnswerRe: Exception when no EXIF data Pin
Simon McKenzie9-Oct-20 23:31
Simon McKenzie9-Oct-20 23:31 
QuestionQuestion regarding usage and licensing Pin
Member 1482982612-May-20 4:17
Member 1482982612-May-20 4:17 
AnswerRe: Question regarding usage and licensing Pin
Simon McKenzie15-May-20 14:10
Simon McKenzie15-May-20 14:10 
QuestionGetting long and lat and putting values in labels Pin
Member 1463927817-Feb-20 12:28
Member 1463927817-Feb-20 12:28 
AnswerRe: Getting long and lat and putting values in labels Pin
Simon McKenzie2-May-20 13:00
Simon McKenzie2-May-20 13:00 
AnswerRe: Getting long and lat and putting values in labels Pin
Hans-Peter Kalb12-Feb-21 23:15
Hans-Peter Kalb12-Feb-21 23:15 
QuestionDateTaken time doesnt show milliseconds Pin
Robert Stefanz10-Dec-18 1:24
Robert Stefanz10-Dec-18 1:24 
BugAperture Value Pin
FJR_WLB_NJ16-Nov-18 6:05
FJR_WLB_NJ16-Nov-18 6:05 
QuestionWriting EXIF data to JPG file. Pin
SunDog271014-Jul-18 21:22
SunDog271014-Jul-18 21:22 
AnswerRe: Writing EXIF data to JPG file. Pin
Hans-Peter Kalb11-Feb-21 8:43
Hans-Peter Kalb11-Feb-21 8:43 
GeneralRe: Writing EXIF data to JPG file. Pin
SunDog271011-Feb-21 9:38
SunDog271011-Feb-21 9:38 
QuestionXamarin Forms Pin
kentrader13-May-18 10:41
professionalkentrader13-May-18 10:41 
QuestionHow do I get LensID and LensType out of ExifLib? Pin
jeroen van dael13-Dec-17 11:07
jeroen van dael13-Dec-17 11:07 
AnswerRe: How do I get LensID and LensType out of ExifLib? Pin
Simon McKenzie23-Jan-18 16:58
Simon McKenzie23-Jan-18 16:58 

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

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