Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Retrieving EXIF information of an image in .NET 3.0

0.00/5 (No votes)
2 Jul 2007 1  
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:

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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here