Class BitmapHelper
- Namespace
- VisioForge.Core.Helpers
- Assembly
- VisioForge.Core.dll
Provides utility methods for converting between Bitmap objects and various raw image formats.
public static class BitmapHelperInheritance
Inherited Members
Remarks
This helper class contains methods for converting between System.Drawing.Bitmap, raw byte arrays, IntPtr-based image data, JPEG images, and various video frame formats including RAWImage, RAWVideoFrame, VideoFrame, and VideoFrameX. It supports multiple pixel formats including RGB24, RGB32, and RGBA, and provides functionality for image resizing, cropping, flipping, and format conversion.
Methods
ARGBBitmapToVideoFrameX(Bitmap)
Converts an ARGB bitmap to a VideoFrameX object.
public static VideoFrameX ARGBBitmapToVideoFrameX(Bitmap bitmap)Parameters
bitmapBitmap-
The source bitmap to convert (must be in ARGB format).
Returns
- VideoFrameX
-
A new VisioForge.Core.Types.X.VideoFrameX object containing the bitmap data in ARGB format.
Remarks
This method allocates a new VideoFrameX structure, copies the bitmap data in ARGB32 format, and sets the appropriate width, height, stride, and format properties. The returned VideoFrameX must be properly disposed to free unmanaged memory.
BitmapToIntPtr(Bitmap, nint, bool)
Converts a Bitmap to raw byte array stored at the specified IntPtr location.
public static void BitmapToIntPtr(Bitmap sourceBmp, nint destArray, bool flip = false)Parameters
sourceBmpBitmap-
The source bitmap to convert.
destArraynint-
The destination IntPtr where the raw image data will be written. Must be pre-allocated.
flipbool-
If set to
true, the image will be flipped vertically before conversion.
Remarks
This method supports Format24bppRgb and Format32bppArgb pixel formats. The destination array must be allocated with sufficient memory before calling this method. The method locks the bitmap, copies the data line by line to handle stride differences, and then unlocks the bitmap.
Exceptions
- Exception
-
Thrown when destArray is IntPtr.Zero (not allocated).
BitmapToIntPtr(Bitmap, nint, int, int, PixelFormat)
Converts a Bitmap to raw byte array with resizing support.
public static void BitmapToIntPtr(Bitmap sourceBmp, nint destArray, int width, int height, PixelFormat pixelFormat)Parameters
sourceBmpBitmap-
The source bitmap to convert.
destArraynint-
The destination IntPtr where the raw image data will be written. Must be pre-allocated.
widthint-
The target width for the output image.
heightint-
The target height for the output image.
pixelFormatPixelFormat-
The target pixel format (Format24bppRgb or Format32bppArgb).
Remarks
If the source bitmap dimensions differ from the specified width and height, the image will be automatically resized. The method handles stride differences between source and destination to ensure proper data alignment.
Exceptions
- Exception
-
Thrown when destArray is IntPtr.Zero (not allocated).
BitmapToIntPtrFlip(ref Bitmap, int, int, PixelFormat, nint, int)
Converts a bitmap to raw image data with vertical flip.
public static void BitmapToIntPtrFlip(ref Bitmap sourceBitmap, int width, int height, PixelFormat pixelFormat, nint destPtr, int destLen)Parameters
sourceBitmapBitmap-
The source bitmap to convert.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
pixelFormatPixelFormat-
The pixel format of the bitmap.
destPtrnint-
Pointer to the destination buffer where raw image data will be written.
destLenint-
The length of the destination buffer in bytes.
Remarks
This method flips the bitmap vertically before copying the data, then restores the original orientation. The destination pointer must be pre-allocated with sufficient memory. If parameters don't match or destPtr is null, the method returns without modification.
ByteArrayToBitmap(byte[], int, int, PixelFormat, ref Bitmap, bool)
Converts a byte array to a pre-existing bitmap with optional vertical flip.
public static void ByteArrayToBitmap(byte[] source, int width, int height, PixelFormat pixelFormat, ref Bitmap preDefinedOutput, bool flip = false)Parameters
sourcebyte[]-
The source byte array containing raw image data.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
pixelFormatPixelFormat-
The pixel format of the source data.
preDefinedOutputBitmap-
A pre-existing bitmap that must match the specified width, height, and pixel format.
flipbool-
If set to
true, the image will be flipped vertically after copying.
Remarks
This method is designed for performance-critical scenarios where the output bitmap is reused to avoid repeated memory allocations. The pre-defined output bitmap must be properly initialized with matching dimensions and pixel format. If parameters don't match or source is null, the method returns without modification.
ByteArrayToBitmap(byte[], int, int, PixelFormat, bool)
Converts a byte array to a new bitmap with optional vertical flip.
public static Bitmap ByteArrayToBitmap(byte[] source, int width, int height, PixelFormat pixelFormat, bool flip = false)Parameters
sourcebyte[]-
The source byte array containing raw image data.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
pixelFormatPixelFormat-
The pixel format for the output bitmap.
flipbool-
If set to
true, the image will be flipped vertically.
Returns
Remarks
This method creates a new bitmap and copies the raw image data from the byte array. The caller is responsible for disposing the returned bitmap when no longer needed.
ByteArrayToBitmap(nint, int, int, PixelFormat, ref Bitmap)
Copies raw image data from an IntPtr to a pre-existing bitmap.
public static void ByteArrayToBitmap(nint srcArray, int srcWidth, int srcHeight, PixelFormat pixelFormat, ref Bitmap destBmp)Parameters
srcArraynint-
Pointer to the source raw image data.
srcWidthint-
The width of the source image in pixels.
srcHeightint-
The height of the source image in pixels.
pixelFormatPixelFormat-
The pixel format of the source data.
destBmpBitmap-
A pre-existing destination bitmap that must match the specified dimensions and pixel format.
Remarks
This method is designed for performance-critical scenarios where the destination bitmap is reused. The destination bitmap must be properly initialized with matching dimensions and pixel format. If parameters don't match or srcArray is null, the method returns without modification.
ByteArrayToBitmapFlip(byte[], int, int, PixelFormat, ref Bitmap)
Converts a byte array to a pre-existing bitmap with automatic vertical flip.
public static void ByteArrayToBitmapFlip(byte[] source, int width, int height, PixelFormat pixelFormat, ref Bitmap preDefinedOutput)Parameters
sourcebyte[]-
The source byte array containing raw image data.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
pixelFormatPixelFormat-
The pixel format of the source data.
preDefinedOutputBitmap-
A pre-existing bitmap that must match the specified width, height, and pixel format.
Remarks
This method is designed for performance-critical scenarios where the output bitmap is reused. The pre-defined output bitmap must be properly initialized with matching dimensions and pixel format. The image is automatically flipped vertically after the data is copied. If parameters don't match or source is null, the method returns without modification.
ByteArrayToJpeg(byte[], int, int, PixelFormat, out byte[], bool)
Converts a raw image byte array to a JPEG byte array.
public static void ByteArrayToJpeg(byte[] source, int width, int height, PixelFormat pixelFormat, out byte[] jpeg, bool flip = false)Parameters
sourcebyte[]-
The source byte array containing raw image data.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
pixelFormatPixelFormat-
The pixel format of the source data.
jpegbyte[]-
Output parameter that receives the JPEG-encoded image as a byte array.
flipbool-
If set to
true, the image will be flipped vertically before JPEG encoding.
Remarks
This method creates a temporary bitmap from the raw byte array, optionally flips it, and then encodes it to JPEG format using the default JPEG quality settings. The temporary bitmap is disposed after conversion. If the source array is null, the jpeg output parameter will be set to null.
Convert(Bitmap, PixelFormat)
Converts a bitmap to a different pixel format.
public static Bitmap Convert(Bitmap source, PixelFormat px)Parameters
sourceBitmap-
The source bitmap to convert.
pxPixelFormat-
The target pixel format.
Returns
Remarks
This method creates a new bitmap with the target pixel format and copies the image data from the source using GDI+ Graphics drawing. The source bitmap is not modified or disposed.
Crop(Bitmap, Rectangle)
Crops a bitmap to the specified rectangle.
public static Bitmap Crop(Bitmap source, Rectangle cropRect)Parameters
sourceBitmap-
The source bitmap to crop.
cropRectRectangle-
The rectangle defining the crop area in the source bitmap.
Returns
Remarks
This method creates a new bitmap with dimensions matching the crop rectangle and copies the specified region from the source. The source bitmap is not modified or disposed.
GenerateImageWithText(string)
Generates a bitmap image containing the specified text.
public static Bitmap GenerateImageWithText(string text)Parameters
textstring-
The text to render in the image.
Returns
Remarks
This method uses Arial 24pt font to render black text on a white background. The bitmap size is automatically calculated to fit the text dimensions. The caller is responsible for disposing the returned bitmap when no longer needed.
GetEncoderInfo(string)
Retrieves the encoder information for the specified MIME type.
public static ImageCodecInfo GetEncoderInfo(string mimeType)Parameters
mimeTypestring-
The MIME type of the desired encoder (e.g., "image/jpeg", "image/png").
Returns
- ImageCodecInfo
-
The ImageCodecInfo object for the specified MIME type, or
nullif not found.
Remarks
This method iterates through all available image encoders on the system and returns the first one that matches the specified MIME type. Common MIME types include "image/jpeg", "image/png", "image/bmp", etc.
IntPtrToBitmap(nint, int, int, PixelFormat, bool)
Converts raw image data from an IntPtr to a System.Drawing.Bitmap.
public static Bitmap IntPtrToBitmap(nint source, int width, int height, PixelFormat pixelFormat, bool horizontalFlip = false)Parameters
sourcenint-
Pointer to the source raw image data.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
pixelFormatPixelFormat-
The pixel format of the source data (Format24bppRgb or Format32bppArgb).
horizontalFlipbool-
If set to
true, the image will be flipped horizontally.
Returns
Remarks
This method creates a new bitmap, locks its bits, copies the data from the source IntPtr, and optionally applies horizontal flipping using fast image processing methods. The source data must be properly aligned according to the stride requirements for the specified pixel format.
Exceptions
- Exception
-
Thrown when the pixel format is not Format24bppRgb or Format32bppArgb.
IntPtrToJpeg(nint, int, int, int, PixelFormat, out byte[], bool)
Converts raw image data from an IntPtr to a JPEG byte array.
public static void IntPtrToJpeg(nint source, int sourceLen, int width, int height, PixelFormat pixelFormat, out byte[] jpeg, bool flip = false)Parameters
sourcenint-
Pointer to the source raw image data.
sourceLenint-
The length of the source data in bytes.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
pixelFormatPixelFormat-
The pixel format of the source data.
jpegbyte[]-
Output parameter that receives the JPEG-encoded image as a byte array.
flipbool-
If set to
true, the image will be flipped vertically before JPEG encoding.
Remarks
This method creates a temporary bitmap from the raw data, optionally flips it, and then encodes it to JPEG format. The temporary bitmap is disposed after conversion. If the source pointer is null or the length is zero, the jpeg output parameter will be set to null.
IsBitmapDisposed(Bitmap)
Extension method that determines whether a bitmap has been disposed.
public static bool IsBitmapDisposed(this Bitmap bitmap)Parameters
bitmapBitmap-
The bitmap to check.
Returns
- bool
-
trueif the bitmap has been disposed; otherwise,false.
Remarks
This method attempts to access the Width property of the bitmap. If the bitmap has been disposed, an ObjectDisposedException or ArgumentException will be thrown and caught, returning true. This is useful for validating bitmap state before attempting operations that would fail on disposed objects.
JpegToByteArray(byte[], int, int, PixelFormat, out nint, out int, bool)
Decodes a JPEG byte array to raw image data stored in an IntPtr.
public static void JpegToByteArray(byte[] jpeg, int width, int height, PixelFormat pixelFormat, out nint output, out int outputSize, bool flip = false)Parameters
jpegbyte[]-
The JPEG-encoded image as a byte array.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
pixelFormatPixelFormat-
The desired pixel format for the output data.
outputnint-
Output parameter that receives an IntPtr to the decoded raw image data.
outputSizeint-
Output parameter that receives the size of the output data in bytes.
flipbool-
If set to
true, the image will be flipped vertically after decoding.
Remarks
This method decodes the JPEG image to a bitmap, optionally flips it, and copies the raw data to unmanaged memory allocated with Marshal.AllocCoTaskMem. The caller is responsible for freeing the memory using Marshal.FreeCoTaskMem when done. If the jpeg array is null, both output parameters will be set to zero/IntPtr.Zero.
JpegToByteArray(nint, int, int, int, PixelFormat, out nint, out int, bool)
Decodes a JPEG image from an IntPtr to raw image data stored in another IntPtr.
public static void JpegToByteArray(nint source, int sourceSize, int width, int height, PixelFormat pixelFormat, out nint output, out int outputSize, bool flip = false)Parameters
sourcenint-
Pointer to the JPEG-encoded image data.
sourceSizeint-
The size of the JPEG data in bytes.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
pixelFormatPixelFormat-
The desired pixel format for the output data.
outputnint-
Output parameter that receives an IntPtr to the decoded raw image data.
outputSizeint-
Output parameter that receives the size of the output data in bytes.
flipbool-
If set to
true, the image will be flipped vertically after decoding.
Remarks
This method copies the JPEG data from the source IntPtr to a managed byte array, decodes it to a bitmap, optionally flips it, and copies the raw data to unmanaged memory allocated with Marshal.AllocCoTaskMem. The caller is responsible for freeing the memory using Marshal.FreeCoTaskMem when done. If the source pointer is null, both output parameters will be set to zero/IntPtr.Zero.
MeasureTextFake(string, Font)
Measures the size of text when rendered with the specified font.
public static Size MeasureTextFake(string text, Font font)Parameters
Returns
Remarks
This method creates a temporary 100x100 bitmap and Graphics object to measure the text size. It's called "fake" because it uses a temporary bitmap rather than measuring against an actual target surface. The method is useful for pre-calculating text dimensions before rendering.
RAWImageToBitmap(RAWImage, bool)
Converts a RAWImage to a System.Drawing.Bitmap.
public static Bitmap RAWImageToBitmap(RAWImage frame, bool horizontalFlip = false)Parameters
frameRAWImage-
The RAWImage frame to convert.
horizontalFlipbool-
If set to
true, the image will be flipped horizontally.
Returns
Remarks
The method automatically determines the appropriate pixel format based on the RAWImage colorspace. Supported colorspaces are RGB, BGR (both convert to Format24bppRgb), and RGBA (converts to Format32bppArgb).
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the frame colorspace is not supported.
RAWVideoFrameToBitmap(RAWVideoFrame, bool)
Converts a RAWVideoFrame to a System.Drawing.Bitmap.
public static Bitmap RAWVideoFrameToBitmap(RAWVideoFrame frame, bool horizontalFlip = false)Parameters
frameRAWVideoFrame-
The RAWVideoFrame to convert.
horizontalFlipbool-
If set to
true, the image will be flipped horizontally.
Returns
Remarks
The method automatically determines the appropriate pixel format based on the frame colorspace. Supported colorspaces are RGB, BGR (both convert to Format24bppRgb), and RGBA (converts to Format32bppArgb).
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the frame colorspace is not supported.
RGBABitmapToRAWImage(Bitmap)
Converts an RGBA bitmap to a RAWVideoFrame object.
public static RAWVideoFrame RGBABitmapToRAWImage(Bitmap bitmap)Parameters
bitmapBitmap-
The source bitmap to convert (must be in RGBA format).
Returns
- RAWVideoFrame
-
A new VisioForge.Core.Types.RAWVideoFrame object containing the bitmap data in RGBA format.
Remarks
This method allocates a new RAWVideoFrame structure with initialized RAWBaseVideoInfo, copies the bitmap data in RGBA32 format, and sets the appropriate properties. The returned RAWVideoFrame must be properly disposed to free unmanaged memory.
RGBBitmapToRAWImage(Bitmap)
Converts an RGB bitmap to a RAWImage object.
public static RAWImage RGBBitmapToRAWImage(Bitmap bitmap)Parameters
bitmapBitmap-
The source bitmap to convert.
Returns
- RAWImage
-
A new VisioForge.Core.Types.RAWImage object containing the bitmap data in RGB format.
Remarks
This method allocates a new RAWImage structure, copies the bitmap data in RGB24 format, and sets the appropriate width, height, stride, and colorspace properties. The returned RAWImage must be properly disposed to free unmanaged memory.
RGBBitmapToRAWVideoFrame(Bitmap)
Converts an RGB bitmap to a RAWVideoFrame object.
public static RAWVideoFrame RGBBitmapToRAWVideoFrame(Bitmap bitmap)Parameters
bitmapBitmap-
The source bitmap to convert.
Returns
- RAWVideoFrame
-
A new VisioForge.Core.Types.RAWVideoFrame object containing the bitmap data in RGB format.
Remarks
This method allocates a new RAWVideoFrame structure with initialized RAWBaseVideoInfo, copies the bitmap data in RGB24 format, and sets the appropriate properties. The returned RAWVideoFrame must be properly disposed to free unmanaged memory.
RGBBitmapToVideoFrame(Bitmap)
Converts an RGB bitmap to a VideoFrame object.
public static VideoFrame RGBBitmapToVideoFrame(Bitmap bitmap)Parameters
bitmapBitmap-
The source bitmap to convert.
Returns
- VideoFrame
-
A new VisioForge.Core.Types.VideoFrame object containing the bitmap data in RGB format.
Remarks
This method allocates a new VideoFrame structure with initialized RAWBaseVideoInfo, copies the bitmap data in RGB24 format, and sets the appropriate properties. The returned VideoFrame must be properly disposed to free unmanaged memory.
RGBBitmapToVideoFrameX(Bitmap)
Converts an RGB bitmap to a VideoFrameX object.
public static VideoFrameX RGBBitmapToVideoFrameX(Bitmap bitmap)Parameters
bitmapBitmap-
The source bitmap to convert.
Returns
- VideoFrameX
-
A new VisioForge.Core.Types.X.VideoFrameX object containing the bitmap data in RGB format.
Remarks
This method allocates a new VideoFrameX structure, copies the bitmap data in RGB24 format, and sets the appropriate width, height, stride, and format properties. The returned VideoFrameX must be properly disposed to free unmanaged memory.
ResizeImage(Image, int, int, bool)
Extension method that resizes an image to the specified dimensions.
public static Bitmap ResizeImage(this Image image, int width, int height, bool fast = false)Parameters
imageImage-
The source image to resize. This image will be disposed after resizing.
widthint-
The target width in pixels.
heightint-
The target height in pixels.
fastbool-
If set to
true, uses fast but lower quality resizing (NearestNeighbor interpolation); iffalse, uses high quality resizing (HighQualityBicubic interpolation).
Returns
Remarks
The method uses different Graphics settings based on the fast parameter:
- Fast mode: Uses NearestNeighbor interpolation, HighSpeed smoothing, and SourceCopy compositing for maximum performance.
- Quality mode: Uses HighQualityBicubic interpolation, HighQuality smoothing, and proper pixel offset for best visual quality. The original image is disposed after the operation, so ensure you don't need it afterwards.
SaveFrame(nint, int, int, PixelFormat, string)
Saves raw image data to a bitmap file.
public static void SaveFrame(nint data, int width, int height, PixelFormat pf, string filename)Parameters
datanint-
Pointer to the raw image data.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
pfPixelFormat-
The pixel format of the image data.
filenamestring-
The full path where the bitmap file will be saved.
Remarks
This method converts the raw image data to a bitmap and saves it in BMP format. The bitmap is automatically disposed after saving.
ToBitmap(VideoFrame, bool)
Extension method that converts a VideoFrame to a System.Drawing.Bitmap.
public static Bitmap ToBitmap(this VideoFrame frame, bool horizontalFlip = false)Parameters
frameVideoFrame-
The VideoFrame to convert.
horizontalFlipbool-
If set to
true, the image will be flipped horizontally.
Returns
Remarks
This extension method provides a convenient fluent syntax for converting VideoFrame objects to Bitmap. Supported colorspaces are RGB, BGR (both convert to Format24bppRgb), and RGBA (converts to Format32bppArgb).
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the frame colorspace is not supported.
ToBitmap(VideoFrameX, bool)
Extension method that converts a VideoFrameX to a System.Drawing.Bitmap.
public static Bitmap ToBitmap(this VideoFrameX frame, bool horizontalFlip = false)Parameters
frameVideoFrameX-
The VideoFrameX to convert.
horizontalFlipbool-
If set to
true, the image will be flipped horizontally.
Returns
Remarks
This extension method provides a convenient fluent syntax for converting VideoFrameX objects to Bitmap. The method supports automatic colorspace conversion for various video formats.
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the frame format is not supported.
VideoFrameToBitmap(VideoFrame, bool)
Converts a VideoFrame to a System.Drawing.Bitmap.
public static Bitmap VideoFrameToBitmap(VideoFrame frame, bool horizontalFlip = false)Parameters
frameVideoFrame-
The VideoFrame to convert.
horizontalFlipbool-
If set to
true, the image will be flipped horizontally.
Returns
Remarks
The method automatically determines the appropriate pixel format based on the frame colorspace. Supported colorspaces are RGB, BGR (both convert to Format24bppRgb), and RGBA (converts to Format32bppArgb).
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the frame colorspace is not supported.
VideoFrameToBitmap(VideoFrameX, bool)
Converts a VideoFrameX to a System.Drawing.Bitmap with automatic colorspace conversion.
public static Bitmap VideoFrameToBitmap(VideoFrameX frame, bool horizontalFlip = false)Parameters
frameVideoFrameX-
The VideoFrameX to convert.
horizontalFlipbool-
If set to
true, the image will be flipped horizontally.
Returns
Remarks
This method supports multiple video formats including RGB, BGR, RGBA, ARGB, and BGRA. For formats that are not directly compatible with GDI+ (RGB, RGBA, ARGB), the method performs colorspace conversion to BGR or BGRA format before creating the bitmap.
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the frame format is not supported.
VideoFrameXToBitmap(VideoFrameX, bool)
Converts a VideoFrameX to a System.Drawing.Bitmap (RGB/RGBA formats only).
public static Bitmap VideoFrameXToBitmap(VideoFrameX frame, bool horizontalFlip = false)Parameters
frameVideoFrameX-
The VideoFrameX to convert.
horizontalFlipbool-
If set to
true, the image will be flipped horizontally.
Returns
Remarks
This method is optimized for VideoFrameX objects with RGB, BGR, or RGBA formats. It does not perform colorspace conversion, so the input format must match one of the supported formats.
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the frame format is not RGB, BGR, or RGBA.