Load Image from a Buffer - C# .NET Core

This tutorial shows how to load an image from a buffer in a C# .NET Core application using the LEADTOOLS SDK.

Summary This tutorial covers how to load an image from a buffer in a C# .NET Core Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Core 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 Image from a Buffer - C# .NET Core 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 via NuGet packages.

This tutorial requires the following NuGet package:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

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 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 of Program.cs.

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

The code below shows three 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 Base64 text string. For this case, a sample text file is provided and can be downloaded using this link.

Add a new method in the Program class, named LoadImageBuffer(). This method will be called inside the Main() method, below the call to the set license code. Add the code below to the new method to load an image from a buffer in the three different ways shown above.

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(@"FILE PATH TO BASE64 STRING FILE"); 
      // 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, select Project -> Properties. Navigate to the Build tab and enable Allow unsafe code.

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application loads the specified image 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.6.17
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.