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 the RasterCommentMetaData class 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 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 a License tutorial, before working on the Read and Write TIFF Tags and Comments - C# .NET Core 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.CommonFor 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:
Note
Adding LEADTOOLS NuGet references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, and the license set, coding can begin.
In the Solution Explorer, open Program.cs. Add the following statements 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 a new string fileName, below the set license code. Set the new string value to the file path containing the TIFF file. Add a new method named ReadAndWriteTifComments(string fileName) to the Program class. Call it in the Main() method after the fileName string value, as shown below.
static void Main(string[] args){if (!SetLicense())Console.WriteLine("Error setting license");elseConsole.WriteLine("License file set successfully");string fileName = @"C:\LEADTOOLS21\Resources\Images\clean.tif";ReadAndWriteTifComments(fileName);ReadAndWriteTifTags(fileName);}
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 fileRasterCommentMetadata writeComment = new RasterCommentMetadata();writeComment.Type = RasterCommentMetadataType.Software;writeComment.FromAscii("LEADTOOLS Demo");codecs.WriteComment(fileName, 1, writeComment);// Read The CommentRasterCommentMetadata readComment =codecs.ReadComment(fileName, 1, RasterCommentMetadataType.Software);Console.WriteLine("The following comment has been read:\n" +$"{readComment.ToAscii()}/n");}}
Note
Because the
RasterCodecsclass implementsIDisposable, make sure it is in ausingstatement for proper disposal.
In the Program class, add a new method named ReadAndWriteTifTags(string fileName), and call it in the Main() method after the ReadAndWriteTifComments() method, 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.");}}
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.
This tutorial showed how to use the RasterTagMetadata and RasterCommentMetadata classes to read/write TIFF comments and tags.