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

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 ImageBox1.ExifCount > 0 Then
  For I = 0 To ImageBox1.ExifCount - 1
    List1.AddItem (ImageBox1.ExifName(I) & ": " & ImageBox1.ExifValueByIndex(I))
  Next
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

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.Caption = ImageBox1.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.Caption = ImageBox1.RationalToReal(ImageBox1.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.Caption ImageBox1.ExifStringToDate(ImageBox1.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 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 related pages.