Reading EXIF data with C# and csXImage

Here are some code fragments showing how to use the ActiveX control csXImage to read Exif data from JPEG and TIFF images. The code is written in C#. It is assumed that you have already installed either the full or trial version of csXImage and have added a control to a form in C#. More on getting started with csXImage in C#.

Using a For loop to display all the EXIF tags in an image

This first piece of code will display all the Exif attributes stored in an image. It assumes that an instance of csXImage is on the form and an image has been loaded into it. It also requires a List Box. Both of these controls are using their default names.

if (axImageBox1.ExifCount > 0)
{
  for (int i = 0; i < axImageBox1.ExifCount - 1; i++)
  {
    listBox1.Items.Add(axImageBox1.get_ExifName(i) + ": " +
      axImageBox1.get_ExifValueByIndex(i));
  }
}

csXImage does not reserve a separate property for every EXIF tag. Instead it stores all the values in an array and they can be read by index or by the tag name. In this example they are read by index by finding the total number of tags present and then looping through them. The name of the tag can be found using ExifName and the value can be found using ExifValueByIndex

In .NET the ExifName and ExifValueByIndex must be preceded with get_ because they are indexed properties.

Reading a tag by name

If a specific tag must be read the ExifValueByName method can be used. For example, the camera model can be found by reading the Model tag:

label1.Text = axImageBox1.get_ExifValueByName("Model");)

If the Model tag is not present, this will return an empty string. The tag name is not case sensitive so "model" and "MODEL" would also work.

Converting the EXIF values to the correct data type

csXImage stores all the EXIF tags as strings, although these tags can contain numerical values. Numbers can be integers or rational (fractional) numbers, and dates are stored as strings in a specific format. Refer to the csXImage instructions for more on these data types. We provide some helper functions for converting between the data types.

Example of converting a rational value to a real number:

lbl.Text = axImageBox1.RationalToReal(axImageBox1.get_ExifValueByName("ShutterSpeedValue")).ToString();

The RationalToReal method converts a string in the form a/b into a real number, where a and b are integers.

Example of converting an EXIF date/time into an ActiveX date/time:

label1.Text =
    axImageBox1.ExifStringToDate(axImageBox1.get_ExifValueByName("DateTimeOriginal")).ToString();

The ExifStringToDate method takes the string value stored in the EXIF tag and converts it to an ActiveX date/time as used by C#.

We have a downloadable C# project that shows a meta data viewer. It shows the code needed to read and write IPTC and Exif data from JPEG and TIFF images.

More on writing EXIF attributes.

Summary of C# related pages.

Cookies

This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.