Home Image-Pro Automation (Macros, Apps, Reports)
Options

Read MetaData from a TIFF image

Hi,
I want to read MetaData from a TIFF image. Is there a way to realize this?

br,
Friedrich

Best Answer

  • Options
    Answer ✓
    Hi Friedrich,

    Image-Pro doesn't expose API to read tiff tags, but you can use existing .NET classes, such as Bitmap to read TIFF TAGs.

    I've attached the project that reads all TIFF tags of the active image. The code looks like this:

        ''' Read all TIFF tags from active image
        Public Sub ReadTIFFTagsFromActiveImage
            Dim doc As IMcDocument=ThisApplication.ActiveDocument
            If doc Is Nothing Then Exit Sub
            Dim filename As String=doc.FileName
            ThisApplication.Output.Show
            ThisApplication.Output.Clear
            ThisApplication.Output.PrintMessage($"File name = {filename}")
            If Not System.IO.Path.GetExtension(filename).ToLower.Contains("tif") Then Exit Sub
    
            Dim bp As System.Drawing.Bitmap = New System.Drawing.Bitmap(filename)
            Dim bpProps() As System.Drawing.Imaging.PropertyItem = bp.PropertyItems
    
            Dim pName As String = String.Empty, pVal As New Object
            For Each p As System.Drawing.Imaging.PropertyItem In bpProps
                If GetPropertyDetail(p, pName, pVal) Then
                    ThisApplication.Output.PrintMessage($"Property ({pName}) = {pVal.ToString} ")
                Else
                    ThisApplication.Output.PrintMessage($"Property ({pName}), Type = {p.Type} Len = {p.Len}")
                End If
            Next
        End Sub
    
        Private Function GetPropertyDetail(ByVal p As System.Drawing.Imaging.PropertyItem, ByRef propName As String, ByRef propValue As Object) As Boolean
            Dim success As Boolean = True
            ' Hard-coded values from TIFF specification 
            Select Case p.Id
                Case 256
                    propName = "ImageWidth"
                    propValue = ExtractValue(p, True)
                Case 257
                    propName = "ImageWidth"
                    propValue = ExtractValue(p, True)
                Case 269
                    propName = "DocumentName"
                    propValue = ExtractValue(p, True)
                Case 270
                    propName = "ImageDescription"
                    propValue = ExtractValue(p, True)
                Case Else
                    propName = "unkown (Id = " & p.Id.ToString() & ")"
                    propValue = Nothing
                    success = False
            End Select
    
            Return success
    
        End Function
    
        Private Function ExtractValue(ByVal p As System.Drawing.Imaging.PropertyItem, ByVal AsSingle As Boolean) As Object
            Select Case p.Type
                Case 2  ' Null-terminated ASCII string
                    Return System.Text.Encoding.ASCII.GetString(p.Value, 0, p.Len - 1)
    
                Case 3  ' Array of unsigned short (16-bit) integers.
                    Dim ms As New System.IO.MemoryStream(p.Value)
                    Dim bReader As New System.IO.BinaryReader(ms)
                    Dim uIntList As New System.Collections.Generic.List(Of System.UInt16)
                    Do Until ms.Position >= ms.Length - 1
                        uIntList.Add(bReader.ReadUInt16)
                    Loop
                    If AsSingle Then
                        Return uIntList(0)
                    Else
                        Return uIntList.ToArray()
                    End If
    
                Case Else
                    Return ("Conversion not defined for type " & p.Type.ToString())
            End Select
        End Function
    
    The result is printed to the Output window:



    The code is based on this example. The PropertyItem class is defined here
    You can extend the macro to read your specific tags.

    Regards, 

    Yuri

Answers

  • Options
    Friedrich,

    The Metadata is available in the Image Information panel. 

    Right-Click on an open Image and select the "Information" (blue i) icon
    Select "Metadata" from the Template dropdown list.

    Best Regards,
    Scott Reed
    Media Cybernetics
  • Options
    Hi Scott,

    I want to read custom metadata programmatically. Is that possible?

    Br,
    Friedrich



  • Options
    Hi Yuri,

    thanks. It works great!

    Br,
    Friedrich
Sign In or Register to comment.