Load and Save Images - C# .NET 6

This tutorial shows how to load and save an image file in a C# .NET 6 Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to load and save an image file using the RasterCodecs and RasterImage classes in a C# .NET 6 Console application.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console application
IDE Visual Studio 2022
Runtime Target .NET 6 or higher
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 this 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 the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

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:

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 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 InitLEAD method. This tutorial uses the following test image C:\LEADTOOLS23\Resources\Images\image1.cmp

C#
static void Main(string[] args) 
{ 
    InitLEAD(); 
 
    RasterImage image = LoadImage(@"C:\LEADTOOLS23\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()) 
   { 
      // If wanting to load from memory 
      //byte[] bytes = File.ReadAllBytes(filename); 
      //using (MemoryStream ms = new MemoryStream(bytes)) 
      //{ 
      //   ms.Position = 0; 
      //   return codecs.Load(ms); 
      //} 
      return codecs.Load(filename); 
   } 
} 

Note: LEADTOOLS supports loading from memory streams. The commented out code above shows one way to load a stream into a RasterImage object.

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) 
{ 
    InitLEAD(); 
 
    RasterImage image = LoadImage(@"C:\LEADTOOLS23\Resources\Images\image1.cmp"); 
    SaveImage(image, @"C:\LEADTOOLS23\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:\LEADTOOLS23\Resources\Images\output.jpg

C#
static void SaveImage(RasterImage image, string outputFilename) 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      // If wanting to save to memory 
      //MemoryStream ms = new MemoryStream(); 
      //codecs.Save(image, ms, RasterImageFormat.Jpeg, 0); 
      codecs.Save(image, outputFilename, RasterImageFormat.Jpeg, 0); 
   } 
} 

Note: LEADTOOLS also supports saving to memory streams. The commented out code above shows how to save to a stream from a RasterImage object.

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 6 Console Application.

See Also

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


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