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 |
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 using local DLL references, the following DLLs are needed:
The local DLLs are located at <INSTALL_DIR>\LEADTOOLS 20\Bin\Dotnet4\x64
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Note
Different file formats require different Codec DLLs. For a complete list, see Files to be Included in Your Application.
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 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
// Using block at the top
using System;
using Leadtools;
using Leadtools.Codecs;
static void Main(string[] args)
{
SetLicense();
RasterImage image = LoadImage(@"C:\Users\Public\Documents\LEADTOOLS 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:\Users\Public\Documents\LEADTOOLS Images\image1.cmp");
SaveImage(image, @"C:\temp\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 are followed correctly, the application 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.