Burn Annotations to an Image - C# .NET 6

This tutorial shows how to burn annotations onto an image in a C# .NET 6 Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers burning annotations to an image in a C# .NET 6 Console application.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (497 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on this tutorial.

Create the Project and Add the 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.

This tutorial requires the following NuGet package:

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

There are two types of runtime licenses:

Add the Burn Annotations Code

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

In Solution Explorer, open Program.cs and add the following statements to the using block at the top of the file.

C#
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Annotations.Engine; 
using Leadtools.Annotations.Rendering; 
using Leadtools.Codecs; 

In the Program.cs file, add a new method called BurnAnnotationsToImage() and call it inside the Main method after the InitLEAD method. The source image is provided with the project. There is also an XML file that contains the annotations data. The file names for these two files are:

File Name Description
Burn-Annotations-to-an-Image-Source-Image.jpg Image file
Burn-Annotations-to-an-Image-Annotations-File.xml LEAD Annotations file

These files are in the same directory as the Program.cs source file.

Inside of BurnAnnotationsToImage(), begin by setting up path variables for both of the provided files and the output destination.

C#
string imageFile = @"PATH TO IMAGE"; 
string annFile = @"PATH TO ANNOTATIONS FILE"; 
string outputFile = @"OUTPUT PATH"; 

Note: The output path needs to include the file name and type, such as @"C:\outputFile.jpeg"

Next create variables for the rendering engine, codecs, and annotations container.

C#
AnnDrawRenderingEngine _renderingEngine = new(); 
using RasterCodecs codecs = new(); 
AnnCodecs annCodecs = new(); 
AnnContainer container = new(); 

Use the following code to load the image and annotations file into their respective variables:

C#
using RasterImage srcImage = codecs.Load(imageFile); 
container = annCodecs.Load(annFile, 1); 
Console.WriteLine("Image loaded"); 
Console.WriteLine("Annotations loaded"); 
 
/* Use this code instead for Memory Stream 
using MemoryStream imageStream = new(); 
byte[] imageBytes = File.ReadAllBytes(imageFile); 
imageStream.Write(imageBytes); 
imageStream.Position = 0; 
using RasterImage srcImage = codecs.Load(imageStream); 
 
using MemoryStream annStream = new(); 
byte[] annBytes = File.ReadAllBytes(annFile); 
annStream.Write(annBytes); 
annStream.Position = 0; 
container = annCodecs.Load(annStream, 1); 
*/ 

Then burn the annotations onto the image.

C#
using RasterImage burnImage = _renderingEngine.RenderOnImage(container, srcImage); 

Lastly, save the file to the desired output path.

C#
codecs.Save(burnImage, outputFile, RasterImageFormat.Jpeg, 0); 
string path = Path.GetFullPath(outputFile); 
Console.WriteLine("Image saved" + path); 
 
/* Use this code instead for Memory Stream 
imageStream.Position = 0; 
codecs.Save(burnImage, imageStream, RasterImageFormat.Jpeg, 0); 
File.WriteAllBytes(outputFile, imageStream.ToArray()); 
string path = Path.GetFullPath(outputFile); 
Console.WriteLine("Image saved" + path); 
*/ 

The final BurnAnnotationsToImage method should look like this:

C#
static void BurnAnnotationsToImage()  
{ 
    string imageFile = @"PATH TO IMAGE"; 
    string annFile = @"PATH TO ANNOTATIONS FILE"; 
    string outputFile = @"OUTPUT PATH"; 
 
    AnnDrawRenderingEngine _renderingEngine = new(); 
    using RasterCodecs codecs = new(); 
    AnnCodecs annCodecs = new(); 
    AnnContainer container = new(); 
 
    using RasterImage srcImage = codecs.Load(imageFile); 
    container = annCodecs.Load(annFile, 1); 
    Console.WriteLine("Image loaded"); 
    Console.WriteLine("Annotations loaded"); 
 
    /* Use this code instead for Memory Stream 
    using MemoryStream imageStream = new(); 
    byte[] imageBytes = File.ReadAllBytes(imageFile); 
    imageStream.Write(imageBytes); 
    imageStream.Position = 0; 
    using RasterImage srcImage = codecs.Load(imageStream); 
 
    using MemoryStream annStream = new(); 
    byte[] annBytes = File.ReadAllBytes(annFile); 
    annStream.Write(annBytes); 
    annStream.Position = 0; 
    container = annCodecs.Load(annStream, 1); 
    */ 
 
    using RasterImage burnImage = _renderingEngine.RenderOnImage(container, srcImage); 
 
    codecs.Save(burnImage, outputFile, RasterImageFormat.Jpeg, 0); 
    string path = Path.GetFullPath(outputFile); 
    Console.WriteLine("Image saved" + path); 
 
    /* Use this code instead for Memory Stream 
    imageStream.Position = 0; 
    codecs.Save(burnImage, imageStream, RasterImageFormat.Jpeg, 0); 
    File.WriteAllBytes(outputFile, imageStream.ToArray()); 
    string path = Path.GetFullPath(outputFile); 
    Console.WriteLine("Image saved" + path); 
    */ 
} 

Run the Project

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

If the steps were followed correctly, the application loads the specified image, loads the specified annotations XML file, and then burns those annotations to the image and exports that image to a file. The following screenshot shows the expected output:

Output Image With Annotations

Wrap-up

This tutorial showed how to add the necessary references to burn annotations to an image, and how to use the AnnCodecs, AnnContainer, and AnnDrawRenderingEngine classes.

See Also

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


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