Load and Save Images - Console C#

This tutorial shows how to use the LEADTOOLS SDK to create a C# Windows 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# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

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 the Load and Save Images - Console C# tutorial.

Create the Project and Add LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the following 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 using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed:

The local DLLs are located at <INSTALL_DIR>\LEADTOOLS 20\Bin\Dotnet4\x64

Note

Different file formats require different Codec DLLs. For a complete list, see Files to be Included in 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:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail 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 Solution Explorer, open Program.cs, then add using Leadtools;, using Leadtools.Codecs;, and using Leadtools.Codecs.Cmp.dll to the using block at the top.

In the Program class add a new method called LoadImage(string filename), and call it in the Main method after the SetLicense method. Use the following test image C:\Users\Public\Documents\LEADTOOLS Images\image1.cmp

C#
// Using block at the top 
using System; 
using Leadtools; 
using Leadtools.Codecs; 
C#
static void Main(string[] args) 
{ 
    SetLicense(); 
    RasterImage image = LoadImage(@"C:\Users\Public\Documents\LEADTOOLS Images\image1.cmp"); 
} 
C#
static RasterImage LoadImage(string filename) 
{ 
    using (RasterCodecs codecs = new RasterCodecs()) 
        return codecs.Load(filename); 
} 

The 'using' statements are necessary because the RasterCodecs class implements IDisposable, which requires using statements for proper disposal.

Add the Save Image Code

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

C#
static void Main(string[] args) 
{ 
    SetLicense(); 
    RasterImage image = LoadImage(@"C:\Users\Public\Documents\LEADTOOLS Images\image1.cmp"); 
    SaveImage(image, @"C:\temp\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 are followed correctly, the application 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.

See Also

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