Class ImageBuffer
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
- Load images from files or byte arrays via LoadAsRGB(string)
- Save images in various formats via SaveAsPng(string, int) or SaveAsBmp(string)
- Resize, rotate, and transform images
- Convert between pixel formats (RGB, RGBA, Grayscale)
- Direct pixel buffer access for custom processing
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
- 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).
- 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.
- Dispose()
Releases all resources used by this ImageBuffer.
- GetScanLine(int)
Returns a pointer to the beginning of the specified scan line.
- Invert()
Inverts each pixel component in the image buffer.
- IsBlank(byte)
Determines whether the image is blank (all pixels have the same or similar values within a tolerance).
- 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.
- 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)
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.
- SaveAsPng(string, int)
Saves the image to a PNG file at the specified path, using the given compression level.
- 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.
- 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).