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.

Overview  
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
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 - Console C# tutorial.

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

C#
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.

C#
unsafe static void LoadImageBuffer() 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      string fileName = @"C:\LEADTOOLS22\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"); 
      unmanagedStream.Dispose(); 
      Marshal.FreeHGlobal(ptr); 
 
      // load base-64 coded image from text file into string 
      string b64String = File.ReadAllText(@"C:\LEADTOOLS22\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}"); 
      } 
   } 
} 

Note

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.

Wrap-up

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

See-Also

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

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