SKPaint Class

Definition

Holds the style and color information about how to draw geometries, text and bitmaps.

public class SKPaint : SkiaSharp.SKObject
Inheritance

Examples

Simple Example

The following example shows three different paints, each set up to draw in a different style. The caller can intermix these paints freely, either using them as is, or modifying them as the drawing proceeds.

var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
    SKCanvas canvas = surface.Canvas;

    canvas.Clear(SKColors.White);

    var paint1 = new SKPaint {
        TextSize = 64.0f,
        IsAntialias = true,
        Color = new SKColor(255, 0, 0),
        Style = SKPaintStyle.Fill
    };

    var paint2 = new SKPaint {
        TextSize = 64.0f,
        IsAntialias = true,
        Color = new SKColor(0, 136, 0),
        Style = SKPaintStyle.Stroke,
        StrokeWidth = 3
    };

    var paint3 = new SKPaint {
        TextSize = 64.0f,
        IsAntialias = true,
        Color = new SKColor(136, 136, 136),
        TextScaleX = 1.5f
    };

    var text = "Skia!";
    canvas.DrawText(text, 20.0f, 64.0f, paint1);
    canvas.DrawText(text, 20.0f, 144.0f, paint2);
    canvas.DrawText(text, 20.0f, 224.0f, paint3);
}

The example above produces the following:

SKPaint and Text

Effects Example

The following example draws using a gradient instead of a single color. To do, this a SKShader is assigned to the paint. Anything drawn with that paint will be drawn with the gradient specified in the call to SKShader.CreateLinearGradient.

var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
    SKCanvas canvas = surface.Canvas;

    canvas.Clear(SKColors.White);

    // create a gradient
    var colors = new[] {
        SKColors.Blue,
        SKColors.Yellow
    };
    var shader = SKShader.CreateLinearGradient(
        new SKPoint(0.0f, 0.0f),
        new SKPoint(256.0f, 256.0f),
        colors,
        null,
        SKShaderTileMode.Clamp);

    // assign the gradient to the paint
    var paint = new SKPaint {
        Shader = shader
    };

    canvas.DrawPaint(paint);
}

The example above produces the following:

SKPaint and SKShader

Remarks

Anytime you draw something in SkiaSharp, and want to specify what color it is, or how it blends with the background, or what style or font to draw it in, you specify those attributes in a paint.

Unlike SKCanvas, an paint object does not maintain an internal stack of state. That is, there is no save/restore on a paint. However, paint objects are relatively light-weight, so the client may create and maintain any number of paint objects, each set up for a particular use.

Factoring all of these color and stylistic attributes out of the canvas state, and into (multiple) paint objects, allows the save and restore operations on the SKCanvas to be that much more efficient, as all they have to do is maintain the stack of matrix and clip settings.

Effects

Beyond simple attributes such as color, strokes, and text values, paints support effects. These are subclasses of different aspects of the drawing pipeline, that when referenced by a paint, are called to override some part of the drawing pipeline.

There are five types of effects that can be assigned to an paint object:

Effect Details
Blend Mode Blend modes and Duff-Porter transfer modes.
Color Filter Modification of the source colors before applying the blend mode.
Mask Filter Modification of the alpha mask before it is colorized and drawn (for example, blur).
Path Effect Modification of the geometry (path) before the alpha mask is generated (for example, dashing).
Shader Gradients and bitmap patterns.

Constructors

SKPaint()

Creates a new paint with the default settings.

Properties

BlendMode

Gets or sets the blend mode.

Color

Gets or sets the paint's foreground color.

ColorFilter

Gets or sets the paint's color filter.

DeviceKerningEnabled

Gets or sets a value indicating whether device kerning is enabled.

FakeBoldText

Gets or sets a value indicating whether fake bold text is enabled.

FilterQuality

Gets or sets the filter quality of the current paint.

FontMetrics

Gets the font metrics for the current typeface.

FontSpacing

Gets the recommend line spacing.

Handle

Gets or sets the handle to the underlying native object.

(Inherited from SKObject)
HintingLevel

Gets or sets the level of hinting to be performed.

ImageFilter

Gets or sets the image filter.

IsAntialias

Gets or sets a value indicating whether anti-aliasing is enabled.

IsAutohinted

Gets or sets a value indicating whether auto-hinting is enabled.

IsDither

Gets or sets a value indicating whether dithering is enabled.

IsEmbeddedBitmapText

Gets or sets a value indicating whether text is an embedded bitmap.

IsLinearText

Gets or sets a value indicating whether text is linear.

IsStroke

Gets or sets a value indicating whether to paint a stroke or the fill.

IsVerticalText

Gets or sets a value indicating whether the text is vertical or horizontal is enabled.

LcdRenderText

Gets or sets a value indicating whether LCD text rendering is enabled.

MaskFilter

Gets or sets the mask filter to use when painting.

OwnsHandle

Gets a value indicating whether this object should destroy the underlying native object.

(Inherited from SKObject)
PathEffect

Gets or sets the path effect to use when painting.

Shader

Gets or sets the shader to use when painting.

StrikeThruText

Gets or sets a value indicating whether to strike through text.

StrokeCap

Gets or sets a value indicating how the start and end of stroked lines and paths are treated.

StrokeJoin

Gets or sets the path's join type.

StrokeMiter

Gets or sets the paint's miter limit.

StrokeWidth

Gets or sets the paint's stroke width.

Style

Gets or sets the painting style.

SubpixelText

Gets or sets a value indicating whether to use subpixel text positioning.

TextAlign

Gets or sets the path's align value.

TextEncoding

Gets or sets the encoding used when drawing or measuring text.

TextScaleX

Gets or sets paint's horizontal scale factor for text.

TextSize

Gets or sets the text size.

TextSkewX

Gets or sets paint's horizontal skew factor for text.

Typeface

Gets or sets the typeface used when painting text. May be null.

UnderlineText

Gets or sets a value indicating whether to underline text.

XferMode

Gets or sets the transfer mode.

Methods

BreakText(Byte[], Single)

Measure the text, stopping early if the measured width exceeds maxWidth.

BreakText(Byte[], Single, Single)

Measure the text, stopping early if the measured width exceeds maxWidth.

BreakText(IntPtr, IntPtr, Single)

Measure the text buffer, stopping early if the measured width exceeds maxWidth.

BreakText(IntPtr, IntPtr, Single, Single)

Measure the text buffer, stopping early if the measured width exceeds maxWidth.

BreakText(String, Single)

Measure the text, stopping early if the measured width exceeds maxWidth.

BreakText(String, Single, Single)

Measure the text, stopping early if the measured width exceeds maxWidth.

BreakText(String, Single, Single, String)

Measure the text, stopping early if the measured width exceeds maxWidth.

Clone()

Creates a copy of the current paint.

ContainsGlyphs(Byte[])

Returns a value indicating whether or not all the characters corresponds to a non-zero glyph index.

ContainsGlyphs(IntPtr, Int32)

Returns a value indicating whether or not all the characters corresponds to a non-zero glyph index.

ContainsGlyphs(String)

Returns a value indicating whether or not all the characters corresponds to a non-zero glyph index.

CountGlyphs(Byte[])

Returns the number of glyphs in text.

CountGlyphs(IntPtr, Int32)

Returns the number of glyphs in text.

CountGlyphs(String)

Returns the number of glyphs in text.

Dispose()

Releases all resources used by this SKNativeObject.

(Inherited from SKNativeObject)
Dispose(Boolean)

Releases the unmanaged resources used by the SKPaint and optionally releases the managed resources.

GetFillPath(SKPath)

Creates a new path from the result of applying any and all effects to a source path.

GetFillPath(SKPath, Single)

Creates a new path from the result of applying any and all effects to a source path.

GetFillPath(SKPath, SKPath)

Applies any and all effects to a source path, returning the result in the destination.

GetFillPath(SKPath, SKPath, Single)

Applies any and all effects to a source path, returning the result in the destination.

GetFillPath(SKPath, SKPath, SKRect)

Applies any and all effects to a source path, returning the result in the destination.

GetFillPath(SKPath, SKPath, SKRect, Single)

Applies any and all effects to a source path, returning the result in the destination.

GetFillPath(SKPath, SKRect)

Creates a new path from the result of applying any and all effects to a source path.

GetFillPath(SKPath, SKRect, Single)

Creates a new path from the result of applying any and all effects to a source path.

GetFontMetrics(SKFontMetrics, Single)

Returns the font metrics and line spacing for the current typeface.

GetGlyphs(Byte[])

Converts text into glyph indices.

GetGlyphs(IntPtr, Int32)

Converts text into glyph indices.

GetGlyphs(String)

Converts text into glyph indices.

GetGlyphWidths(Byte[])

Retrieves the advance for each glyph in the text.

GetGlyphWidths(Byte[], SKRect[])

Retrieves the advance and bounds for each glyph in the text.

GetGlyphWidths(IntPtr, Int32)

Retrieves the advance for each glyph in the text.

GetGlyphWidths(IntPtr, Int32, SKRect[])

Retrieves the advance and bounds for each glyph in the text.

GetGlyphWidths(String)

Retrieves the advance for each glyph in the text.

GetGlyphWidths(String, SKRect[])

Retrieves the advance and bounds for each glyph in the text.

GetHorizontalTextIntercepts(Byte[], Single[], Single, Single, Single)

Calculate the intersections of two parallel lines and the glyphs.

GetHorizontalTextIntercepts(IntPtr, Int32, Single[], Single, Single, Single)

Calculate the intersections of two parallel lines and the glyphs.

GetHorizontalTextIntercepts(String, Single[], Single, Single, Single)

Calculate the intersections of two parallel lines and the glyphs.

GetPositionedTextIntercepts(Byte[], SKPoint[], Single, Single)

Calculate the intersections of two parallel lines and the glyphs.

GetPositionedTextIntercepts(IntPtr, Int32, SKPoint[], Single, Single)

Calculate the intersections of two parallel lines and the glyphs.

GetPositionedTextIntercepts(String, SKPoint[], Single, Single)

Calculate the intersections of two parallel lines and the glyphs.

GetTextIntercepts(Byte[], Single, Single, Single, Single)

Calculate the intersections of two parallel lines and the glyphs.

GetTextIntercepts(IntPtr, Int32, Single, Single, Single, Single)

Calculate the intersections of two parallel lines and the glyphs.

GetTextIntercepts(SKTextBlob, Single, Single)

Calculate the intersections of two parallel lines and the glyphs.

GetTextIntercepts(String, Single, Single, Single, Single)

Calculate the intersections of two parallel lines and the glyphs.

GetTextPath(Byte[], Single, Single)

Returns the path (outline) for the specified text.

GetTextPath(Byte[], SKPoint[])

Returns the path (outline) for the specified text.

GetTextPath(IntPtr, IntPtr, Single, Single)

Returns the path (outline) for the specified text.

GetTextPath(IntPtr, IntPtr, SKPoint[])

Returns the path (outline) for the specified text.

GetTextPath(String, Single, Single)

Returns the path (outline) for the specified text.

GetTextPath(String, SKPoint[])

Returns the path (outline) for the specified text.

MeasureText(Byte[])

Measures the specified text.

MeasureText(Byte[], SKRect)

Measures the specified text.

MeasureText(IntPtr, IntPtr)

Measures the specified UTF-8 encoded text.

MeasureText(IntPtr, IntPtr, SKRect)

Measures the specified UTF-8 encoded text.

MeasureText(String)

Measures the specified text.

MeasureText(String, SKRect)

Measures the specified text.

Applies to