Reading EXIF data with Visual Basic .NET 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 VB.NET. 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 VB.NET. More on getting started with csXImage in VB.NET.

Using a For..Next 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 Then
  For I = 0 To AxImageBox1.ExifCount - 1
    ListBox1.Items.Add(AxImageBox1.get_ExifName(I) & ": " &_
End If

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:

Label1.Text = AxImageBox1.RationalToReal(AxImageBox1.get_ExifValueByName("ShutterSpeedValue"))

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"))

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

We have a downloadable VB.NET 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 VB.NET related pages.