Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
Saving and Reading TIFF Tags and Comments
  1. Start Visual Studio .NET.
  2. Start with the project that you created in Loading and Displaying an Image
  3. Drag and drop a button onto Form1. Change the following properties:
    PropertyValue
    Name:btnCommentReadWrite
    Text:Comment Read\Write
  4. Add the following code for the btnCommentReadWrite control’s click procedure:

    Visual Basic

    
    Private Sub btnCommentReadWrite_Click(ByVal sender As System.Object, ByVal e 
       As System.EventArgs) Handles btnCommentReadWrite.Click
          'Write the following comment to the image
          Dim writeComment As RasterCommentMetadata = New RasterCommentMetadata()
          writeComment.Type = RasterCommentMetadataType.Software
          writeComment.FromAscii("LEADTOOLS Demo")
    
          codecs.WriteComment("C:\Users\Public\Documents\LEADTOOLS Images\clean.tif", 
                               1, writeComment)
    
          'Read the Comment
          Dim readComment As RasterCommentMetadata = 
             codecs.ReadComment("C:\Users\Public\Documents\LEADTOOLS Images\clean.tif", 
                                 1, RasterCommentMetadataType.Software)
                                 Console.WriteLine("The following comment has been read:")
    
          MessageBox.Show(readComment.ToAscii(), "The following comment has been read:")
    End Sub
    

    C#

    
    private void btnCommentReadWrite_Click(object sender, EventArgs e)
    {
       //Write the comment to the file
       RasterCommentMetadata writeComment = new RasterCommentMetadata();
       writeComment.Type = RasterCommentMetadataType.Software;
       writeComment.FromAscii("LEADTOOLS Demo");
    
       codecs.WriteComment(dlg.FileName, 1, writeComment);
    
       //Read The Comment
       RasterCommentMetadata readComment = 
          codecs.ReadComment(dlg.FileName, 1, RasterCommentMetadataType.Software);
    
       MessageBox.Show(readComment.ToAscii(), "The following comment has been read:");
    }
    
  5. To read or write TIFF tags, drag and drop a button onto Form1. Change the following properties:
    PropertyValue
    Name:btnReadWriteTags
    Text:Read\WriteTags
  6. Add the following code for the btnReadWriteTags control’s click procedure:

    Visual Basic

    
    Private Sub btnReadWriteTags_Click(ByVal sender As System.Object, 
                                       ByVal e As System.EventArgs) Handles btnReadWriteTags.Click
          'This code reads the Xresolution from a TIFF image and modifies the value 
           and saves it back.
          Const XresTagID As Integer = 282
          Dim tag As RasterTagMetadata
          tag = New RasterTagMetadata()
    
          Dim ReadTag As RasterTagMetadata = codecs.ReadTag(FileName, 1, XresTagID)
          Dim Rational As RasterMetadataURational() = ReadTag.ToURational()
          Rational(0).Numerator = rational(0).Numerator * 5
          Rational(0).Denominator = rational(0).Denominator * 1
          ReadTag.FromURational(rational)
          codecs.WriteTag(FileName, 1, ReadTag)
    End Sub
    

    C#

    
    private void btnReadWriteTags_Click(object sender, EventArgs e)
    {
       //This code reads the Xresolution from a TIFF image and modifies the value and saves it back.
       const int XresTagID = 282;
       RasterTagMetadata tag;
       tag = new RasterTagMetadata();
    
       RasterTagMetadata ReadTag = codecs.ReadTag(FileName, 1, XresTagID);
       RasterMetadataURational[] rational = ReadTag.ToURational();
       >Rational(0).Numerator = rational(0).Numerator * 5;
       Rational(0).Denominator = rational(0).Denominator * 1;
       ReadTag.FromURational(rational);
       codecs.WriteTag(FileName, 1, ReadTag);
    }
    

    Note: Normally you do not have to modify this Tag because LEADTOOLS automatically sets the resolution tags to the bitmap's DPI values whenever the Save method is used. This code only shows how to modify it without loading and saving the image itself.

  7. Compile and run your project.