Class SkiaHelper
- Namespace
- VisioForge.Core.Helpers
- Assembly
- VisioForge.Core.dll
Provides helper methods for working with SkiaSharp bitmaps, including format conversions, image transformations, and interoperability with video frames.
public static class SkiaHelperInheritance
Inherited Members
Methods
BGRAToSKBitmap(byte[], int, int, int)
Converts a byte array of BGRA pixels (bytes B, G, R, A) to an SKBitmap with the platform colour type. This is the memory layout of a 32-bit Windows bitmap and of the 32-bit frames the DirectShow engines report as VisioForge.Core.Types.RAWVideoColorSpace.RGBA. Only the rows that fit in the array are copied; the rest of the bitmap is transparent.
public static SKBitmap BGRAToSKBitmap(byte[] data, int width, int height, int stride)Parameters
databyte[]-
The BGRA pixel data.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
strideint-
The distance between the starts of two rows in bytes; zero means packed rows (
width* 4); a negative stride (a bottom-up image) is rejected.
Returns
- SKBitmap
-
A new SKBitmap containing the image data.
Exceptions
- ArgumentNullException
-
Thrown when
datais null. - ArgumentOutOfRangeException
-
Thrown when the stride is negative or shorter than one row of pixels.
BGRAToSKBitmap(nint, int, int, int, long)
Converts unmanaged BGRA pixels (bytes B, G, R, A) to an SKBitmap with the platform colour type.
This is the memory layout of a 32-bit Windows bitmap and of the 32-bit frames the DirectShow engines report as
VisioForge.Core.Types.RAWVideoColorSpace.RGBA. Only the rows that fit in dataSize bytes are copied;
the rest of the bitmap is transparent.
public static SKBitmap BGRAToSKBitmap(nint data, int width, int height, int stride, long dataSize)Parameters
datanint-
A pointer to the BGRA pixel data.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
strideint-
The distance between the starts of two rows in bytes; zero means packed rows (
width* 4); a negative stride (a bottom-up image) is rejected. dataSizelong-
The number of bytes readable at
data; must be positive.
Returns
- SKBitmap
-
A new SKBitmap containing the image data.
Exceptions
- ArgumentNullException
-
Thrown when
datais zero anddataSizeis positive. - ArgumentOutOfRangeException
-
Thrown when
dataSizeis not positive, or the stride is negative or shorter than one row of pixels.
ByteArrayRGBToSKBitmap(byte[], int, int)
Converts a byte array containing RGB pixel data to an SKBitmap. The RGB data is expanded to RGBA, so the bitmap is created with an explicit SkiaSharp.SKColorType.Rgba8888 color type. Using the default constructor here would yield the platform-native BGRA bitmap on Windows, and the RGBA bytes would then be read as BGRA, swapping the red and blue channels (a blue tint).
public static SKBitmap ByteArrayRGBToSKBitmap(byte[] frame, int width, int height)Parameters
framebyte[]-
The byte array containing RGB pixel data, each row padded to a multiple of 4 bytes.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
Returns
- SKBitmap
-
A new SKBitmap containing the converted image data.
Exceptions
- ArgumentNullException
-
Thrown when
frameis null. - ArgumentOutOfRangeException
-
Thrown when
frameis shorter than the image.
FlipHorizontal(SKBitmap)
Creates a new SKBitmap that is horizontally flipped (mirrored) from the original.
public static SKBitmap FlipHorizontal(this SKBitmap bmp)Parameters
bmpSKBitmap-
The original SKBitmap to flip.
Returns
- SKBitmap
-
A new SKBitmap containing the horizontally flipped image.
FlipVertical(SKBitmap)
Creates a new SKBitmap that is vertically flipped from the original.
public static SKBitmap FlipVertical(this SKBitmap bmp)Parameters
bmpSKBitmap-
The original SKBitmap to flip.
Returns
- SKBitmap
-
A new SKBitmap containing the vertically flipped image.
RGBAToSKBitmap(byte[], int, int, int)
Converts a byte array of RGBA pixels (bytes R, G, B, A) to an SKBitmap with the platform colour type. Only the rows that fit in the array are copied; the rest of the bitmap is transparent.
public static SKBitmap RGBAToSKBitmap(byte[] data, int width, int height, int stride)Parameters
databyte[]-
The RGBA pixel data.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
strideint-
The distance between the starts of two rows in bytes; zero means packed rows (
width* 4); a negative stride (a bottom-up image) is rejected.
Returns
- SKBitmap
-
A new SKBitmap containing the image data.
Exceptions
- ArgumentNullException
-
Thrown when
datais null. - ArgumentOutOfRangeException
-
Thrown when the stride is negative or shorter than one row of pixels.
RGBAToSKBitmap(nint, int, int, int, long)
Converts unmanaged RGBA pixels (bytes R, G, B, A) to an SKBitmap with the platform colour type.
Only the rows that fit in dataSize bytes are copied; the rest of the bitmap is transparent.
public static SKBitmap RGBAToSKBitmap(nint data, int width, int height, int stride, long dataSize)Parameters
datanint-
A pointer to the RGBA pixel data.
widthint-
The width of the image in pixels.
heightint-
The height of the image in pixels.
strideint-
The distance between the starts of two rows in bytes; zero means packed rows (
width* 4); a negative stride (a bottom-up image) is rejected. dataSizelong-
The number of bytes readable at
data; must be positive.
Returns
- SKBitmap
-
A new SKBitmap containing the image data.
Exceptions
- ArgumentNullException
-
Thrown when
datais zero anddataSizeis positive. - ArgumentOutOfRangeException
-
Thrown when
dataSizeis not positive, or the stride is negative or shorter than one row of pixels.
Rotate(SKBitmap, SKRotationAngle)
Rotates an SKBitmap by the specified angle.
public static SKBitmap Rotate(this SKBitmap originalBitmap, SKRotationAngle rotation)Parameters
originalBitmapSKBitmap-
The original SKBitmap to rotate.
rotationSKRotationAngle-
The rotation angle to apply (90, 180, or 270 degrees).
Returns
- SKBitmap
-
A new SKBitmap containing the rotated image with dimensions adjusted as necessary.
SaveToFile(SKBitmap, string, SKEncodedImageFormat, int)
Saves an SKBitmap to a file in the specified format.
public static void SaveToFile(this SKBitmap bitmap, string filename, SKEncodedImageFormat format = SKEncodedImageFormat.Png, int quality = 85)Parameters
bitmapSKBitmap-
The SKBitmap to save.
filenamestring-
The full path and filename where the image will be saved.
formatSKEncodedImageFormat-
The image format to use for encoding, one of PNG, JPEG, WebP or BMP. Defaults to PNG.
qualityint-
The quality level for lossy formats (0-100). Defaults to 85. Only applicable for formats like JPEG.
Exceptions
- NotSupportedException
-
The format has no encoder in the Skia build the SDK ships with.
ToARGB(SKBitmap, out int)
Converts an SKBitmap to an ARGB byte array and returns a pointer to the allocated memory.
public static nint ToARGB(this SKBitmap bitmap, out int length)Parameters
bitmapSKBitmap-
The SKBitmap to convert.
lengthint-
When this method returns, contains the length of the allocated memory in bytes.
Returns
- nint
-
A pointer to unmanaged memory containing the ARGB data. The caller is responsible for freeing this memory using Marshal.FreeHGlobal.
ToColor(SKColor)
Extension method that converts a SkiaSharp SKColor to a System.Drawing.Color.
public static Color ToColor(this SKColor color)Parameters
colorSKColor-
The SKColor to convert.
Returns
Remarks
This method preserves all color components: Red, Green, Blue, and Alpha (transparency). The conversion is straightforward as both color types use the same RGBA color model with 8 bits per component.
ToSKColor(Color)
Extension method that converts a System.Drawing.Color to a SkiaSharp SKColor.
public static SKColor ToSKColor(this Color color)Parameters
colorColor-
The System.Drawing.Color to convert.
Returns
- SKColor
-
A SkiaSharp.SKColor with the same RGBA values as the input color.
Remarks
This method preserves all color components: Red, Green, Blue, and Alpha (transparency). The conversion is straightforward as both color types use the same RGBA color model with 8 bits per component.
VideoFrameBGRAToSKBitmap(VideoFrameX)
Converts a VideoFrameX containing BGRA data to an SKBitmap with the platform colour type. Padded rows are copied honouring VisioForge.Core.Types.X.VideoFrameInfoX.Stride.
public static SKBitmap VideoFrameBGRAToSKBitmap(this VideoFrameX frame)Parameters
frameVideoFrameX-
The VideoFrameX containing BGRA pixel data.
Returns
- SKBitmap
-
A new SKBitmap containing the image data.
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the frame stride is shorter than one row of pixels, or its first plane starts past the end of its data.
VideoFrameRGBAToSKBitmap(VideoFrameX)
Converts a VideoFrameX containing RGBA data to an SKBitmap with the platform colour type.
public static SKBitmap VideoFrameRGBAToSKBitmap(this VideoFrameX frame)Parameters
frameVideoFrameX-
The VideoFrameX containing RGBA pixel data.
Returns
- SKBitmap
-
A new SKBitmap containing the converted image data.
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the frame stride is shorter than one row of pixels, or its first plane starts past the end of its data.
VideoFrameRGBToSKBitmap(VideoFrameX)
Converts a VideoFrameX containing RGB data to an SKBitmap with the platform colour type.
public static SKBitmap VideoFrameRGBToSKBitmap(this VideoFrameX frame)Parameters
frameVideoFrameX-
The VideoFrameX containing RGB pixel data.
Returns
- SKBitmap
-
A new SKBitmap containing the converted image data.
Exceptions
- ArgumentOutOfRangeException
-
Thrown when the frame stride is shorter than one row of pixels, or its first plane starts past the end of its data.