Burn Annotations to an Image - C# .NET 6

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

Overview
Summary This tutorial covers burning annotations to an image in a C# .NET 6 application.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (497 KB)
Platform C# .NET 6 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 theAdd References and Set a Licensetutorial, before working on theBurn Annotations to an Image - C# .NET 6tutorial.

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

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

公关所需许可解锁的功能oject. 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.

InSolution Explorer, openProgram.csand add the following statements to theusingblock at the top of the file.

C#
usingSystem;usingSystem.IO;usingLeadtools;usingLeadtools.Annotations.Engine;usingLeadtools.Annotations.Rendering;usingLeadtools.Codecs;

In theProgram.csfile, add a new method calledBurnAnnotationsToImage()and call it inside the Main method after theInitLEAD()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 theProgram.csC# source file.

Add the below code to load aRasterImage, load theAnnContainer, map theAnnContainerto the image, burn the container to the image, and export the new image to a file.

C#
staticvoidBurnAnnotationsToImage(){stringimageFile =@"Burn-Annotations-to-an-Image-Source-Image.jpg";stringannFile =@"Burn-Annotations-to-an-Image-Annotations-File.xml";stringoutputFile =@"output.jpg";AnnDrawRenderingEngine _renderingEngine =newAnnDrawRenderingEngine();using(RasterCodecs codecs =newRasterCodecs()){AnnCodecs annCodecs =newAnnCodecs();AnnContainer container =newAnnContainer();using(RasterImage srcImage = codecs.Load(imageFile)){// If you would like to use Memory Stream, then use this code:/*byte[] bytes = File.ReadAllBytes(imageFile);using (MemoryStream ms = new MemoryStream(bytes)){ms.Position = 0;codecs.Load(ms);Console.WriteLine("Image loaded");}*/container.Mapper.MapResolutions(srcImage.XResolution, srcImage.YResolution, srcImage.XResolution, srcImage.YResolution);container.Size = container.Mapper.SizeToContainerCoordinates(srcImage.ImageSize.ToLeadSizeD());container = annCodecs.Load(annFile, 1);// Uncomment the below code to use memory stream to handle the files// byte[] outputBytes = File.ReadAllBytes(outputFile);// using (MemoryStream ms = new MemoryStream(outputBytes))// {using(RasterImage burnImage = _renderingEngine.RenderOnImage(container, srcImage)){codecs.Save(burnImage, outputFile, RasterImageFormat.Jpeg, 0);stringpath = Path.GetFullPath(outputFile);Console.WriteLine("Image saved"+ path);/*ms.Position = 0;codecs.Save(burnImage, ms, RasterImageFormat.Jpeg, 0);string path = Path.GetFullPath(outputFile);Console.WriteLine("Image saved" + path);*/}// }}}}

Handling Streams

To load an image using memory stream, uncomment the code located in theBurnAnnotationsToImage()method.

C#
// load the image using memory streambyte[] bytes = File.ReadAllBytes(imageFile);using(MemoryStream ms =newMemoryStream(bytes)){ms.Position = 0;codecs.Load(ms);Console.WriteLine("Image loaded");}// save the stream of outputFilebyte[] outputBytes = File.ReadAllBytes(outputFile);using(MemoryStream ms =newMemoryStream(outputBytes)){ms.Position = 0;codecs.Save(burnImage, ms, RasterImageFormat.Jpeg, 0);stringpath = Path.GetFullPath(outputFile);Console.WriteLine("Image saved"+ path);}

Run the Project

Run the project by pressingF5, or by selectingDebug -> 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 theAnnCodecs,AnnContainer, andAnnDrawRenderingEngineclasses.

See Also

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