This tutorial shows how to load an image from a buffer in a C# .NET 6 console application using the LEADTOOLS SDK.
| Overview | |
|---|---|
| Summary | This tutorial covers how to load an image from a buffer 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 |
|
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 6 tutorial.
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:
Leadtools.Formats.Raster.CommonFor a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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:
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:
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:\LEADTOOLS22\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(@"FILE PATH TO BASE64 STRING FILE");// 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, select Project ->
Properties . Navigate to theBuildtab and enableAllow unsafe code.
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.