Load, Save, and Split Annotations - Console C#

This tutorial shows how to create a C# Windows Console application that uses the LEADTOOLS SDK to load and save annotation containers, as well as split an annotation container from a TIFF image.

Overview  
Summary This tutorial covers how to load, save, and split an annotation container in a Console C# Application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (5 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

Before working on the Load, Save, and Split Annotations - Console C# tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.

Create the Project and Add LEADTOOLS References

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 by one or the other of the following two methods (but not both). For this project, the following references are needed:

If NuGet references are used, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which DLL files are required for your application, refer to Files to be Included in 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 to Setting a Runtime License.

There are two types of runtime licenses:

Note

For details on how to properly add LEADTOOLS NuGet and local references, refer to the Add References and Set a License tutorial.

Add the Load TIFF Annotations Code

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

In Solution Explorer, open Program.cs. Add a new method called LoadTifAnnotationsExample() and call it inside the Main method.

Add the following statements to the using block at the top of Program.cs:

C#
// Using block at the top 
using System; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Annotations.Engine; 

Add the below code to load the annotations from a TIFF file.

C#
// Add this global variable 
static AnnContainer annContainer; 
C#
static void LoadTifAnnotationsExample() 
{ 
   // Load the annotations from the TIFF file 
   AnnCodecs annCodecs = new AnnCodecs(); 
 
   annContainer = annCodecs.Load(@"FILE PATH TO SAMPLE TestFileTifAnnotations.tif", 1); 
 
   // Print out the objects in the container to show they are loaded 
   Console.WriteLine("ANNOTATIONS LOADED: From Test TIF File that Already Contained Annotations:\n"); 
   foreach (var annObject in annContainer.Children) 
   { 
      Console.WriteLine($"Annotation: {annObject}"); 
   } 
      Console.WriteLine("\n"); 
} 

Add the Save TIFF Annotations Code

In Program.cs, add a new method called SaveTifAnnotationsExample and call this method inside the Main method, under LoadTifAnnotationsExample(). Add the below code inside the new method to save the annotations to a new TIFF file.

C#
static void SaveTifAnnotationsExample() 
{ 
   using (RasterCodecs rasterCodecs = new RasterCodecs()) 
   { 
      AnnCodecs annCodecs = new AnnCodecs(); 
 
      RasterTagMetadata tag = annCodecs.SaveToTag(annContainer, false); 
 
      rasterCodecs.WriteTag(@"FILE PATH TO SAMPLE SaveAnnotationsToTif.tif", 1, tag); 
 
      // Now load the annotations from the TIFF file we just saved to ensure they were saved correctly 
      AnnContainer savedTifContainer = annCodecs.Load(@"FILE PATH TO SAMPLE SaveAnnotationsToTif.tif", 1); 
 
      // Print out the objects in the container to show they are loaded 
      Console.WriteLine("ANNOTATIONS LOADED: From TIFF File that we Saved Annotations to:\n"); 
      foreach (var annObject in savedTifContainer.Children) 
      { 
         Console.WriteLine($"Annotation: {annObject}"); 
      } 
      Console.WriteLine("\n"); 
   } 
} 

Add the Split Annotation Container from TIFF Code

In Program.cs, add a new method called SplitContainerToTifAndXmlExample and call this method in the Main method, under SaveTifAnnotationsExample(). Add the below code to split the annotation container from the TIFF image and save it to a separate XML file.

C#
static void SplitContainerToTifAndXmlExample() 
{ 
   // Save all the Rectangle annotations from the container to XML 
   AnnCodecs annCodecs = new AnnCodecs(); 
   AnnContainer xmlContainer = annContainer.Clone(); 
 
   for (int i = 0; i < xmlContainer.Children.Count; i++) 
   { 
      if (xmlContainer.Children[i].Id != AnnObject.RectangleObjectId) 
      { 
         xmlContainer.Children.Remove(xmlContainer.Children[i]); 
         i--; 
      } 
   } 
 
   annCodecs.Save(@"FILE PATH TO EXPORT RectangleAnnotationsXml.xml", xmlContainer, AnnFormat.Annotations, 1); 
   // Now load the annotations from the XML file we just saved to ensure they were saved correctly 
   AnnContainer rectangleXmlContainer = annCodecs.Load(@"FILE PATH TO EXPORT RectangleAnnotationsXml.xml", 1); 
 
   // Print out the objects in the container to show they are loaded 
   Console.WriteLine("ANNOTATIONS LOADED: From XML File that we Saved ONLY Rectangle Annotations to:"); 
   foreach (var annObject in rectangleXmlContainer.Children) 
   { 
      Console.WriteLine($"Annotation: {annObject}"); 
   } 
   Console.WriteLine(""); 
 
   // Save all the Note annotations from the container to TIFF 
   AnnContainer tifContainer = annContainer.Clone(); 
 
   for (int i = 0; i < tifContainer.Children.Count; i++) 
   { 
      if (tifContainer.Children[i].Id != AnnObject.NoteObjectId) 
      { 
         tifContainer.Children.Remove(tifContainer.Children[i]); 
         i--; 
      } 
   } 
 
   using (RasterCodecs rasterCodecs = new RasterCodecs()) 
   { 
      RasterTagMetadata tag = annCodecs.SaveToTag(tifContainer, false); 
 
      rasterCodecs.WriteTag(@"FILE PATH TO SAMPLE SaveJustNoteAnnotationsToTif.tif", 1, tag); 
   } 
 
   // Now load the annotations from the XML file we just saved to ensure they were saved correctly 
   AnnContainer noteTifContainer = annCodecs.Load(@"FILE PATH TO SAMPLE SaveJustNoteAnnotationsToTif.tif", 1); 
 
   // Print out the objects in the container to show they are loaded 
   Console.WriteLine("ANNOTATIONS LOADED: From TIFF File from which we Saved ONLY Note Annotations to:"); 
   foreach (var annObject in noteTifContainer.Children) 
   { 
      Console.WriteLine($"Annotation: {annObject}"); 
   } 
   Console.WriteLine("\n"); 
} 

Note

This tutorial sample uses a few existing TIFF files that you can download here. Be sure to replace the file paths in the above functions accordingly.

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and displays the output, as shown in the following screenshot:

Project output

Wrap-up

This tutorial showed how to add the necessary references to work with automated annotations and work with the AnnCodecs and AnnContainer classes.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.