意图d and Write TIFF Tags and Comments - C# .NET Core

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

Required Knowledge

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.

Create the Project and Add LEADTOOLS References

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:

For a complete list of which DLL files are required for your application, refer toFiles to be Included With Your Application.

Set the License File

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.

Add the Read/Write TIFF Comments Code

With the project created, the references added, and the license set, coding can begin.

In theSolution Explorer, openProgram.cs. Add the following statements to theusingblock at the top ofProgram.cs.

C#
usingSystem;usingSystem.IO;usingLeadtools;usingLeadtools.Codecs;

In theProgramclass, 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 theProgramclass. Call it in theMain()method after thefileNamestring value, as shown below.

C#
staticvoidMain(string[] args){if(!SetLicense())Console.WriteLine("Error setting license");elseConsole.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.

C#
staticvoid意图dAndWriteTifComments(stringfileName){using(RasterCodecs codecs =newRasterCodecs()){// Write the comment to the fileRasterCommentMetadata writeComment =newRasterCommentMetadata();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 theRasterCodecsclass implementsIDisposable, make sure it is in ausingstatement for proper disposal.

Add the Read/Write TIFF Tags Code

In theProgramclass, 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 theXResolutionof the TIFF image, modify the value, and write the tag to the file.

C#
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

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 theXResolutionfrom the TIFF image, modifies the resolution value and writes the tag back to the TIFF image.

Wrap-up

This tutorial showed how to use theRasterTagMetadataandRasterCommentMetadataclasses to read/write TIFF comments and tags.

See Also

iOS
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2022LEAD Technologies, Inc.All Rights Reserved.