Table of Contents

Class SkiaHelper

Namespace
Alternet.Skia
Assembly
Alternet.UI.Common.dll

Provides utility methods and helpers for working with SkiaSharp, a 2D graphics library. This class contains methods that do not use Alternet.UI.

public static class SkiaHelper
Inheritance
SkiaHelper

Remarks

This class contains static methods designed to simplify common tasks when using SkiaSharp. It is intended to be a central place for reusable SkiaSharp-related functionality.

Fields

DefaultAntialiasing

Gets or sets default value for IsAntialias property when SKPaint is created by the conversion methods.

DegToRadF

Gets PI divided by 180f.

NativeLibraryName

Represents the name of the SkiaSharp native library.

RadToDegF

Gets 180f divided by PI.

SKCanvasDrawPointsName

Represents the name of the SkiaSharp canvas method used to draw points.

SampleCharsForIsMonospace

A sample string containing characters commonly used to test whether a font is monospaced.

SpanStackLimit

Represents the maximum number of spans that can be stored in a stack.

Properties

DefaultFont

Gets or sets default font for use with SkiaSharp.

DefaultFontName

Gets or sets default font name for use with SkiaSharp.

DefaultFontSize

Gets or sets default font size for use with SkiaSharp.

DefaultMonoFontName

Gets or sets default fixed-pitch font name.

DefaultTypeFace

Gets or sets default SKTypeface using to create default SkiaSharp font.

FocusRectPaint

Gets or sets the paint object used to render the focus rectangle.

FontFamilies

Gets all installed font families as array of strings.

GrayscaleColorFilter

Gets or sets the SKColorFilter used to convert images to grayscale.

IsDefaultMonoFontNameAssigned

Gets a value indicating whether the default monospaced font name has been assigned.

NullCanvas

Gets a null SKCanvas instance that performs no drawing operations.

Methods

ApplyPixelSnapping(SKCanvas)

Convenience: apply the pixel-snapping translate to the canvas. It saves the canvas state and returns an IDisposable that restores when disposed: using (ApplyPixelSnapping(canvas)) { ... } // paints inside will be offset

BitmapIsOk(SKBitmap?)

Gets whether or not specified bitmap is ok.

CharSize(SKCanvas, char, SKFont)

Measures the size of a single character when drawn using the specified font.

CharSize(SKCanvas, char, int, SKFont)

Returns the size of a sequence of identical characters when drawn with the specified font.

ConvertToGrayscale(SKBitmap)

Converts the specified SKBitmap to a grayscale version using a color filter.

CreateBitmapCanvas(SKBitmap, float)

Creates canvas on the specified bitmap.

CreateBitmapCanvas(SKSize, float, bool)

Creates canvas on the bitmap with the specified size and scaling factor.

CreateDefaultFont()

Creates default font.

CreateFillPaint(SKColor)

Creates SKPaint with Fill style for the specified SKColor value.

CreateFocusRectPaint(SKColor, float, float, float, float, bool, bool, SKStrokeCap)

Creates an SKPaint configured to draw a focus-like dashed rectangle (similar to WinForms DrawFocusRectangle). The returned SKPaint must be disposed by the caller.

CreateNullCanvas(int, int)

Creates dummy SKCanvas object which performs not painting.

CreateNullSurface(int, int)

Creates dummy SKSurface object ignores any painting.

CreateStrokeAndFillPaint(SKColor)

Creates SKPaint with StrokeAndFill style for the specified SKColor value.

CreateStrokePaint(SKColor)

Creates SKPaint with Stroke style for the specified SKColor value.

DebugBezierPointsAssert(ReadOnlySpan<SKPoint>)

Checks whether array of SKPoint parameter is ok.

DrawAnimatedFocusRect(SKCanvas, SKRect, SKColor, long, float, float, float, float, bool, bool)

Convenience helper that animates the dash phase to produce the "marching ants" effect. Call this from your paint loop and pass a time value (e.g. milliseconds). Uses CreateFocusRectPaint internally.

DrawBeziers(SKCanvas, SKPaint, ReadOnlySpan<SKPoint>)

Draws a series of Bezier splines from an array of SKPoint structures.

DrawBorderWithPaint(SKCanvas, SKPaint, SKRect, float)

Draws a border around the specified rectangle using the provided paint and border width.

DrawDashedLine(SKCanvas, SKPoint, SKPoint, SKColor, float, SkiaDashStyle, float[]?, float, bool)

Draws a line on the canvas using a dash style similar to System.Drawing.Pen.DashStyle. This helper creates and configures an SKPaint internally (caller does not own/dispose it).

DrawDotLine(SKCanvas, float, float, float, float, SKPaint, SKPaint?, int)

Draws a dotted line on the specified graphics context, alternating between two colors.

DrawFocusRect(SKCanvas, SKRect, SKColor)

Draws a focus rectangle on the specified canvas using the given color. FocusRectPaint is used to draw the rectangle.

DrawFocusRect(SKCanvas, SKRect, SKColor, float, float, float, float, bool, bool)

Draws a focus-like dashed rectangle (similar to WinForms DrawFocusRectangle). Uses CreateFocusRectPaint internally.

DrawHorzLine(SKCanvas, SKPaint, SKPoint, float, float)

Draws a horizontal line on the specified canvas.

DrawText(SKCanvas, ReadOnlySpan<char>, SKPoint, SKFont, SKPaint, SKPaint?)

Draws the specified text on the canvas at the given location using the specified font and paint settings.

DrawVertLine(SKCanvas, SKPaint, SKPoint, float, float)

Draws a vertical line on the specified canvas.

FastCeilToInt(float)

Rounds the specified floating-point value up to the nearest integer and returns the result as an int.

FillGradient(SKCanvas, SKRect, SKColor, SKColor, SKPoint, SKPoint)

Fills the specified rectangular area on the canvas with a linear gradient.

Fill<T>(T[], T)

Sets all elements in the specified array to the specified value.

GetAverageCanvasScale(SKCanvas)

Returns an approximated average scale factor of the canvas transform (works with rotation/skew). Use this to convert "device-pixel" lengths into canvas units: canvasUnits = devicePixels / scale.

GetBottomLineRect(SKRect, float)

Gets rectangle of the bottom border edge with the specified width.

GetFontFamiliesNames()

Gets all installed font families as enumerable.

GetLeftLineRect(SKRect, float)

Gets rectangle of the left border edge with the specified width.

GetPathFromSystemPoints(ReadOnlySpan<Point>, SkiaFillMode)

Creates a new SKPath from a collection of drawing points, optionally specifying the fill mode.

GetPixelSnappingOffset(SKCanvas)

Computes the pixel-snapping offset (in canvas/logical coordinates) that aligns 1px strokes to device pixel centers, taking the canvas TotalMatrix into account. Returns an SKPoint { X = offsetX, Y = offsetY } where: offsetX = 0.5f / effectiveScaleX offsetY = 0.5f / effectiveScaleY Use: canvas.Save(); canvas.Translate(offset.X, offset.Y); ... canvas.Restore();

GetRightLineRect(SKRect, float)

Gets rectangle of the right border edge with the specified width.

GetSKPointsForDrawWave(SKRectI)

Generates an array of points representing a wave pattern within the specified rectangular area.

GetTextExtent(SKCanvas, ReadOnlySpan<char>, SKFont)

Gets text size.

GetTopLineRect(SKRect, float)

Gets rectangle of the top border edge with the specified width.

ImageToBitmap(SKImage)

Converts an SKImage to an SKBitmap.

InvokeWithFilledSpan(int, char, ReadOnlySpanAction<char>, bool)

Allocates a Span<T> of the specified length, fills it with the given character, and invokes the provided action with the resulting ReadOnlySpan<T>. Uses stack allocation if the span size is below the threshold; otherwise falls back to heap.

InvokeWithUTF8Span(ReadOnlySpan<char>, Action<nint, int>)

Converts the specified UTF-16 character span to UTF-8 and invokes the provided action with a pointer to the UTF-8 data and its length.

IsFamilySkia(string)

Gets whether or not specified font is supported in SkiaSharp.

IsMonospace(SKFont, float)

Returns true if the SKFont appears to be monospaced. It first checks Typeface.IsFixedPitch if available, then measures glyph advance widths.

PixelFromDip(SKSize, float)

Converts device-independent units to pixels.

PixelFromDip(float, float)

Converts device-independent units to pixels.

RecreateBitmapCanvas(ref SKCanvas?, ref float?, float, SKSize, bool)

Recreates a bitmap-backed SKCanvas with the specified scale factor and size.

ResetFonts()

Resets loaded font families.

SetPaintDefaults(SKPaint)

This method is called by all SKPaint create methods. It raises PaintCreated event and initializes SKPaint instance properties with the default values.

ToDegrees(float)

Converts an angle from radians to degrees.

ToSkia(SkiaFillMode)

Converts SkiaFillMode to SKPathFillType.

ToSkiaPoints(Point[])

Converts an array of Point to SKPoint.

Events

PaintCreated

Occurs when SKPaint instance is created by one of the create methods.