Burn Annotations to an Image - .NET Core Console C#

This tutorial shows how to create a C# .NET Core Console application that uses the LEADTOOLS SDK to burn annotations onto an Image.

Overview  
Summary This tutorial covers burning annotations to an image in a C# .NET Core Console application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (496 KB)
Platform C# .NET Core Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

Before working on the Burn Annotations to an Image - .NET Core Console C# tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License Tutorial tutorial.

Create the Project and Add the LEADTOOLS References

Create a new C# .NET Core Console project, and add the below necessary LEADTOOLS references.

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>\LEADTOOLS21\Bin\Dotnet4\x64:

Leadtools.dll
Leadtools.Annotations.Engine.dll
Leadtools.Annotations.Rendering.dll
Leadtools.Codecs.dll

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

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

How to properly add LEADTOOLS NuGet and local references is covered in the Add References and Set a License Tutorial.

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 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 SetLicense() 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 C# source file.

Add the below code to load a RasterImage, load the AnnContainer, map the AnnContainer to the image, burn the container to the image, and export the new image to a file.

C#
static void BurnAnnotationsToImage() 
{ 
    string imageFile = @"Burn-Annotations-to-an-Image-Source-Image.jpg"; 
    string annFile = @"Burn-Annotations-to-an-Image-Annotations-File.xml"; 
    string outputFile = @"output.jpg"; 
 
    AnnDrawRenderingEngine _renderingEngine = new AnnDrawRenderingEngine(); 
 
    using (RasterCodecs codecs = new RasterCodecs()) 
    { 
        AnnCodecs annCodecs = new AnnCodecs(); 
        AnnContainer container = new AnnContainer(); 
        using (RasterImage srcImage = codecs.Load(imageFile)) 
        { 
            container.Mapper.MapResolutions(srcImage.XResolution, srcImage.YResolution, srcImage.XResolution, srcImage.YResolution); 
            container.Size = container.Mapper.SizeToContainerCoordinates(srcImage.ImageSize.ToLeadSizeD()); 
            container = annCodecs.Load(annFile, 1); 
            using (RasterImage burnImage = _renderingEngine.RenderOnImage(container, srcImage)) 
            { 
                codecs.Save(burnImage, outputFile, RasterImageFormat.Jpeg, 0); 
            } 
        } 
    } 
} 

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 21.0.2020.9.16
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.