Table of Contents

Class ImageHelper

Namespace
VisioForge.Core.Helpers
Assembly
VisioForge.Core.dll

Provides comprehensive image processing utilities for RGB/RGBA image manipulation, stride calculations, and pixel format conversions. This static helper class handles low-level image operations including memory copying, color format conversions, image flipping, and stride calculations for various pixel formats (RGB24, RGB32, ARGB). Essential for video frame processing and image manipulation operations across the VisioForge media framework.

public static class ImageHelper

Inheritance

Inherited Members

Methods

DrawFilledRectangle(nint, int, int, Rectangle, Color)

Draws a filled rectangle on raw image data.

public static void DrawFilledRectangle(nint data, int width, int height, Rectangle rect, Color color)

Parameters

data nint

Pointer to the raw image data buffer.

width int

The width of the image in pixels.

height int

The height of the image in pixels.

rect Rectangle

The rectangle to fill.

color Color

The color to use for filling the rectangle.

Remarks

This method fills a rectangular area directly on the raw image buffer using fast image processing methods. This is more efficient than using GDI+ for simple filled rectangle operations.

DrawRGBImage(nint, int, int, nint, int, int, int, bool, ILogger)

Copies an uncompressed RGB image from a source buffer to a destination buffer at specified coordinates. Supports both RGB24 (3 bytes per pixel) and RGB32 (4 bytes per pixel) formats.

public static void DrawRGBImage(nint src, int srcWidth, int srcHeight, nint dest, int destWidth, int x, int y, bool draw32b, ILogger logger)

Parameters

src nint

Pointer to the source image buffer containing raw RGB pixel data.

srcWidth int

Width of the source image in pixels.

srcHeight int

Height of the source image in pixels.

dest nint

Pointer to the destination image buffer where the image will be drawn.

destWidth int

Width of the destination image in pixels (used for stride calculation).

x int

X-coordinate in the destination image where the source image will be placed.

y int

Y-coordinate in the destination image where the source image will be placed.

draw32b bool

If true, processes as RGB32 format (4 bytes per pixel); if false, processes as RGB24 format (3 bytes per pixel).

logger ILogger

Optional logger instance for error reporting. Pass null to disable logging.

DrawRectangle(nint, int, int, Rectangle, int, Color)

Draws a rectangle outline on raw image data.

public static void DrawRectangle(nint data, int width, int height, Rectangle rect, int lineSize, Color color)

Parameters

data nint

Pointer to the raw image data buffer.

width int

The width of the image in pixels.

height int

The height of the image in pixels.

rect Rectangle

The rectangle to draw.

lineSize int

The thickness of the rectangle lines in pixels.

color Color

The color to use for drawing the rectangle.

Remarks

This method draws a rectangle by filling four separate rectangles (top, bottom, left, and right lines) directly on the raw image buffer using fast image processing methods. This is more efficient than using GDI+ for simple rectangle drawing operations.

FlipRGBImage(byte[], int, int)

Flips an RGB24 image vertically by reversing the order of scan lines. This is commonly needed when working with bitmap formats that store pixels bottom-up.

public static byte[] FlipRGBImage(byte[] data, int width, int height)

Parameters

data byte[]

The raw RGB24 image data as a byte array (3 bytes per pixel).

width int

The width of the image in pixels.

height int

The height of the image in pixels.

Returns

byte[]

A new byte array containing the vertically flipped image data.

GetImageBufferSizeRGB(Bitmap)

Extension method that calculates the total buffer size required to store the bitmap's RGB data.

public static int GetImageBufferSizeRGB(this Bitmap bmp)

Parameters

bmp Bitmap

The bitmap to calculate buffer size for.

Returns

int

The total buffer size in bytes, accounting for stride alignment.

Remarks

This method calculates the buffer size by multiplying the stride (width with padding) by the height. The stride accounts for the alignment requirements of the bitmap's pixel format.

GetStrideByPixelSize(int, byte)

Calculates the stride (bytes per row) for an image based on arbitrary pixel size. The stride is aligned to 4-byte boundaries for optimal memory access.

public static int GetStrideByPixelSize(int width, byte pixelSize)

Parameters

width int

The width of the image in pixels.

pixelSize byte

The size of each pixel in bytes (e.g., 3 for RGB24, 4 for RGB32).

Returns

int

The stride value in bytes, aligned to 4-byte boundaries.

GetStrideRGB(int, PixelFormatX)

Calculates the stride (bytes per row) for an RGB image based on its width and pixel format. The stride is aligned to 4-byte boundaries as required by many graphics APIs.

public static int GetStrideRGB(int width, PixelFormatX pixelFormat)

Parameters

width int

The width of the image in pixels.

pixelFormat PixelFormatX

The pixel format of the image (Format24bppRgb, Format32bppArgb, etc.).

Returns

int

The stride value in bytes, aligned to 4-byte boundaries. Returns 0 for unsupported formats.

GetStrideRGB(int, PixelFormat)

Calculates the stride (row width in bytes) for an RGB image with proper alignment.

public static int GetStrideRGB(int width, PixelFormat pixelFormat)

Parameters

width int

The width of the image in pixels.

pixelFormat PixelFormat

The pixel format of the image.

Returns

int

The stride value in bytes, aligned to 4-byte boundaries.

Remarks

Stride represents the number of bytes in one row of image data, including padding. Windows bitmaps require each row to be aligned to 4-byte boundaries for performance. This method supports Format24bppRgb (3 bytes per pixel) and Format32bpp* formats (4 bytes per pixel). For unsupported formats, returns 0.

GetStrideRGB24(int)

Calculates the stride (bytes per row) for an RGB24 image (3 bytes per pixel). The stride is aligned to 4-byte boundaries for optimal memory access.

public static int GetStrideRGB24(int width)

Parameters

width int

The width of the image in pixels.

Returns

int

The stride value in bytes, aligned to 4-byte boundaries.

GetStrideRGB32(int)

Calculates the stride (bytes per row) for an RGB32 image (4 bytes per pixel). The stride is aligned to 4-byte boundaries for optimal memory access.

public static int GetStrideRGB32(int width)

Parameters

width int

The width of the image in pixels.

Returns

int

The stride value in bytes, aligned to 4-byte boundaries.

GetStrideYUY2(int)

Calculates the stride (bytes per row) for a YUY2 image format. YUY2 uses 2 bytes per pixel in a packed format (Y0 U0 Y1 V0).

public static int GetStrideYUY2(int width)

Parameters

width int

The width of the image in pixels.

Returns

int

The stride value in bytes (width * 2).

MakeCOLORREF(byte, byte, byte)

Creates a COLORREF value from individual red, green, and blue color components.

public static int MakeCOLORREF(byte r, byte g, byte b)

Parameters

r byte

The red component value (0-255).

g byte

The green component value (0-255).

b byte

The blue component value (0-255).

Returns

int

A 32-bit integer representing the COLORREF value in Windows GDI format (0x00BBGGRR).

MakeCOLORREF(Color)

Converts a System.Drawing.Color to a Windows COLORREF value.

public static int MakeCOLORREF(Color color)

Parameters

color Color

The color to convert.

Returns

int

An integer representing the COLORREF value in the format 0x00BBGGRR.

Remarks

COLORREF is a Windows API color format where colors are stored as 32-bit values with the red component in the low-order byte, green in the second byte, and blue in the third byte. The high-order byte is not used and should be zero.

MakeCOLORREF(SKColor)

Converts a SkiaSharp SKColor to a Windows COLORREF value.

public static int MakeCOLORREF(SKColor color)

Parameters

color SKColor

The SKColor to convert.

Returns

int

An integer representing the COLORREF value in the format 0x00BBGGRR.

Remarks

COLORREF is a Windows API color format where colors are stored as 32-bit values with the red component in the low-order byte, green in the second byte, and blue in the third byte. This overload allows conversion from SkiaSharp color objects.

RectangleConv(RectangleF)

Extension method that converts a RectangleF to a Rectangle by truncating floating-point values.

public static Rectangle RectangleConv(this RectangleF rect)

Parameters

rect RectangleF

The RectangleF to convert.

Returns

Rectangle

A Rectangle with integer coordinates.

Remarks

This method casts the floating-point X, Y, Width, and Height values to integers. Values are truncated (not rounded), so 1.9 becomes 1.