Reading Exif/GPS data from loaded image

Questions, comments and suggestions concerning VintaSoft Imaging .NET SDK.

Moderator: Alex

communi
Posts: 12
Joined: Wed Jul 13, 2011 11:07 pm

Reading Exif/GPS data from loaded image

Post by communi »

Hi,

is there a piece of VB sample code how to read the in Exif data stored GPS data (Lat, Long, Alt etc.)of a jpg file which is currently displayed in ImageViewer and display the GPS data in a textbox etc.? The helpfile was not really helpful in this case.... ;)

Regards,
Communi
Alex
Site Admin
Posts: 2304
Joined: Thu Jul 10, 2008 2:21 pm

Re: Reading Exif/GPS data from loaded image

Post by Alex »

Hello,

Here is a snippet of code which shows how to get information about EXIF tags of JPEG file:

Code: Select all

Imports System.IO
Imports Vintasoft.Imaging
Imports Vintasoft.Imaging.Codecs.Jpeg
Imports Vintasoft.Imaging.Codecs.Tiff

Module Module1

    Sub Main()
        Using jpegStream As FileStream = New FileStream("..\..\Exif.jpg", FileMode.Open, FileAccess.Read)
            Using jpeg As JpegFile = New JpegFile(jpegStream)

                Dim exif As ExifData = jpeg.Page.Exif
                If exif Is Nothing Then
                    Console.WriteLine("JPEG file does not have EXIF data.")
                Else
                    WriteTagsData("EXIF", exif.ExifTags)
                    WriteTagsData("GPS", exif.GpsTags)
                End If

            End Using
        End Using

        Console.WriteLine("Press any key to continue...")
        Console.ReadKey()
    End Sub

    Sub WriteTagsData(ByRef tagsName As String, ByRef tags As TiffTagCollection)
        If tags.Count = 0 Then
            Console.WriteLine(String.Format("No {0} data.", tagsName))
        Else
            Console.WriteLine(String.Format("{0} data [{1}]:", tagsName, tags.Count))
            For i = 0 To tags.Count - 1
                Console.WriteLine(String.Format("- {0}", tags(i)))
            Next
        End If
        Console.WriteLine()
    End Sub

End Module
Best regards, Alexander
communi
Posts: 12
Joined: Wed Jul 13, 2011 11:07 pm

Re: Reading Exif/GPS data from loaded image

Post by communi »

Hi Alex,

I tried this code changed to WinForms code - but no luck. Instead of the GPS data in the Textbox is always only "- Vintasoft.Imaging.Codecs.Tiff.TiffTag" displayed( and the photo has GPS data). If I take a photo without geotags it recognises this correctly and displays "No GPS data". So whats wrong?
Here is the code I have used:

Code: Select all

Imports System.IO
Imports Vintasoft.Imaging
Imports Vintasoft.Imaging.Codecs.Jpeg
Imports Vintasoft.Imaging.Codecs.Tiff

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Using jpegStream As FileStream = New FileStream("..\..\geotag005.jpg", FileMode.Open, FileAccess.Read)
        Using jpeg As JpegFile = New JpegFile(jpegStream)
            Dim exif As ExifData = jpeg.Page.Exif
            If exif Is Nothing Then
                TextBox1.Text = "JPEG file does not have EXIF data."
            Else
                WriteTagsData("EXIF", exif.ExifTags)
                WriteTagsData("GPS", exif.GpsTags)
            End If
        End Using
    End Using
End Sub

Public Sub WriteTagsData(ByRef tagsName As String, ByRef tags As TiffTagCollection)
    If tags.Count = 0 Then
        TextBox1.Text = String.Format("No {0} data.", tagsName)
    Else
        TextBox1.Text = String.Format("{0} data [{1}]:", tagsName, tags.Count)
        For i = 0 To tags.Count - 1
            TextBox1.Text = String.Format("- {0}", tags(i)) & vbCrLf
        Next
    End If
End Sub
Regards
Alex
Site Admin
Posts: 2304
Joined: Thu Jul 10, 2008 2:21 pm

Re: Reading Exif/GPS data from loaded image

Post by Alex »

You need to use this code:

Code: Select all

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Using jpegStream As FileStream = New FileStream("..\..\geotag005.jpg", FileMode.Open, FileAccess.Read)
        Using jpeg As JpegFile = New JpegFile(jpegStream)

            Dim exif As ExifData = jpeg.Page.Exif
            If exif Is Nothing Then
                TextBox1.Text = "JPEG file does not have EXIF data."
            Else
                WriteTagsData("EXIF", exif.ExifTags)
                WriteTagsData("GPS", exif.GpsTags)
            End If

        End Using
    End Using
End Sub

Private Sub WriteTagsData(ByRef tagsName As String, ByRef tags As TiffTagCollection)
    If tags.Count = 0 Then
        TextBox1.Text = TextBox1.Text + String.Format("No {0} data.", tagsName) & vbCrLf
    Else
        TextBox1.Text = TextBox1.Text + String.Format("{0} data [{1}]:", tagsName, tags.Count) & vbCrLf
        Dim tag As TiffTag
        For i = 0 To tags.Count - 1
            tag = tags(i)
            TextBox1.Text = TextBox1.Text + String.Format("- Name={0}, Id={1}, Type={2}, Data={3}", tag.Name, tag.Id, tag.Type, tag.Data) & vbCrLf
        Next
    End If
    TextBox1.Text = TextBox1.Text + vbCrLf
End Sub
Best regards, Alexander
communi
Posts: 12
Joined: Wed Jul 13, 2011 11:07 pm

Re: Reading Exif/GPS data from loaded image

Post by communi »

Thanks, this works fine! But there is no "real" content, no real data displayed, only this:

GPS data [7]:
- Name=InteroperabilityIndex, Id=1, Type=Ascii, Data=N
- Name=GPSLatitude, Id=2, Type=Rational, Data=Vintasoft.Imaging.Codecs.Tiff.Rational[]
- Name=GPSLongitudeRef, Id=3, Type=Ascii, Data=E
- Name=GPSLongitude, Id=4, Type=Rational, Data=Vintasoft.Imaging.Codecs.Tiff.Rational[]
- Name=GPSAltitudeRef, Id=5, Type=Byte, Data=0
- Name=GPSAltitude, Id=6, Type=Rational, Data=83889 / 278
- Name=GPSTimeStamp, Id=7, Type=Rational, Data=Vintasoft.Imaging.Codecs.Tiff.Rational[]

Is this normal?
Alex
Site Admin
Posts: 2304
Joined: Thu Jul 10, 2008 2:21 pm

Re: Reading Exif/GPS data from loaded image

Post by Alex »

Hello,

You need to convert arrays to string.

Here is a snippet of code:

Code: Select all

Private Sub WriteTagsData(ByRef tagsName As String, ByRef tags As TiffTagCollection)
    If tags.Count = 0 Then
        TextBox1.Text = TextBox1.Text + String.Format("No {0} data.", tagsName) & vbCrLf
    Else
        TextBox1.Text = TextBox1.Text + String.Format("{0} data [{1}]:", tagsName, tags.Count) & vbCrLf
        Dim tag As TiffTag
        Dim tagData As Object
        Dim tagDataAsArray As Array
        Dim tagDataAsString As String
        ' for each tag in tag collection
        For i = 0 To tags.Count - 1
            ' get tag
            tag = tags(i)

            ' get tag data
            tagData = tag.Data
            ' if tag data is array
            If IsArray(tagData) Then
                tagDataAsString = "[ "
                tagDataAsArray = CType(tagData, Array)
                For j = 0 To tagDataAsArray.Length - 1
                    tagDataAsString = tagDataAsString + tagDataAsArray(j).ToString + "; "
                Next
                tagDataAsString = tagDataAsString + "]"

            ' if tag data is NOT array
            Else
                tagDataAsString = tagData.ToString()
            End If

            ' add information about tag to textbox
            TextBox1.Text = TextBox1.Text + String.Format("- Name={0}, Id={1}, Type={2}, Data={3}", tag.Name, tag.Id, tag.Type, tagDataAsString) & vbCrLf
        Next
    End If
    TextBox1.Text = TextBox1.Text + vbCrLf
End Sub
Best regards, Alexander
communi
Posts: 12
Joined: Wed Jul 13, 2011 11:07 pm

Re: Reading Exif/GPS data from loaded image

Post by communi »

Hi Alex,

now it works great!! Thank you! I only have to find a way to convert the data values to a human readable format ;) Any ideas? :D

But one question. The Name of the first data is "InteroperabilityIndex" but the correct name should be "GPSLatitudeRef", why the code doesn't recognize this? The exif data in the image are correct, other software shows this name correctly as "GPSLatitudeRef"
GPS data [7]:
- Name=InteroperabilityIndex, Id=1, Type=Ascii, Data=N
- Name=GPSLatitude, Id=2, Type=Rational, Data=[ xxxxx ]
- Name=GPSLongitudeRef, Id=3, Type=Ascii, Data=E
- Name=GPSLongitude, Id=4, Type=Rational, Data=[xxxxx]
- Name=GPSAltitudeRef, Id=5, Type=Byte, Data=0
- Name=GPSAltitude, Id=6, Type=Rational, Data=83889 / 278
- Name=GPSTimeStamp, Id=7, Type=Rational, Data=[ 15 / 1; 47 / 1; 32 / 1; ]
Alex
Site Admin
Posts: 2304
Joined: Thu Jul 10, 2008 2:21 pm

Re: Reading Exif/GPS data from loaded image

Post by Alex »

We have fixed bug in version 4.3.35.5 of SDK and now your code will work correctly.

But the following code is better:

Code: Select all

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Using jpegStream As FileStream = New FileStream("..\..\geotag005.jpg", FileMode.Open, FileAccess.Read)
        Using jpeg As JpegFile = New JpegFile(jpegStream)

            Dim exif As ExifData = jpeg.Page.Exif
            If exif Is Nothing Then
                TextBox1.Text = "JPEG file does not have EXIF data."
            Else
                WriteTagsData("EXIF", exif.ExifTags, GetType(ExifTagId))
                WriteTagsData("GPS", exif.GpsTags, GetType(GpsTagId))
            End If

        End Using
    End Using
End Sub

Private Sub WriteTagsData(ByRef tagsName As String, ByRef tags As TiffTagCollection, ByRef tagIdEnumType As Type)
    If tags.Count = 0 Then
        TextBox1.Text = TextBox1.Text + String.Format("No {0} data.", tagsName) & vbCrLf
    Else
        TextBox1.Text = TextBox1.Text + String.Format("{0} data [{1}]:", tagsName, tags.Count) & vbCrLf
        Dim tag As TiffTag
        Dim tagData As Object
        Dim tagDataAsArray As Array
        Dim tagDataAsString As String
        ' for each tag in tag collection
        For i = 0 To tags.Count - 1
            ' get tag
            tag = tags(i)

            ' get tag data
            tagData = tag.Data
            ' if tag data is array
            If IsArray(tagData) Then

                tagDataAsString = "[ "
                tagDataAsArray = CType(tagData, Array)
                For j = 0 To tagDataAsArray.Length - 1
                    tagDataAsString = tagDataAsString + tagDataAsArray(j).ToString + "; "
                Next
                tagDataAsString = tagDataAsString + "]"

                ' if tag data is NOT array
            Else
                tagDataAsString = tagData.ToString()
            End If

            ' add information about tag to textbox
            TextBox1.Text = TextBox1.Text + String.Format("- Name={0}, Id={1}, Type={2}, Data={3}", tag.GetName(tagIdEnumType), tag.Id, tag.Type, tagDataAsString) & vbCrLf
        Next
    End If
    TextBox1.Text = TextBox1.Text + vbCrLf
End Sub
Best regards, Alexander
communi
Posts: 12
Joined: Wed Jul 13, 2011 11:07 pm

Re: Reading Exif/GPS data from loaded image

Post by communi »

Alex,
Thank You!!! :) Now it works fine with the latest version, great job!!!!!!
js95007
Posts: 7
Joined: Wed Feb 01, 2012 9:28 am

Re: Reading Exif/GPS data from loaded image

Post by js95007 »

Hey guys don't know if this post is still active, but still worth a shot
I have tried to use the latest code Alex gave, but ended up with three errors.
These probably where trivial errors on my part (I'm something of a novice ;) ) but I haven't been able to solve it, so here goes:

Using jpeg As JpegFile = New JpegFile(jpegStream) --- "jpeg is not defined"
Private Sub WriteTagsData(ByRef tagsName As String, ByRef tags As TiffTagCollection, ByRef tagIdEnumType As Type) --- "TiffTagCollection is not defined"
Dim tag As TiffTag --- "TiffTag is not defined"

Hope you can shed some light, I have spent the last three days endlessly searching for how to extract GPS info from an image in Visual Basic :?
Thanks for any help you can give, it will be greatly appreciated
Post Reply