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
Try it in another language

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

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

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 local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

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

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 the Solution Explorer, open Program.cs, then add using Leadtools; and using Leadtools.Codecs; to the using block at the top.

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. Use the following test image C:\LEADTOOLS22\Resources\Images\image1.cmp

C#
static void Main(string[] args) 
{ 
    SetLicense(); 
    RasterImage image = LoadImage(@"C:\LEADTOOLS22\Resources\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:\LEADTOOLS22\Resources\Images\image1.cmp"); 
    SaveImage(image, @"C:\LEADTOOLS22\Resources\Images\output.jpg"); 
} 
C#
static void SaveImage(RasterImage image, string outputFilename) 
{ 
    using (RasterCodecs codecs = new RasterCodecs()) 
        codecs.Save(image, outputFilename, RasterImageFormat.Jpeg, 0); 
} 

Handling Streams

To handle loading and saving the files using MemoryStream, replace the existing code in the LoadImage() with the following:

C#
static RasterImage LoadImage(string filename) 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      byte[] data = File.ReadAllBytes(filename); 
      using (MemoryStream ms = new MemoryStream(data)) 
         return codecs.Load(ms); 
   } 
} 

Replace the existing code in the SaveImage() method with the following:

C#
static void SaveImage(RasterImage image, string outputFilename) 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      MemoryStream outputStream = new MemoryStream(); 
      codecs.Save(image, outputStream, 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.

See Also

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


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