LEADTOOLS Image File Support (Leadtools.Codecs assembly) Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.29
ReadTags(String,Int32) Method
See Also 
Leadtools.Codecs Namespace > RasterCodecs Class > ReadTags Method : ReadTags(String,Int32) Method



fileName
A System.String containing the input file name.
pageNumber
1-based index of the page from which to read the tags.
fileName
A System.String containing the input file name.
pageNumber
1-based index of the page from which to read the tags.
Reads all the tags stored in a TIFF file.

Syntax

Visual Basic (Declaration) 
Overloads Public Function ReadTags( _
   ByVal fileName As String, _
   ByVal pageNumber As Integer _
) As RasterCollection(Of RasterTagMetadata)
Visual Basic (Usage)Copy Code
Dim instance As RasterCodecs
Dim fileName As String
Dim pageNumber As Integer
Dim value As RasterCollection(Of RasterTagMetadata)
 
value = instance.ReadTags(fileName, pageNumber)
C++/CLI 
public:
RasterCollection<RasterTagMetadata^>^ ReadTags( 
   String^ fileName,
   int pageNumber
) 

Parameters

fileName
A System.String containing the input file name.
pageNumber
1-based index of the page from which to read the tags.

Return Value

A collection of Leadtools.RasterTagMetadata containing all the tags found in the file. If the file does not contain any tags, an empty collection will be returned. If the file format does not support tags, an exception will be thrown.

Example

This example will load all the metadata (tags, geo-keys and comments) found in a disk file.

Visual BasicCopy Code
Public Sub MetadataLoadExample()
   ' Prompt the user for an image file
   Dim imageFileName As String = PromptForFileName()
   ' Initialize LEADTOOLS
   Using codecs As New RasterCodecs()
      ' Get the file format
      Dim format As RasterImageFormat

      Using info As CodecsImageInfo = codecs.GetInformation(imageFileName, False)
         format = info.Format
      End Using

      ' Load the tags
      Dim tags As RasterCollection(Of RasterTagMetadata) = Nothing
      If RasterCodecs.TagsSupported(format) Then
         tags = codecs.ReadTags(imageFileName, 1)
      End If

      ' Load the comments
      Dim comments As RasterCollection(Of RasterCommentMetadata) = Nothing
      If RasterCodecs.CommentsSupported(format) Then
         comments = codecs.ReadComments(imageFileName, 1)
      End If

      ' Load the geo keys
      Dim geoKeys As RasterCollection(Of RasterTagMetadata) = Nothing
      If RasterCodecs.GeoKeysSupported(format) Then
         geoKeys = codecs.ReadGeoKeys(imageFileName, 1)
      End If

      ' Load the markers
      Dim markers As RasterCollection(Of RasterMarkerMetadata) = Nothing
      If RasterCodecs.MarkersSupported(format) Then
         markers = codecs.ReadMarkers(imageFileName)
      End If

      Dim txtFileName As String = Path.Combine( _
         Path.GetDirectoryName(imageFileName), _
         Path.GetFileNameWithoutExtension(imageFileName) + "_metadata.txt")

      Using writer As StreamWriter = File.CreateText(txtFileName)
         ' Write the tags
         WriteTags(writer, "Tags", tags)

         ' Write the comments
         WriteComments(writer, comments)

         ' Write the geo keys (tags and geokeys use the same data type)
         WriteTags(writer, "GeoKeys", geoKeys)

         ' Write the markers
         WriteMarkers(writer, markers)
      End Using

      ' Show the text file we created
      System.Diagnostics.Process.Start(txtFileName)
   End Using
End Sub

Private Shared Sub WriteTags(ByVal writer As StreamWriter, ByVal name As String, ByVal tags As RasterCollection(Of RasterTagMetadata))
   writer.WriteLine("{0}:", name)

   If Not IsNothing(tags) Then
      For Each tag As RasterTagMetadata In tags
         writer.WriteLine("Id: 0x{0}, data length: {1}", tag.Id.ToString("X"), tag.GetData().Length)
      Next
   Else
      writer.WriteLine("Not supported")
   End If

   writer.WriteLine()
End Sub

Private Shared Sub WriteComments(ByVal writer As StreamWriter, ByVal comments As RasterCollection(Of RasterCommentMetadata))
   writer.WriteLine("Comments:")

   If Not IsNothing(comments) Then
      For Each comment As RasterCommentMetadata In comments
         writer.WriteLine("Type: {0}, data length: {1}", comment.Type, comment.GetData().Length)
      Next
   Else
      writer.WriteLine("Not supported")
   End If

   writer.WriteLine()
End Sub

Private Shared Sub WriteMarkers(ByVal writer As StreamWriter, ByVal markers As RasterCollection(Of RasterMarkerMetadata))
   writer.WriteLine("Comments:")

   If Not IsNothing(markers) Then
      For Each marker As RasterMarkerMetadata In markers
         writer.WriteLine("ID: {0}, data length: {1}", marker.Id, marker.GetData().Length)
      Next
   Else
      writer.WriteLine("Not supported")
   End If

   writer.WriteLine()
End Sub
C#Copy Code
public void MetadataLoadExample()
{
   // Prompt the user for an image file
   string imageFileName = PromptForFileName();
   // Initialize LEADTOOLS
   using(RasterCodecs codecs = new RasterCodecs())
   {
      // Get the file format
      RasterImageFormat format;

      using(CodecsImageInfo info = codecs.GetInformation(imageFileName, false))
      {
         format = info.Format;
      }

      // Load the tags
      RasterCollection<RasterTagMetadata> tags = null;
      if(RasterCodecs.TagsSupported(format))
         tags = codecs.ReadTags(imageFileName, 1);

      // Load the comments
      RasterCollection<RasterCommentMetadata> comments = null;
      if(RasterCodecs.CommentsSupported(format))
         comments = codecs.ReadComments(imageFileName, 1);

      // Load the geo keys
      RasterCollection<RasterTagMetadata> geoKeys = null;
      if(RasterCodecs.GeoKeysSupported(format))
         geoKeys = codecs.ReadGeoKeys(imageFileName, 1);

      // Load the markers
     RasterCollection<RasterMarkerMetadata> markers = null;
     if (RasterCodecs.MarkersSupported(format))
         markers = codecs.ReadMarkers(imageFileName);

      string txtFileName = Path.Combine(
         Path.GetDirectoryName(imageFileName),
         Path.GetFileNameWithoutExtension(imageFileName) + "_metadata.txt");

      using(StreamWriter writer = File.CreateText(txtFileName))
      {
         // Write the tags
         WriteTags(writer, "Tags", tags);

         // Write the comments
         WriteComments(writer, comments);

         // Write the geo keys (tags and geokeys use the same data type)
         WriteTags(writer, "GeoKeys", geoKeys);

         // Write the markers
         WriteMarkers(writer, markers);
      }

      // Show the text file we created
      System.Diagnostics.Process.Start(txtFileName);
   }
}

private static void WriteTags(StreamWriter writer, string name, RasterCollection<RasterTagMetadata> tags)
{
   writer.WriteLine("{0}:", name);

   if(tags != null)
   {
      foreach(RasterTagMetadata tag in tags)
      {
         writer.WriteLine("Id: 0x{0}, data length: {1}", tag.Id.ToString("X"), tag.GetData().Length);
      }
   }
   else
   {
      writer.WriteLine("Not supported");
   }

   writer.WriteLine();
}

private static void WriteComments(StreamWriter writer, RasterCollection<RasterCommentMetadata> comments)
{
   writer.WriteLine("Comments:");

   if(comments != null)
   {
      foreach(RasterCommentMetadata comment in comments)
      {
         writer.WriteLine("Type: {0}, data length: {1}", comment.Type, comment.GetData().Length);
      }
   }
   else
   {
      writer.WriteLine("Not supported");
   }

   writer.WriteLine();
}

private static void WriteMarkers(StreamWriter writer, RasterCollection<RasterMarkerMetadata> markers)
{
   writer.WriteLine("Markers:");

   if (markers != null)
   {
      foreach (RasterMarkerMetadata marker in markers)
      {
         writer.WriteLine("ID: {0}, data length: {1}", marker.Id, marker.GetData().Length);
      }
   }
   else
   {
      writer.WriteLine("Not supported");
   }

   writer.WriteLine();
}
SilverlightCSharpCopy Code
public void MetadataLoadExample(Stream inStreamImage, StreamWriter outStreamText)
{
   // Initialize LEADTOOLS
   RasterCodecs codecs = new RasterCodecs();
   {
      // Get the file format
      RasterImageFormat format;
      CodecsImageInfo info = codecs.GetInformation(inStreamImage, false);
      {
         format = info.Format;
      }

      // Load the tags
      RasterCollection<RasterTagMetadata> tags = null;
      if(RasterCodecs.TagsSupported(format))
         tags = codecs.ReadTags(inStreamImage, 1);

      // Load the comments
      RasterCollection<RasterCommentMetadata> comments = null;
      if(RasterCodecs.CommentsSupported(format))
         comments = codecs.ReadComments(inStreamImage, 1);

      // Load the geo keys
      RasterCollection<RasterTagMetadata> geoKeys = null;
      if(RasterCodecs.GeoKeysSupported(format))
         geoKeys = codecs.ReadGeoKeys(inStreamImage, 1);

      // Write the tags
      WriteTags(outStreamText, "Tags", tags);

      // Write the comments
      WriteComments(outStreamText, comments);

      // Write the geo keys (tags and geokeys use the same data type)
      WriteTags(outStreamText, "GeoKeys", geoKeys);
   }
}

private static void WriteTags(StreamWriter writer, string name, RasterCollection<RasterTagMetadata> tags)
{
   writer.WriteLine("{0}:", name);

   if(tags != null)
   {
      foreach(RasterTagMetadata tag in tags)
      {
         writer.WriteLine("Id: 0x{0}, data length: {1}", tag.Id.ToString("X"), tag.GetData().Length);
      }
   }
   else
   {
      writer.WriteLine("Not supported");
   }

   writer.WriteLine();
}

private static void WriteComments(StreamWriter writer, RasterCollection<RasterCommentMetadata> comments)
{
   writer.WriteLine("Comments:");

   if(comments != null)
   {
      foreach(RasterCommentMetadata comment in comments)
      {
         writer.WriteLine("Type: {0}, data length: {1}", comment.Type, comment.GetData().Length);
      }
   }
   else
   {
      writer.WriteLine("Not supported");
   }

   writer.WriteLine();
}
SilverlightVBCopy Code
Public Sub MetadataLoadExample(ByVal inStreamImage As Stream, ByVal outStreamText As StreamWriter)
   ' Initialize LEADTOOLS
   Dim codecs As RasterCodecs = New RasterCodecs()
      ' Get the file format
      Dim format As RasterImageFormat
      Dim info As CodecsImageInfo = codecs.GetInformation(inStreamImage, False)
         format = info.Format

      ' Load the tags
      Dim tags As RasterCollection(Of RasterTagMetadata) = Nothing
      If RasterCodecs.TagsSupported(format) Then
         tags = codecs.ReadTags(inStreamImage, 1)
      End If

      ' Load the comments
      Dim comments As RasterCollection(Of RasterCommentMetadata) = Nothing
      If RasterCodecs.CommentsSupported(format) Then
         comments = codecs.ReadComments(inStreamImage, 1)
      End If

      ' Load the geo keys
      Dim geoKeys As RasterCollection(Of RasterTagMetadata) = Nothing
      If RasterCodecs.GeoKeysSupported(format) Then
         geoKeys = codecs.ReadGeoKeys(inStreamImage, 1)
      End If

      ' Write the tags
      WriteTags(outStreamText, "Tags", tags)

      ' Write the comments
      WriteComments(outStreamText, comments)

      ' Write the geo keys (tags and geokeys use the same data type)
      WriteTags(outStreamText, "GeoKeys", geoKeys)
End Sub

Private Shared Sub WriteTags(ByVal writer As StreamWriter, ByVal name As String, ByVal tags As RasterCollection(Of RasterTagMetadata))
   writer.WriteLine("{0}:", name)

   If Not tags Is Nothing Then
      For Each tag As RasterTagMetadata In tags
         writer.WriteLine("Id: 0x{0}, data length: {1}", tag.Id.ToString("X"), tag.GetData().Length)
      Next tag
   Else
      writer.WriteLine("Not supported")
   End If

   writer.WriteLine()
End Sub

Private Shared Sub WriteComments(ByVal writer As StreamWriter, ByVal comments As RasterCollection(Of RasterCommentMetadata))
   writer.WriteLine("Comments:")

   If Not comments Is Nothing Then
      For Each comment As RasterCommentMetadata In comments
         writer.WriteLine("Type: {0}, data length: {1}", comment.Type, comment.GetData().Length)
      Next comment
   Else
      writer.WriteLine("Not supported")
   End If

   writer.WriteLine()
End Sub

Remarks

To read a specific tag stored in a file, use ReadTag(String,Int32,Int32) and to enumerate all the tag ids (but not the data) stored in a file use EnumTags(String,Int32).

This method will throw an exception if the file format does not support tags. To check if a file format supports tags, use RasterCodecs.TagsSupported. You can also automatically load all the tags stored in a file during a load operation by setting the CodecsLoadOptions.Tags property to true. The tags data will be stored in the result image RasterImage.Tags collection.

To load all the tags stored in a stream containing the image data, use ReadTags(Stream,Int32).

Requirements

Target Platforms: Silverlight, Windows XP, Windows Server 2003 family, Windows Server 2008 family, Windows Vista, Windows 7, MAC OS/X (Intel Only)

See Also