Table of Contents

Class ImageBuffer

Namespace
LMKit.Media.Image
Assembly
LM-Kit.NET.dll

Represents an unmanaged image buffer for pixel data, providing high-performance methods to load, save, convert, and manipulate images. This type owns a native handle and must be disposed to release unmanaged resources.

public sealed class ImageBuffer : IDisposable
Inheritance
ImageBuffer
Implements
Inherited Members

Examples

Example: Load and use with vision model

using LMKit.Model;
using LMKit.Media.Image;
using LMKit.TextGeneration;
using LMKit.Data;
using System;

// Load a vision-enabled model LM model = LM.LoadFromModelID("gemma-3-4b");

// Load an image using ImageBuffer image = ImageBuffer.LoadAsRGB("photo.jpg"); Console.WriteLine($"Image size: {image.Width}x{image.Height}");

// Create attachment for conversation var attachment = new Attachment(image);

// Use with MultiTurnConversation var conversation = new MultiTurnConversation(model); var response = conversation.Submit("What do you see in this image?", attachment); Console.WriteLine(response.Completion);

Example: Resize and convert image

using LMKit.Media.Image;
using System;

using ImageBuffer original = ImageBuffer.LoadAsRGB("large_photo.jpg"); Console.WriteLine($"Original: {original.Width}x{original.Height}");

// Resize to 512x512 using ImageBuffer resized = original.Resize(512, 512); Console.WriteLine($"Resized: {resized.Width}x{resized.Height}");

// Convert to grayscale using ImageBuffer grayscale = resized.ConvertGRAY8();

// Save result grayscale.SaveAsPng("processed.png");

Example: Load from byte array

using LMKit.Media.Image;
using System;
using System.IO;

// Load image data from any source byte[] imageData = File.ReadAllBytes("image.png");

using ImageBuffer image = ImageBuffer.LoadAsRGB(imageData); Console.WriteLine($"Loaded image: {image.Width}x{image.Height}, Format: {image.PixelFormat}");

Remarks

The ImageBuffer class is used to work with images in LM-Kit.NET, particularly for vision-enabled models and multimodal applications.

Key Features

Common Use Cases

  • Loading images for vision LLM analysis
  • Creating attachments for multimodal conversations
  • Image preprocessing before embedding generation
  • Image manipulation and transformation

Important
ImageBuffer implements IDisposable. Always dispose of instances when done to release unmanaged memory resources.

Properties

BitDepth

Gets the bit depth of the image based on the current pixel format.

Buffer

Gets a pointer to the raw pixel buffer.

BufferLength

Gets the total length, in bytes, of the pixel buffer.

Format

Gets the pixel format of the image.

Height

Gets the height of the image in pixels.

HorizontalResolution

Gets the horizontal resolution (DPI) stored in the image metadata.

PixCount

Gets the total number of pixels in the image.

Scan0

Gets a pointer to the first logical scan line (top row) of the image, taking into account the stride direction.

Stride

Gets the number of bytes in a single row (scan line) of the image.

VerticalResolution

Gets the vertical resolution (DPI) stored in the image metadata.

Width

Gets the width of the image in pixels.

Methods

Clone()

Creates a deep copy of this image buffer.

ConvertGRAY8()

Creates a new ImageBuffer with pixel data converted to 8-bit grayscale (GRAY8).

ConvertRGB24()

Creates a new ImageBuffer with pixel data converted to 24-bit RGB (RGB24).

ConvertRGBA32()

Creates a new ImageBuffer with pixel data converted to 32-bit ARGB (RGBA32).

Crop(int, int, int, int)

Returns a new ImageBuffer containing a copy of the specified rectangular region. The source buffer is not modified.

CropAuto(int, byte)

Automatically crops uniform borders from the image by scanning from each edge until a pixel differs from the border color, then returns the cropped buffer. The border color is sampled from the corners with a simple majority/consensus, improving robustness over single-pixel sampling.

Deskew(DeskewParameters)

Estimates page skew and, if significant, returns a new image with rotation correction applied.

DespeckleBitonal(DespeckleBitonalParameters)

Removes speckle noise (salt-and-pepper artifacts, scanner debris, photocopy specks) from a bitonal document image.

Dispose()

Releases all resources used by this ImageBuffer.

Draw(ImageBuffer, int, int)

Copies the entire source image onto this image at the specified destination coordinates. Both images must share the same pixel format.

DrawRegion(ImageBuffer, int, int, int, int, int, int)

Copies a rectangular region of the source image onto this image at the specified destination coordinates. Both images must share the same pixel format.

FlipHorizontal()

Flips the image horizontally (mirrors left to right).

FlipVertical()

Flips the image vertically (mirrors top to bottom).

GetScanLine(int)

Returns a pointer to the beginning of the specified scan line.

Invert()

Inverts each pixel component in the image buffer.

IsBlackAndWhite()

Determines whether the image is strictly black and white: every pixel is either pure black (all channels 0) or pure white (all channels 255), with no intermediate values. Works for any supported pixel format (GRAY8, RGB24, RGBA32).

IsBlank(IBounds, byte)

Determines whether the specified region of the image is blank (all pixels have the same or similar values within a tolerance).

IsBlank(byte)

Determines whether the image is blank (all pixels have the same or similar values within a tolerance).

Load(byte[])

Loads image data from a byte array, preserving the native pixel format.

Load(ReadOnlyMemory<byte>)

Loads image data from a memory block, preserving the native pixel format.

Load(ReadOnlySpan<byte>)

Loads image data from a span, preserving the native pixel format.

Load(string)

Loads an image from a file path, preserving its native pixel format. Grayscale images are loaded as GRAY8, images with alpha as RGBA32, and all others as RGB24.

LoadAsRGB(byte[])

Loads image data from the provided byte array and returns a new ImageBuffer in RGB24 format.

LoadAsRGB(ReadOnlyMemory<byte>)

Loads image data from the provided memory block and returns a new ImageBuffer in RGB24 format.

LoadAsRGB(ReadOnlySpan<byte>)

Loads image data from the provided span and returns a new ImageBuffer in RGB24 format.

LoadAsRGB(string)

Loads an image from the given file path, applies any necessary path fixes, and returns a new ImageBuffer in RGB24 format.

OtsuBinarize()

Converts the image to a binary (bitonal) representation using global Otsu thresholding.

Resize(int, int)

Resizes the image to the specified dimensions, without preserving the aspect ratio.

ResizeBox(int, int, Color32)

Resizes the image to fit within the specified box, adding padding with the given background color to preserve aspect ratio.

Rotate(int)

Rotates the image by the specified angle. Only 90, 180 or 270 degrees clockwise are supported.

SaveAsBmp(string, bool)

Saves the image to a BMP file at the specified path.

SaveAsJpeg(string, int)

Saves the image to a JPEG file at the specified path, using the given quality level.

SaveAsJpegBytes(int)

Saves the image as JPEG to a byte array without writing to disk.

SaveAsPdf(string, PdfGenerationOptions)

Saves the image as a single-page PDF file at the specified path.

SaveAsPng(string, int)

Saves the image to a PNG file at the specified path, using the given compression level.

SaveAsPngBytes(int)

Saves the image as PNG to a byte array without writing to disk.

SaveAsPnm(string)

Saves the image in PNM format to the specified file path. GRAY8 is saved as PGM (P5), RGB24 as PPM (P6), RGBA32 as PAM (P7).

SaveAsTga(string, bool)

Saves the image in TGA (Truevision TARGA) format to the specified file path.

SaveAsTiff(string)

Saves the image in TIFF format to the specified file path. Uses LZW compression automatically.

SaveAsWebp(string, int)

Saves the image in WebP format to the specified file path.

SaveAsWebpBytes(int)

Saves the image as WebP to a byte array without writing to disk.

SetResolution(float)

Sets both horizontal and vertical resolution to the same DPI value.

SetResolution(float, float)

Sets the horizontal and vertical resolution (DPI) metadata for the image.

SmartBinarize()

Converts the image to a clean black-and-white representation optimized for OCR and text extraction. Uses layout analysis to adapt to varying document conditions.

TryDetectBorderBackgroundColor(out Color32, float)

Attempts to estimate a uniform background color by analyzing only the image border (top and bottom rows, and left/right columns).

Share