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 |
Try it in another language |
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.
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:
Leadtools.Formats.Raster.Common
NuGet packageIf local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
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.
With the project created, the references added, and the license set, coding can begin.
In the Solution Explorer, open Program.cs
, then add using Leadtools;
and using Leadtools.Codecs;
to the using
block at the top.
// 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. Use the following test image C:\LEADTOOLS21\Resources\Images\image1.cmp
static void Main(string[] args)
{
SetLicense();
RasterImage image = LoadImage(@"C:\LEADTOOLS21\Resources\Images\image1.cmp");
}
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.
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.
static void Main(string[] args)
{
SetLicense();
RasterImage image = LoadImage(@"C:\LEADTOOLS21\Resources\Images\image1.cmp");
SaveImage(image, @"C:\LEADTOOLS21\Resources\Images\output.jpg");
}
static void SaveImage(RasterImage image, string outputFilename)
{
using (RasterCodecs codecs = new RasterCodecs())
codecs.Save(image, outputFilename, RasterImageFormat.Jpeg, 0);
}
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.
This tutorial showed how to use the RasterCodecs
and RasterImage
classes to load and save images.