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 |
|
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.
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:
Leadtools.Formats.Raster.CommonIf local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:
Leadtools.dllLeadtools.Codecs.dllLeadtools.Codecs.Cmp.dllFor a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
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.
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:
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 bufferMemoryStream 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 pointerIntPtr ptr = Marshal.AllocHGlobal(buffer.Length);Marshal.Copy(buffer, 0, ptr, buffer.Length);// 2. Load the image from memory using unmanaged pointerUnmanagedMemoryStream 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 stringstring b64String = File.ReadAllText(@"C:\LEADTOOLS21\Resources\Images\Load-Image-from-a-Buffer-base64-image.txt");// 3. Load image from Base64 encoded stringif (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
Projectmenu then go toBuildtab and enable theAllow unsafe codeoption.
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.