Take the following steps to start a project and to add some code that reads and writes comments and tags:
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:
Click the Select button and then press the OK button to add the above DLLs to the application.
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:
Imports LeadtoolsImports Leadtools.Codecs
using Leadtools;using Leadtools.Codecs;
Add an event handler to the Form1 Load event and add the following code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' Initialize a new RasterCodecs objectcodecs = New RasterCodecs()End Sub
private void Form1_Load(object sender, System.EventArgs e){// Initialize a new RasterCodecs objectcodecs = new RasterCodecs();}
Add the following variable to the Form1 class:
' the RasterCodecs object for loading/saving imagesPrivate codecs As RasterCodecs
// the RasterCodecs object for loading/saving imagesprivate RasterCodecs codecs;
Drag and drop a button onto Form1. Change the following properties:
| Property | Value |
| Name: | btnCommentReadWrite |
| Text: | Comment Read\Write |
Add the following code for the btnCommentReadWrite control’s click procedure:
VB
Private Sub btnCommentReadWrite_Click(ByVal sender As System.Object, ByVal eDim ofd As OpenFileDialog = New OpenFileDialog()Tryofd.Filter = "Tif files (*.tif)|*.tif|All files (*.*)|*.*"If ofd.ShowDialog() = DialogResult.OK Then'Write the comment to the fileDim writeComment As RasterCommentMetadata = New RasterCommentMetadata()writeComment.Type = RasterCommentMetadataType.SoftwarewriteComment.FromAscii("LEADTOOLS Demo")codecs.WriteComment(ofd.FileName, 1, writeComment)'Read The CommentDim readComment As RasterCommentMetadata = codecs.ReadComment(ofd.FileName, 1, RasterCommentMetadataType.Software)MessageBox.Show(readComment.ToAscii(), "The following comment has been read:")End IfFinallyofd.Dispose()End TryEnd 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 fileRasterCommentMetadata writeComment = new RasterCommentMetadata();writeComment.Type = RasterCommentMetadataType.Software;writeComment.FromAscii("LEADTOOLS Demo");codecs.WriteComment(ofd.FileName, 1, writeComment);//Read The CommentRasterCommentMetadata readComment =codecs.ReadComment(ofd.FileName, 1, RasterCommentMetadataType.Software);MessageBox.Show(readComment.ToAscii(), "The following comment has been read:");}}}
To read or write TIFF tags, drag and drop a button onto Form1. Change the following properties:
| Property | Value |
| Name: | btnReadWriteTags |
| Text: | Read\WriteTags |
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.ClickDim ofd As OpenFileDialog = New OpenFileDialog()Tryofd.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 = 282Dim tag As RasterTagMetadatatag = New RasterTagMetadata()Dim ReadTag As RasterTagMetadata = codecs.ReadTag(ofd.FileName, 1, XresTagID)Dim rational As RasterMetadataURational() = ReadTag.ToURational()rational(0).Numerator = rational(0).Numerator * 5rational(0).Denominator = rational(0).Denominator * 1ReadTag.FromURational(rational)codecs.WriteTag(ofd.FileName, 1, ReadTag)MessageBox.Show("Resolution changed successfully.")End IfFinallyofd.Dispose()End TryEnd 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.
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
