Load and Save Images - .NET Core

This tutorial shows how to use the LEADTOOLS SDK to create a C# .NET Core Console application that uses the RasterCodecs and RasterImage classes to load and save an image file.

Overview  
Summary This tutorial covers how to use the RasterCodecs Class in a C# .NET Core Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Core Console application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on the Load and Save Images - .NET Core 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 LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License Tutorial 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 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 references is covered in the Add References and Set a License tutorial.

Add the Load Image Code

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

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

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

In the Program class add a new method called LoadImage(string filename), and call it in the Main method after the SetLicense method. This tutorial uses the following test image C:\LEADTOOLS21\Resources\Images\image1.cmp

C#
static void Main(string[] args) 
{ 
   if (!SetLicense()) 
   { 
      Console.WriteLine("Error setting license"); 
      return; 
   } 
   RasterImage image = LoadImage(@"C:\LEADTOOLS21\Resources\Images\image1.cmp"); 
} 

Add the below code to load the image. The using statement is recommended because the RasterCodecs class implements IDisposable, which causes proper disposal with using statements.

C#
static RasterImage LoadImage(string filename) 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
      return codecs.Load(filename); 
} 

Add the Save Image Code

In the Program class, add a new method called SaveImage(RasterImage image, string outputFilename), and call it in the Main method after the LoadImage method.

C#
static void Main(string[] args) 
{ 
   if (!SetLicense()) 
   { 
      Console.WriteLine("Error setting license"); 
      return; 
   } 
   RasterImage image = LoadImage(@"C:\LEADTOOLS21\Resources\Images\image1.cmp"); 
   SaveImage(image, @"C:\LEADTOOLS21\Resources\Images\output.jpg"); 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

Add the below code to export the image as JPEG to the following file path: C:\LEADTOOLS21\Resources\Images\output.jpg

C#
static void SaveImage(RasterImage image, string outputFilename) 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
      codecs.Save(image, outputFilename, 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 runs and creates a new file in the output location specified in the save call.

Wrap-up

This tutorial showed how to use the RasterCodecs and RasterImage classes to load and save images in a .NET Core Console Application.

See Also

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

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