This tutorial shows how to read and write TIFF tags and comments in a C# .NET Core application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to use theRasterCommentMetaDataclass in a C# .NET Core Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET Core Console Application |
IDE | Visual Studio 2019, 2022 |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing theAdd References and Set a Licensetutorial, before working on the意图d and Write TIFF Tags and Comments - C# .NET Coretutorial.
Start with a copy of the project created in theAdd References and Set a Licensetutorial. If you do not have that project, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added via NuGet packages.
This tutorial requires the following NuGet package:
Leadtools.Formats.Raster.Common
For a complete list of which DLL files are required for your application, refer toFiles to be Included With Your Application.
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 toSetting a Runtime License.
There are two types of runtime licenses:
Note
Adding LEADTOOLS NuGet references and setting a license are covered in more detail in theAdd References and Set a Licensetutorial.
With the project created, the references added, and the license set, coding can begin.
In theSolution Explorer, openProgram.cs
. Add the following statements to theusing
block at the top ofProgram.cs
.
usingSystem;
usingSystem.IO;
usingLeadtools;
usingLeadtools.Codecs;
In theProgram
class, add a new stringfileName
, below the set license code. Set the new string value to the file path containing the TIFF file. Add a new method named意图dAndWriteTifComments(string fileName)
to theProgram
class. Call it in theMain()
method after thefileName
string value, as shown below.
staticvoidMain(string[] args)
{
if(!SetLicense())
Console.WriteLine("Error setting license");
else
Console.WriteLine("License file set successfully");
stringfileName =@"C:\LEADTOOLS22\Resources\Images\clean.tif";
意图dAndWriteTifComments(fileName);
意图dAndWriteTifTags(fileName);
}
Add the code below to the意图dAndWriteTifComments()
method to write a comment to the TIFF file, then read the comment and display it to the console.
staticvoid意图dAndWriteTifComments(stringfileName)
{
using(RasterCodecs codecs =newRasterCodecs())
{
// Write the comment to the file
RasterCommentMetadata writeComment =newRasterCommentMetadata();
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");
}
}
Note
Because the
RasterCodecs
class implementsIDisposable
, make sure it is in ausing
statement for proper disposal.
In theProgram
class, add a new method named意图dAndWriteTifTags(string fileName)
, and call it in theMain()
method after the意图dAndWriteTifComments()
method, as shown above. Add the below code to the new method to read theXResolution
of the TIFF image, modify the value, and write the tag to the file.
staticvoid意图dAndWriteTifTags(stringfileName)
{
using(RasterCodecs codecs =newRasterCodecs())
{
// This code reads the Xresolution from a TIFF image, modifies the value, and saves it back.
constintXresTagID = 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;
意图dTag.FromURational(rational);
codecs.WriteTag(fileName, 1, ReadTag);
Console.WriteLine("Resolution changed successfully.");
}
}
Run the project by pressingF5, or by selectingDebug -> Start Debugging.
If the steps were followed correctly, the console appears and the application writes a new TIFF comment and then displays the comment in the console. Then, the application reads theXResolution
from the TIFF image, modifies the resolution value and writes the tag back to the TIFF image.
This tutorial showed how to use theRasterTagMetadata
andRasterCommentMetadata
classes to read/write TIFF comments and tags.