This tutorial shows how to read and write TIFF tags and comments using the LEADTOOLS SDK in a Console application.
Overview | |
---|---|
Summary | This tutorial covers how to use the RasterCommentMetaData class in a C# Windows Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (3 KB) |
Platform | Windows Console C# Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set License tutorial, before working on the Read and Write TIFF Tags and Comments - Console C# tutorial.
Create a new C# Windows Console project, and add the below necessary LEADTOOLS references.
The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:
If using NuGet references, this tutorial requires the following NuGet package:
Leadtools.Formats.Raster.Common
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Tif.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
Note
Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set License.
With the project created, the references added, and the license set, coding can begin.
Open Program.cs
in the Solution Explorer and then add using Leadtools;
and using Leadtools.Codecs;
statements to the using block at the top.
In the Program class add a new string fileName
below the SetLicense();
method call, and set it to be equal to the file path containing the TIFF file. Now add a new method called ReadAndWriteTifComments(string fileName)
and call it in the Main method after SetLicense();
.
// Using block at the top
using System;
using System.IO;
using Leadtools;
using Leadtools.Codecs;
static void Main(string[] args)
{
SetLicense();
string fileName = @"C:\LEADTOOLS21\Resources\Images\clean.tif";
ReadAndWriteTifComments(fileName);
}
static void ReadAndWriteTifComments(string fileName)
{
using (RasterCodecs codecs = new RasterCodecs())
{
//Write the comment to the file
RasterCommentMetadata writeComment = new RasterCommentMetadata();
writeComment.Type = RasterCommentMetadataType.Software;
writeComment.FromAscii("LEADTOOLS Demo");
codecs.WriteComment(fileName, 1, writeComment);
//Read The Comment
RasterCommentMetadata readComment =
codecs.ReadComment(fileName, 1, RasterCommentMetadataType.Software);
Console.WriteLine("The following comment has been read:\n" +
$"{readComment.ToAscii()}/n");
}
}
Because the RasterCodecs
class implements IDisposable
, make sure it is in a using
statement for proper disposal.
In the Program class, add a new method called ReadAndWriteTifTags(string fileName)
, and call it in the Main method after the ReadAndWriteTifComments(fileName)
method.
static void ReadAndWriteTifTags(string fileName)
{
using (RasterCodecs codecs = new RasterCodecs())
{
//This code reads the Xresolution from a TIFF image, modifies the value, and saves it back.
const int XresTagID = 282;
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);
Console.WriteLine("Resolution changed successfully.");
}
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application writes a new TIFF comment and then displays the comment in the console. Then the application reads the XResolution
from the TIFF image and modifies the resolution value and writes the tag back to the TIFF image.
This tutorial showed how to use the RasterTagMetadata
and RasterCommentMetadata
classes to read/write TIFF comments and tags.