Click here to Skip to main content
15,881,559 members
Articles / Desktop Programming / WPF
Article

Retrieving EXIF information of an image in .NET 3.0

Rate me:
Please Sign up or sign in to vote.
3.40/5 (4 votes)
2 Jul 2007CPOL2 min read 67.2K   1.2K   29   9
How to retrieve EXIF information of an image in .NET 3.0.

Screenshot - Article_Image.jpg

Introduction

When we talk about digital photography, one of the things which comes to mind is EXIF info. What is it? EXIF is the short for "Exchangeable Image File". This is a standard for storing interchangeable information in digital photography image files using JPEG compression. Most of the new digital cameras use the EXIF annotation, and store information on the image such as shutter speed, exposure, compensation, F number, metering system, ISO number, Date Time etc. This piece of code explains how to retrieve the EXIF information from an image using .NET 3.0.

Using the code

To use the code, you should have the following items installed on your machine:

  • .NET Framework 3.0
  • WPF Patch release for Visual Studio 2005

Using the code is pretty simple, and can be downloaded from above. In the code, the constructor of class ExifMetaInfo accepts the URI of an image as a parameter, and creates an object of type BitmapFrame (BitmapFrame is a new class library available under "System.Windows.Media.Imaging" of .NET 3.0). Once the BitmapFrame object is created, we can get the meta-data information of the image using the MetaData property of BitmapFrame. We can type cast the MetaData property to the BitmapMetadata class, which is again a new class in .NET 3.0 under "System.Windows.Media.Imaging". Now, it's a matter of passing the right parameters to the "BitmapMetadata" object to get the EXIF information. Following is a list of parameters which we may pass to get the appropriate EXIF information:

Width/app1/ifd/exif/subifd:{uint=40962}
Height/app1/ifd/exif/subifd:{uint=40963}
EquipmentManufacturer/app1/ifd/exif:{uint=271}
CameraModel/app1/ifd/exif:{uint=272}
CreationSoftware/app1/ifd/exif:{uint=305}
ColorRepresentation/app1/ifd/exif/subifd:{uint=40961}")
ExposureTime/app1/ifd/exif/subifd:{uint=33434}
LensAperture/app1/ifd/exif/subifd:{uint=33437}

.....and many more.

The section below shows a snap of the constructor of the ExifMetaInfo class, which accepts a URI as a parameter and creates an object of type BitmapFrame:

C#
public ExifMetaInfo(Uri imageUri)
{
    BitmapFrame bFrame = BitmapFrame.Create(imageUri, 
                BitmapCreateOptions.DelayCreation, 
                BitmapCacheOption.None);
    _metaInfo = (BitmapMetadata)bFrame.Metadata;
}

Once we are done with the creation of the object, the next thing to do is to fetch the values. In the given class, all the EXIF info is being retrieved through properties. The code below shows the retrieval of the Width property of the image:

C#
public uint? Width
{
    get
    {
        object obj = GetMetaInfo("/app1/ifd/exif/subifd:{uint=40962}");
        if (obj == null)
        {
            return null;
        }
        else
        {
            if (obj.GetType() == typeof(UInt32))
                return (uint)obj;
            else
                return Convert.ToUInt32(obj);
        }
    }
}

private object GetMetaInfo(string infoQuery)
{
    if (_metaInfo.ContainsQuery(infoQuery))
        return _metaInfo.GetQuery(infoQuery);
    else
        return null;
}

Points of interest

Please note that all JPG images may not have EXIF info available. You may modify the code of the ExifMetaInfo class to give a user friendly message when no information is available.

History

Nothing available at present.

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalgps information Pin
cookie_biscuit28-Mar-09 15:17
cookie_biscuit28-Mar-09 15:17 
GeneralRe: gps information Pin
Felipe Ceotto17-Apr-09 0:44
Felipe Ceotto17-Apr-09 0:44 
Generaledit the exif information using .net 2.0 Pin
nallamani198317-Mar-09 21:32
nallamani198317-Mar-09 21:32 
GeneralASP.NET 3.0 Pin
RoelAlblas12-Jul-08 4:45
RoelAlblas12-Jul-08 4:45 
AnswerRe: ASP.NET 3.0 Pin
Puru Adhikari14-Nov-08 23:54
Puru Adhikari14-Nov-08 23:54 
Yes but you definetly need to change the code a bit
GeneralMetadata 272 may contain string[] Pin
CodeChampion22-Apr-08 4:18
CodeChampion22-Apr-08 4:18 
QuestionExif GetQuery strings Pin
GreenKnight12-Sep-07 3:51
GreenKnight12-Sep-07 3:51 
GeneralRe: Exif GetQuery strings Pin
FaulstiR7-Dec-07 17:46
FaulstiR7-Dec-07 17:46 
AnswerRe: Exif GetQuery strings Pin
FaulstiR7-Dec-07 18:07
FaulstiR7-Dec-07 18:07 

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.