Build a .NET Core Image Conversion MicroService using LEADTOOLS NuGet Packages

This blog post is to demonstrate the simplicity when working with LEADTOOLS NuGet packages to convert a hosted file from the web to a PNG. LEAD introduced NuGet packages at the release of Version 20 and never looked back. The following example is a spin-off of a prior blog that exemplified how to incorporate LEADTOOLS using .NET Core without NuGet packages.

Setting up the Project

This project was created using Visual Studio 2017.

Create a new application and select “ASP.NET Core Web Application”

Select API Template

Adding NuGet packages to the Application

Adding NuGet packages is much easier and faster than searching specific DLLs to add to your application. LEADTOOLS has 26 NuGet packages to choose from. For the purpose of this example, we will be adding four of them.

How to add NuGet packages
  1. Right-Click the project and select Manage NuGet Packages. This will take you to the NuGet Package Manager where you can browse the four NuGets in the search bar.
  2. Select the NuGet package and hit Install.
  3. Accept the License Acceptance.

Add ImagingController

Now that the NuGet packages are installed, it is time to add the ImagingController and set your license.
Press Shift+Alt+C to create a new class file named ImagingController in the “Controllers” folder.
This class is where the URL is passed and loaded to a RasterImage. Once the RasterImage contains and image, you can convert/save it to any of LEADTOOLS supported file formats.

Code Details

using System;
using System.IO;
using Leadtools;
using Leadtools.Codecs;
using Microsoft.AspNetCore.Mvc;

namespace ImageConversion.Controllers
    public class ImagingController : Controller
        private static void UnlockLeadtools()
            // Embed your license. Use for a refernce
            string licString =
                     License = <doc><ver>2.0</ver><code>INSERT LICENSE HERE</code></doc>";

            byte[] licBytes = System.Text.Encoding.UTF8.GetBytes(licString);
            string key = "INSERT DEV KEY HERE";
            RasterSupport.SetLicense(licBytes, key);

        public ActionResult Get(Uri url, int pageNumber)

            const string mimeType = "image/png";

            if (url == null)
                return Content("url cannot be null");
            if (!url.IsAbsoluteUri)
                return Content("url must be absolute. Make sure to include the protocol.");

            string targetFile = null;
                targetFile = Path.GetTempFileName();
                using (RasterCodecs codecs = new RasterCodecs())
                    int totalPages = codecs.GetTotalPages(url).Result;
                    if (pageNumber < 1 )
                        pageNumber = 1;                                            
                    else if (pageNumber > totalPages)
                        pageNumber = totalPages;
                    RasterImage image = codecs.Load(url, pageNumber).Result;
                    codecs.Save(image, targetFile, RasterImageFormat.Png, 0);

                // Load converted file and return.
                byte[] buffer = System.IO.File.ReadAllBytes(targetFile);
                return File(buffer, mimeType);
            catch (Exception ex)
                return Content(ex.Message);
                // Clean up
                if (targetFile != null && System.IO.File.Exists(targetFile))

Run the Application

  1. Run the service in Visual Studio. This will open to the URL that is specified in the Project Properties, under the Debug tab.
  2. The demo will have a URL where you only need to append the hosted image URL. There is also capability to work with multi-page hosted images like TIF or PDF. If a page number is not given, the service will default to the first page in the image.

PDF example when setting pageNumber to 3:

  • http://localhost:7448/api/Imaging?url=
PDF, page 3, converted to PNG

TIF example:

  • http://localhost:7448/api/Imaging?url=
TIF converted to PNG

To test this with the latest version of LEADTOOLS, download the free 60 day evaluation straight from our site. If you have any comments or questions regarding this, feel free to comment on this post or contact our Support department at

This entry was posted in File Formats and tagged , , , , . Bookmark the permalink.

One Response to Build a .NET Core Image Conversion MicroService using LEADTOOLS NuGet Packages

  1. Ritika Goswami says:

    Nice good information given keep sharing more thanks for it keep sharing more

Leave a Reply

Your email address will not be published. Required fields are marked *