Load Image from a Buffer - Console C#

This tutorial shows how to create a C# Windows console application that loads an image from a buffer.

Summary This tutorial covers how to load an image from a buffer in a C# Windows Console application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 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 Image from a Buffer - Console C# tutorial.

Create the Project and Add the 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 local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\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:


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. Add the following statements to the using block at the top.

using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
using System.Runtime.InteropServices; 

The code below shows 3 ways to load an image from a buffer:

  1. When the image data is in a byte array.
  2. When the image is stored in memory referenced by a pointer.
  3. When the image is encoded into a Base-64 text string. For this case, a sample text file is provide and can be downloaded using this link.

Add a new method called LoadImageBuffer() and call it inside the Main() method. Add the below code to load an image from a buffer.

unsafe static void LoadImageBuffer() 
   using (RasterCodecs codecs = new RasterCodecs()) 
      string fileName = @"C:\LEADTOOLS21\Resources\Images\image1.cmp"; 
      byte[] buffer = File.ReadAllBytes(fileName); 
      // 1. Load the image from a byte-array buffer 
      MemoryStream ms = new MemoryStream(buffer); 
      using (RasterImage image = codecs.Load(ms)) 
         Console.WriteLine($"Image loaded successfully!\nImage size is {image.ImageWidth} by {image.ImageHeight}\n"); 
      // Load the file data into an unmanaged pointer 
      IntPtr ptr = Marshal.AllocHGlobal(buffer.Length); 
      Marshal.Copy(buffer, 0, ptr, buffer.Length); 
      // 2. Load the image from memory using unmanaged pointer 
      UnmanagedMemoryStream unmanagedStream = new UnmanagedMemoryStream((byte*)ptr.ToPointer(), buffer.Length); 
      using (RasterImage image = codecs.Load(unmanagedStream)) 
         Console.WriteLine($"Image loaded successfully!\nImage size is {image.ImageWidth} by {image.ImageHeight}\n"); 
      // load base-64 coded image from text file into string 
      string b64String = File.ReadAllText(@"C:\LEADTOOLS21\Resources\Images\Load-Image-from-a-Buffer-base64-image.txt"); 
      // 3. Load image from Base64 encoded string 
      if (b64String != null) 
         byte[] bytes = Convert.FromBase64String(b64String); 
         using (MemoryStream b64ms = new MemoryStream(bytes)) 
         using (RasterImage image = codecs.Load(b64ms)) 
            Console.WriteLine($"Image loaded successfully!\nImage size is {image.ImageWidth} by {image.ImageHeight}"); 


In order to be able to compile this code, open the Project Properties from the Project menu then go to Build tab and enable the Allow unsafe code option.

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 loads any image that is supported by the above codecs filters from a buffer. The application also allows the options to load an image from a Base64 string.


This tutorial showed how to load an image from a buffer. Also it showed how to use the RasterCodecs and RasterImage classes.


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