Reading EXIF metadata in ASP with csImageFile

The ASP component csImageFile can be used to read and write EXIF data that is stored in JPEG and TIFF images. This data is usually written at the time that the image is created, usually by the camera or scanner. Here we will show how all the supported EXIF tags can be read and also demonstrate how some tag values can be converted into meaningful values. The code fragments shown below use ASP with VBScript, although other COM enabled languages can be used.

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

The code shown below will display the name of each EXIF tag stored in the file, together with its value. It assumes that the csImageFile object has been created with a name Image and a JPG or TIFF image has been loaded.

If Image.ExifCount = 0 Then
  Response.Write "<p>No EXIF data in the image.</p>"
  Response.Write "<p>"
  For I = 0 to Image.ExifCount - 1
    Response.Write Image.ExifName(I) & ": " & Image.ExifValueByIndex(I) & "<br />"
  Response.Write "</p>"
End If

csImageFile 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.

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:

Response.Write Image.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

csImageFile 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 csImageFile 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:

Response.Write Image.RationalToReal(Image.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:

Response.Write Image.ExifStringToDate(Image.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 VBScript.

We have an online example where you can upload a JPG image and view the EXIF data.

Click here for more on writing EXIF attributes.