Saving and Reading TIFF Tags and Comments

Take the following steps to start a project and to add some code that reads and writes comments and tags:

  1. Start Visual Studio .NET.
  2. Choose File->New->Project… from the menu.
  3. In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
  4. Type the project name as "Saving and Reading Tiff Tags and Comments" in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK.
  5. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to the "<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32 "folder and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.Codecs.Fax.dll
    • Leadtools.Codecs.Tif.dll

    Click the Select button and then press the OK button to add the above DLLs to the application.

  6. Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file:

    C#
    VB
    Imports Leadtools 
    Imports Leadtools.Codecs 
                     
         
    using Leadtools; 
    using Leadtools.Codecs; 
                     
         

  7. Add an event handler to the Form1 Load event and add the following code:

    C#
    VB
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
       ' Initialize a new RasterCodecs object 
       codecs = New RasterCodecs() 
    End Sub 
                     
         
    private void Form1_Load(object sender, System.EventArgs e) 
    { 
       // Initialize a new RasterCodecs object 
       codecs = new RasterCodecs(); 
    } 
                     
         

  8. Add the following variable to the Form1 class:

    ' the RasterCodecs object for loading/saving images 
    Private codecs As RasterCodecs 
                     
         
    // the RasterCodecs object for loading/saving images 
    private RasterCodecs codecs; 
                     
         

  9. Drag and drop a button onto Form1. Change the following properties:

Property Value
Name: btnCommentReadWrite
Text: Comment Read\Write
  1. Add the following code for the btnCommentReadWrite control’s click procedure:

    VB

    Private Sub btnCommentReadWrite_Click(ByVal sender As System.Object, ByVal e  
      Dim ofd As OpenFileDialog = New OpenFileDialog() 
      Try 
         ofd.Filter = "Tif files (*.tif)|*.tif|All files (*.*)|*.*" 
         If ofd.ShowDialog() = DialogResult.OK Then 
            'Write the comment to the file 
            Dim writeComment As RasterCommentMetadata = New RasterCommentMetadata() 
            writeComment.Type = RasterCommentMetadataType.Software 
            writeComment.FromAscii("LEADTOOLS Demo") 
            codecs.WriteComment(ofd.FileName, 1, writeComment) 
            'Read The Comment 
            Dim readComment As RasterCommentMetadata = codecs.ReadComment(ofd.FileName, 1, RasterCommentMetadataType.Software) 
            MessageBox.Show(readComment.ToAscii(), "The following comment has been read:") 
         End If 
      Finally 
         ofd.Dispose() 
      End Try 
    End Sub 

    C#

    private void btnCommentReadWrite_Click(object sender, EventArgs e) 
    { 
      using (OpenFileDialog ofd = new OpenFileDialog()) 
      { 
         ofd.Filter = "Tif files (*.tif)|*.tif|All files (*.*)|*.*"; 
         if (ofd.ShowDialog() == DialogResult.OK) 
         { 
            //Write the comment to the file 
            RasterCommentMetadata writeComment = new RasterCommentMetadata(); 
            writeComment.Type = RasterCommentMetadataType.Software; 
            writeComment.FromAscii("LEADTOOLS Demo"); 
            codecs.WriteComment(ofd.FileName, 1, writeComment); 
            //Read The Comment 
            RasterCommentMetadata readComment = 
            codecs.ReadComment(ofd.FileName, 1, RasterCommentMetadataType.Software); 
            MessageBox.Show(readComment.ToAscii(), "The following comment has been read:"); 
         } 
      } 
    } 

  2. To read or write TIFF tags, drag and drop a button onto Form1. Change the following properties:

Property Value
Name: btnReadWriteTags
Text: Read\WriteTags
  1. Add the following code for the btnReadWriteTags control’s click procedure:

    VB

    Private Sub btnReadWriteTags_Click(ByVal sender As System.Object,  
                                       ByVal e As System.EventArgs) Handles btnReadWriteTags.Click 
      Dim ofd As OpenFileDialog = New OpenFileDialog() 
      Try 
         ofd.Filter = "Tif files (*.tif)|*.tif|All files (*.*)|*.*" 
         If ofd.ShowDialog() = DialogResult.OK Then 
            '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(ofd.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(ofd.FileName, 1, ReadTag) 
            MessageBox.Show("Resolution changed successfully.") 
         End If 
      Finally 
         ofd.Dispose() 
      End Try 
    End Sub 

    C#

    private void btnReadWriteTags_Click(object sender, EventArgs e) 
    { 
      using (OpenFileDialog ofd = new OpenFileDialog()) 
      { 
         ofd.Filter = "Tif files (*.tif)|*.tif|All files (*.*)|*.*"; 
         if (ofd.ShowDialog() == DialogResult.OK) 
         { 
            //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(ofd.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(ofd.FileName, 1, ReadTag); 
            MessageBox.Show("Resolution changed successfully."); 
         } 
      } 
    } 

    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.

  2. Compile and run your project.
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document
Click or drag to resize