Visual Basic: Get image metadata

Platform:Visual Basic
Task:Get image metadata
This whole topic of image metadata is baffling and definitely needs improvement but all I needed to do was get the date and time a photo was taken and this is what I came up with.
    ''' <summary>
    ''' Retrieves metadata from an image according to the property item submitted.  
    ''' </summary>
    ''' <param name="ImageFile">The file to examine</param>
    ''' <param name="PropertyItemNumber">The metadata parameter to return. The most useful ones are 1=Camera, 6=Capture Date</param>
    ''' <returns>String</returns>
    ''' <remarks>Example: Dim MyImageCaptureDate as String = GetImageMetadata("C:\MyImage.jpg",6).  
    ''' See for more information regarding metadata property items.</remarks>
    Public Function GetImageMetadata(ByVal ImageFile As String, Optional ByVal PropertyItemNumber As Integer = 6) As String
        'make sure we get good input
        If ImageFile Is Nothing Or IsNothing(PropertyItemNumber) Then
            Return Nothing
            Exit Function
            If My.Computer.FileSystem.FileExists(ImageFile) = True And IsNumeric(PropertyItemNumber) = True Then
                'Create an Image object. 
                Dim MyImageFile As Image = Image.FromFile(ImageFile)
                'Get the PropertyItems property from image.
                Dim PropItems As System.Drawing.Imaging.PropertyItem() = MyImageFile.PropertyItems
                'Convert the value of the propertyitemnumber (6 is capturedate) to a string, and display it.
                Dim Encoding As New System.Text.ASCIIEncoding()
                Dim Metadata As String = Nothing
                    Metadata = Encoding.GetString(PropItems(PropertyItemNumber).Value)
                Catch ioorex As IndexOutOfRangeException
                    Return ""
                    Exit Function
                Catch ex As Exception
                    Return ""
                    Exit Function
                End Try

                'the dates in the metadata are funky having colons instead of slashes for the first part of the date, fix it here.  Example: 2010:05:25 10:25:49 will go to 2010\05\25 10:25:49
                If PropertyItemNumber = 6 Or PropertyItemNumber = 13 Or PropertyItemNumber = 14 Then
                    'dates have colons instead of slashes: fix the date 
                    Metadata = Replace(Metadata, ":", "/", 1, 2)
                End If
                Return Metadata
                Return Nothing
                Exit Function
            End If
        End If
    End Function
Back to Index