Exif example for Visual Basic

Note: Also works with Access 95 and 97.

This example does the following:

1. Reads all of the comments in an existing Exif file, updating the Comment array.

2. Loads and modifies the file's image.

3. Updates some of the comments in the comment array.

4. Saves the file.

5. Reads and displays the updated comments.

Note: Comments for GIF, TIFF, and DICOM files are simpler than the ones in this Exif example. With those file types, all of the comments are strings that you can get and set with simple assignments.

Dim SubjectDistance(1) As Long 'Array for CMNT_SUBJECTDISTANCE
Dim NewSubjectDistance As Variant 'Array to be updated by ReadComment
Dim NewDateTime As String 'String to be updated by ReadComment
Dim MyComment() As Byte 'Array for CMNT_USERCOMMENT
Dim MyCommentText As String 'String for CMNT_USERCOMMENT
Dim MyCommentSize As Integer 'Size of the comment array
Dim NewComment As Variant 'Array to be updated by ReadComment
Dim NewCommentText As String 'String for CMNT_USERCOMMENT that we read
Dim FilePath, DistanceString, ThisDate, ThisTime, MyMsg 'Miscellaneous
Dim i as Integer 'Counter
Dim ArraySize 'Size of an array

'Specify the file that we will update.
FilePath = "d:\lead\images\exif1.tif"

'Get all of the current comments from an Exif file.
'Temporarily disable method errors so that we do not fail when comments are missing.
Lead1.EnableMethodErrors = False
For i = 0 To CMNT_LAST
  Lead1.Comment(i) = Empty
  Lead1.Comment(i) = Lead1.ReadComment(FilePath, 0, i)
Next i
Lead1.EnableMethodErrors = True

'Load and modify the image.
Lead1.Load FilePath, 0, 0, 1
Lead1.Reverse

'Update the date when the file is generated.
ThisDate = Format(Date, "yyyy:mm:dd ")
ThisTime = Format(Time, "hh:mm:ss")
Lead1.Comment(CMNT_SZDATETIMEDIGITIZED) = ThisDate + ThisTime
Lead1.Comment(CMNT_SZSUBSECTIMEDIGITIZED) = Empty

'Change the subject distance to 4 1/3 meters.
SubjectDistance(0) = 13
SubjectDistance(1) = 3
Lead1.Comment(CMNT_SUBJECTDISTANCE) = SubjectDistance

'Specify the user comment string.
MyCommentText = "This is my new comment."
MyCommentSize = 8 + Len(MyCommentText)

'Define the array for the user comment.
ReDim MyComment(MyCommentSize) As Byte

'Fill in the ASCII prefix.
MyComment(0) = Asc("A")
MyComment(1) = Asc("S")
MyComment(2) = Asc("C")
MyComment(3) = Asc("I")
MyComment(4) = Asc("I")
MyComment(5) = &H0
MyComment(6) = &H0
MyComment(7) = &H0

'Fill in the rest of the comment, starting at the end.
MyComment(MyCommentSize) = &H0
For i = MyCommentSize - 1 To 8 Step -1
  MyComment(i) = Asc(Right(MyCommentText, MyCommentSize - i))
Next i

'Update the user comment.
Lead1.Comment(CMNT_USERCOMMENT) = Empty
Lead1.Comment(CMNT_USERCOMMENT) = MyComment

'Save the file and read the comments that we saved.
Lead1.Save FilePath, FILE_EXIF, 24, 0, SAVE_OVERWRITE
NewDateTime = Lead1.ReadComment(FilePath, 0, CMNT_SZDATETIMEDIGITIZED)
NewSubjectDistance = Lead1.ReadComment(FilePath, 0, CMNT_SUBJECTDISTANCE)
NewComment = Lead1.ReadComment(FilePath, 0, CMNT_USERCOMMENT)

'Interpret the Subject-Distance RATIONAL.
DistanceString = Format(NewSubjectDistance(0) / NewSubjectDistance(1), "0.000000")

'Initialize the string for the message box.
MyMsg = "Subject Distance = " + DistanceString + Chr(13) + "Date/Time = " + NewDateTime

'Add the ASCII comment if it is there
If Chr$(NewComment(0)) = "A" Then
  ArraySize = UBound(NewComment, 1) - LBound(NewComment, 1)
  NewCommentText = ""
  For i = 8 To ArraySize
    NewCommentText = NewCommentText + Chr$(NewComment(i))
  Next i
  MyMsg = MyMsg + Chr(13) + Chr(13) + NewCommentText
End If

'Display the message
MsgBox MyMsg

'Clear the comments from memory
For i = 0 To CMNT_LAST
  Lead1.Comment(i) = Empty
Next i