This tutorial shows how to read and write TIFF tags and comments in a C# .NET 6 application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to use the RasterCommentMetaData class in a C# .NET application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET Console Application |
IDE | Visual Studio 2022 |
Runtime Target | .NET 6 or Higher |
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 a License tutorial, before working on the Read and Write TIFF Tags and Comments - C# .NET 6 tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. 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 to Files 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 to Setting a Runtime License.
There are two types of runtime licenses:
With the project created, the references added, and the license set, coding can begin.
In the Solution Explorer, open Program.cs
. Add the following variables to the using
block at the top of Program.cs
.
using System;
using System.IO;
using Leadtools;
using Leadtools.Codecs;
In the Program
class, add the following variables in the Program
class before Main()
. Add the new string fileName
and set the new string value to the file path containing the TIFF file. Next, add the new string multiPageTif
and set the new string value to the file path if one will be used.
public static string fileName = @"C:\LEADTOOLS22\Resources\Images\clean.tif";
public static string multiPageTif = @""; // Update to path location to a MultiPage Tiff
In the Program
class, add a new method named ReadAndWriteTifComments(string fileName)
to the Program
class. Call it in the Main()
method as shown below.
static void Main(string[] args)
{
InitLEAD();
ReadAndWriteTifComments(fileName);
ReadAndWriteTifTags(fileName);
ReadAndWriteTagsToMultipageTif(multiPageTif);
}
Add the code below to the ReadAndWriteTifComments()
method to write a comment to the TIFF file, then read the comment and display it to the console.
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");
}
}
Note
Because the
RasterCodecs
class implementsIDisposable
, make sure it is in ausing
statement for proper disposal.
In the Program
class, add a new method named ReadAndWriteTifTags(string fileName)
, and call it in the Main()
method after ReadAndWriteTifComments()
, as shown above. Add the below code to the new method to read the XResolution
of the TIFF image, modify the value, and write the tag to the file.
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.");
}
}
In the Program
class, add a new method named ReadAndWriteTagsToMultipageTif(string fileName)
, and call it in the Main()
method after the ReadAndWriteTifTags()
method, as shown above. Add the below code to the new method to read the PageName for each page in a MultiPage TIFF file, and then save the values in a new file.
static void ReadAndWriteTagsToMultipageTif(string fileName)
{
if (!(String.IsNullOrEmpty(multiPageTif)))
{
// This code reads the PageName for each page in a MultiPage TIFF file, and then saves the values in a new file.
using (RasterCodecs codecs = new RasterCodecs())
{
const int TIFFTAG_PAGENAME = 285;
CodecsImageInfo imageInfo = codecs.GetInformation(multiPageTif, true);
int totalpages = imageInfo.TotalPages;
codecs.Options.Load.Tags = true;
codecs.Options.Save.Tags = true;
for (int i = 1; i <= totalpages; i++)
{
var rasterImage = codecs.Load(multiPageTif, i);
codecs.ReadTag(multiPageTif, i, TIFFTAG_PAGENAME).ToAscii();
Console.WriteLine(codecs.ReadTag(multiPageTif, i, TIFFTAG_PAGENAME).ToAscii());
codecs.Save(rasterImage, "output.tif", RasterImageFormat.TifJpeg411, 0, 1, 1, 1, CodecsSavePageMode.Append);
Console.WriteLine(codecs.ReadTag("output.tif", i, TIFFTAG_PAGENAME).ToAscii());
}
}
}
}
Run the project by pressing F5, or by selecting Debug -> 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 the XResolution
from the TIFF image, modifies the resolution value and writes the tag back to the TIFF image. The application will also write TIFF comments to a new multipage TIFF if one is provided.
This tutorial showed how to use the RasterTagMetadata
and RasterCommentMetadata
classes to read/write TIFF comments and tags.