Reading EXIF data from Visual Basic.Net Program

Started by Keelandb, April 15, 2010, 08:03:45 PM

Previous topic - Next topic

Keelandb

As a research forest ecologist I take a lot of pictures while I am in the woods or swamps. As such I have a lot of files with nonsense names like DSCN00659 or IMG00032. It is also somewhat of a problem to keep track of where the photos were taken and what they show. As such we (myself and Mr. John McCoy whom I used to work with) have been thinking about an appropriate program for many years. Upon retirement I now have time to work on this program. I know that there are several good programs that could be used, but I have Visual Basic.Net 2003 so I'll use that. Eventually the program will allow saving information such as State, County/Parish, Location, Site, Lat/Long, Photographer, notes, common name, species, family, etc, but for now I am working on the EXIF data. Someone else had written some code for Visual Basic, but I had troubles getting it to work, so here is what I have written. Any comments or suggestions would be appreciated. When the entire program is finished I'll make it available.
Program by Bobby Keeland and John McCoy.

    Private Sub GetEXIF_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles GetEXIF.Click

        ' This is written for Visual Basic.Net 2003
        Dim GetEXIFLoc = TxtEXIF.Text    ' Get EXIF file location from TextBox
        Dim GetPhotoLoc = TxtPhoto.Text ' Get Photo location from TextBox
        ' I need to change this so that I get the photo file location
        ' from an OpenFileDialog, but that will be added later.

        ' ************************ Set start information.
        Dim start_info As New ProcessStartInfo(GetEXIFLoc + GetPhotoLoc)
        start_info.UseShellExecute = False
        start_info.CreateNoWindow = False
        start_info.RedirectStandardOutput = True
        start_info.RedirectStandardError = True

        ' Make the process and set its start information.
        Dim Proc As New Process
        Proc.StartInfo = start_info
        Proc.Start()

        ' Attach to stdout and stderr, and put into
        ' the TextBox EXIFData.Text
        Dim std_out As System.IO.StreamReader = Proc.StandardOutput()
        Dim std_err As System.IO.StreamReader = Proc.StandardError()
        ExifData.Text = std_out.ReadToEnd()
        txtstderr.Text = std_err.ReadToEnd()

        std_out.Close()              ' Clean up
        std_err.Close()
        Proc.Close()

        ' Now read the EXIF readout to determine things like the line for
        ' shutterspeed (Shutter Speed              :1/500).
        ' Now record the actual shuttershpeed (ie. the 1/500) and put it
        ' into a TextBox for later input into a database
        Dim counter As Integer
        Dim tempArray() As String
        tempArray = ExifData.Lines

        'Loop through the array and send contents to bound textboxes.
        Dim delimChar As Char = ":"
        Dim split As String() = Nothing
        Dim s As String
        Dim MyString, TrimString As String

        For counter = 0 To tempArray.GetUpperBound(0)
            If counter = 0 Then
                split = tempArray(0).Split(delimChar)
                For Each s In split
                    MyString = split(0)
                    TrimString = RTrim(MyString)
                    ' I'll have to read the string (TrimString) each time to
                    ' determine whether it is shutterspeed or something else
                    ' and from that determine which textbox to put it in
                    ' so that it goes to the right field in the database.
                    TextBox1.Text = TrimString
                    TextBox8.Text = split(1)
                Next s

            ElseIf counter = 1 Then
             '   More options to be entered for other EXIF data
             '   Ideally this would be done as a function
            End If
       Next
End Sub

Phil Harvey

This looks reasonable.  You will have to consider the possibility of values which contain a colon, so you should split the exiftool output lines only at the first colon in each line.  Also, you could avoid having to trim the whitespace if you use the exiftool -S option and split at the first colon-space combination.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).