Leadtools.Codecs Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
ReadComment(String,Int32,RasterCommentMetadataType) Method
See Also  Example
Leadtools.Codecs Namespace > RasterCodecs Class > ReadComment Method : ReadComment(String,Int32,RasterCommentMetadataType) Method



fileName
A String containing the input file name.
pageNumber
1-based index of the page from which to read the comment.
type
The type of comment. Refer to Types of File Comments.
Gets a comment field from a file.

Syntax

Visual Basic (Declaration) 
Public Overloads Function ReadComment( _
   ByVal fileName As String, _
   ByVal pageNumber As Integer, _
   ByVal type As RasterCommentMetadataType _
) As RasterCommentMetadata
Visual Basic (Usage)Copy Code
Dim instance As RasterCodecs
Dim fileName As String
Dim pageNumber As Integer
Dim type As RasterCommentMetadataType
Dim value As RasterCommentMetadata
 
value = instance.ReadComment(fileName, pageNumber, type)
C# 
public RasterCommentMetadata ReadComment( 
   string fileName,
   int pageNumber,
   RasterCommentMetadataType type
)
C++/CLI 
public:
RasterCommentMetadata ReadComment( 
   String^ fileName,
   int pageNumber,
   RasterCommentMetadataType type
) 

Parameters

fileName
A String containing the input file name.
pageNumber
1-based index of the page from which to read the comment.
type
The type of comment. Refer to Types of File Comments.

Return Value

A RasterCommentMetadata object containing the comment field information. If no such comment is found in the file, this method will return a null reference (Nothing in Visual Basic).

Example

This example demonstrates all of the methods related to comments for TIFF files. It saves a few comments to a file before loading them back

Visual BasicCopy Code
Public Sub CommentsExample()
   RasterCodecs.Startup()
   Dim codecs As RasterCodecs = New RasterCodecs()

   Dim srcFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"
   Dim destFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_Comments.tif"

   ' Convert the source file to TIF
   Console.WriteLine("Converting the source file to TIF")
   codecs.Convert(srcFileName, destFileName, RasterImageFormat.Tif, 0, 0, 24, Nothing)

   ' Add the artist comment
   Dim writeComment As RasterCommentMetadata = New RasterCommentMetadata()
   writeComment.Type = RasterCommentMetadataType.Artist
   writeComment.FromAscii("LEADTOOLS")

   Console.WriteLine("Writing the following comment:")
   Console.WriteLine(" Type:{0}, Data:{1}", writeComment.Type, writeComment.ToAscii())

   codecs.WriteComment(destFileName, 1, writeComment)

   ' Read the comment back
   Dim readComment As RasterCommentMetadata = codecs.ReadComment(destFileName, 1, RasterCommentMetadataType.Artist)
   Console.WriteLine("The following comment has been read:")
   Console.WriteLine(" Type:{0}, Data:{1}", readComment.Type, readComment.ToAscii())

   ' Write a few comments to the file in one pass
   Dim comments As RasterCollection(Of RasterCommentMetadata) = New RasterCollection(Of RasterCommentMetadata)()
   writeComment = New RasterCommentMetadata()
   writeComment.Type = RasterCommentMetadataType.Artist
   writeComment.FromAscii("LEADTOOLS Again")
   comments.Add(writeComment)

   writeComment = New RasterCommentMetadata()
   writeComment.Type = RasterCommentMetadataType.Copyright
   writeComment.FromAscii("(c) 2006")
   comments.Add(writeComment)

   Console.WriteLine("Writing the following comments to the file:")
   For Each comment As RasterCommentMetadata In comments
      Console.WriteLine(" Type:{0}, Data:{1}", comment.Type, comment.ToAscii())
   Next comment

   codecs.WriteComments(destFileName, 1, comments)

   ' Now get all the comments in the file and show them:
   Console.WriteLine("Reading all comments from the file:")

   Dim tifComments As RasterCommentMetadataType() = _
   { _
      RasterCommentMetadataType.Artist, _
      RasterCommentMetadataType.Copyright, _
      RasterCommentMetadataType.DateTime, _
      RasterCommentMetadataType.Description, _
      RasterCommentMetadataType.HostComputer, _
      RasterCommentMetadataType.Make, _
      RasterCommentMetadataType.Model, _
      RasterCommentMetadataType.NameOfDocument, _
      RasterCommentMetadataType.NameOfPage, _
      RasterCommentMetadataType.Software _
   }

   For Each tifComment As RasterCommentMetadataType In tifComments
      Dim comment As RasterCommentMetadata = codecs.ReadComment(destFileName, 1, tifComment)
      If Not comment Is Nothing Then
         Console.Write("Found comment, Type:{0}, Data:", comment.Type)

         Dim dataType As RasterCommentMetadataDataType = RasterCommentMetadata.GetDataType(comment.Type)

         Dim byteData As Byte()
         Dim shortData As Short()
         Dim rationalData As RasterMetadataRational()
         Dim urationalData As RasterMetadataURational()

         Select Case dataType
            Case RasterCommentMetadataDataType.Ascii
               Console.WriteLine(comment.ToAscii())

            Case RasterCommentMetadataDataType.Byte
               byteData = comment.ToByte()
               Dim i As Integer = 0
               Do While i < byteData.Length
                  Console.Write("{0:X} ", byteData(i))
                  i += 1
               Loop
               Console.WriteLine()

            Case RasterCommentMetadataDataType.Int16
               shortData = comment.ToInt16()
               Dim i As Integer = 0
               Do While i < shortData.Length
                  Console.Write("{0:X} ", shortData(i))
                  i += 1
               Loop
               Console.WriteLine()

            Case RasterCommentMetadataDataType.Rational
               rationalData = comment.ToRational()
               Dim i As Integer = 0
               Do While i < rationalData.Length
                  Console.Write("{0}/{1) ", rationalData(i).Numerator, rationalData(i).Denominator)
                  i += 1
               Loop
               Console.WriteLine()

            Case RasterCommentMetadataDataType.URational
               urationalData = comment.ToURational()
               Dim i As Integer = 0
               Do While i < urationalData.Length
                  Console.Write("{0}/{1) ", urationalData(i).Numerator, urationalData(i).Denominator)
                  i += 1
               Loop
               Console.WriteLine()
         End Select
      End If
   Next tifComment

   ' Clean up
   codecs.Dispose()
   RasterCodecs.Shutdown()
End Sub
C#Copy Code
public void CommentsExample() 

   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; 
   string destFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_Comments.tif"; 
 
   // Convert the source file to TIF 
   Console.WriteLine("Converting the source file to TIF"); 
   codecs.Convert(srcFileName, destFileName, RasterImageFormat.Tif, 0, 0, 24, null); 
 
   // Add the artist comment 
   RasterCommentMetadata writeComment = new RasterCommentMetadata(); 
   writeComment.Type = RasterCommentMetadataType.Artist; 
   writeComment.FromAscii("LEADTOOLS"); 
 
   Console.WriteLine("Writing the following comment:"); 
   Console.WriteLine(" Type:{0}, Data:{1}", writeComment.Type, writeComment.ToAscii()); 
 
   codecs.WriteComment(destFileName, 1, writeComment); 
 
   // Read the comment back 
   RasterCommentMetadata readComment = codecs.ReadComment(destFileName, 1, RasterCommentMetadataType.Artist); 
   Console.WriteLine("The following comment has been read:"); 
   Console.WriteLine(" Type:{0}, Data:{1}", readComment.Type, readComment.ToAscii()); 
 
   // Write a few comments to the file in one pass 
   RasterCollection<RasterCommentMetadata> comments = new RasterCollection<RasterCommentMetadata>(); 
   writeComment = new RasterCommentMetadata(); 
   writeComment.Type = RasterCommentMetadataType.Artist; 
   writeComment.FromAscii("LEADTOOLS Again"); 
   comments.Add(writeComment); 
 
   writeComment = new RasterCommentMetadata(); 
   writeComment.Type = RasterCommentMetadataType.Copyright; 
   writeComment.FromAscii("(c) 2006"); 
   comments.Add(writeComment); 
 
   Console.WriteLine("Writing the following comments to the file:"); 
   foreach(RasterCommentMetadata comment in comments) 
      Console.WriteLine(" Type:{0}, Data:{1}", comment.Type, comment.ToAscii()); 
 
   codecs.WriteComments(destFileName, 1, comments); 
 
   // Now get all the comments in the file and show them: 
   Console.WriteLine("Reading all comments from the file:"); 
 
   RasterCommentMetadataType[] tifComments = 
   { 
      RasterCommentMetadataType.Artist, 
      RasterCommentMetadataType.Copyright, 
      RasterCommentMetadataType.DateTime, 
      RasterCommentMetadataType.Description, 
      RasterCommentMetadataType.HostComputer, 
      RasterCommentMetadataType.Make, 
      RasterCommentMetadataType.Model, 
      RasterCommentMetadataType.NameOfDocument, 
      RasterCommentMetadataType.NameOfPage, 
      RasterCommentMetadataType.Software, 
   }; 
 
   foreach(RasterCommentMetadataType tifComment in tifComments) 
   { 
      RasterCommentMetadata comment = codecs.ReadComment(destFileName, 1, tifComment); 
      if(comment != null) 
      { 
         Console.Write("Found comment, Type:{0}, Data:", comment.Type); 
 
         RasterCommentMetadataDataType dataType = RasterCommentMetadata.GetDataType(comment.Type); 
 
         byte[] byteData; 
         short[] shortData; 
         RasterMetadataRational[] rationalData; 
         RasterMetadataURational[] urationalData; 
 
         switch(dataType) 
         { 
            case RasterCommentMetadataDataType.Ascii: 
               Console.WriteLine(comment.ToAscii()); 
               break; 
 
            case RasterCommentMetadataDataType.Byte: 
               byteData = comment.ToByte(); 
               for(int i = 0; i < byteData.Length; i++) 
                  Console.Write("{0:X} ", byteData[i]); 
               Console.WriteLine(); 
               break; 
 
            case RasterCommentMetadataDataType.Int16: 
               shortData = comment.ToInt16(); 
               for(int i = 0; i < shortData.Length; i++) 
                  Console.Write("{0:X} ", shortData[i]); 
               Console.WriteLine(); 
               break; 
 
            case RasterCommentMetadataDataType.Rational: 
               rationalData = comment.ToRational(); 
               for(int i = 0; i < rationalData.Length; i++) 
                  Console.Write(@"{0}/{1) ", rationalData[i].Numerator, rationalData[i].Denominator); 
               Console.WriteLine(); 
               break; 
 
            case RasterCommentMetadataDataType.URational: 
               urationalData = comment.ToURational(); 
               for(int i = 0; i < urationalData.Length; i++) 
                  Console.Write(@"{0}/{1) ", urationalData[i].Numerator, urationalData[i].Denominator); 
               Console.WriteLine(); 
               break; 
         } 
      } 
   } 
 
   // Clean up 
   codecs.Dispose(); 
   RasterCodecs.Shutdown(); 
}

Remarks

Some file formats can contain comments, and some cannot, and each file format has its own set of comment types. When you save a file, the comments in the RasterImage object can be saved in the file. The index into the array (specified using a constant) determines the type of comment.

You can use RasterCodecs.CommentsSupported to check if a certain file format supports tags.

To read all the comments stored in a file, use RasterCodecs.ReadComments(String,Int32).

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also