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 Leadtools
Imports 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 object
codecs = New RasterCodecs()
End Sub
private void Form1_Load(object sender, System.EventArgs e)
{
// Initialize a new RasterCodecs object
codecs = new RasterCodecs();
}
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;
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 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:");
}
}
}
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.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.
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET