SkiaSharp.SKCanvas Class

Encapsulates all of the state about drawing into a device (bitmap).

See Also: SKCanvas

Syntax

public class SKCanvas : SKObject

Remarks

Encapsulates all of the state about drawing into a device (bitmap).

This includes a reference to the device itself, and a stack of matrix/clip values. For any given draw call (e.g. drawRect), the geometry of the object being drawn is transformed by the concatenation of all the matrices in the stack. The transformed geometry is clipped by the intersection of all of the clips in the stack.

While the canvas holds the state of the drawing device, the state (style) of the object being drawn is held by the Paint, which is provided as a parameter to each of the draw() methods. The Paint holds attributes such as color, typeface, the text size, the stroke width, the shader (for example, gradients, patterns), etc.

The SKCanvas is returned when accessing the SKSurface.Canvas property of a surface.

Transformations

The SKCanvas supports a number of 2D transformations.   Unlike other 2D graphic systems like CoreGraphics or Cairo, SKCanvas extends the transformations to include perspectives.   

You can use the SKCanvas.ScaleSKCanvas.SkewSKCanvas.TranslateSKCanvas.RotateDegreesSKCanvas.RotateRadians to perform some of the most common 2D transformations.   

For more control you can use the SKCanvas.SetMatrix to set an arbitrary transformation using the SKMatrix and the SKCanvas.Concat to concatenate an SKMatrix transformation to the current matrix in use.

The SKCanvas.ResetMatrix can be used to reset the state of the matrix

Drawing

The drawing operations can take a SKPaint parameter to affect their drawing.  You use SKPaint objects to cache the style and color information to draw geometries, texts and bitmaps.

Clipping Path

State

It is possible to save the current transformations by calling the SKCanvas.Save method which preserves the current transformation matrix, you can then alter the matrix and restore the previous state by using the SKCanvas.Restore or SKCanvas.RestoreToCount methods.   

Additionally, it is possible to push a new state with SKCanvas.SaveLayer which will make an offscreen copy of a region, and once the drawing is completed, calling the SKCanvas.Restore method which copies the offscreen bitmap into this canvas.

The example below shows a typical use:

C# Example

using (var surface = SKSurface.Create (width: 640, height: 480, SKImageInfo.PlatformColorType, SKAlphaType.Premul)) {
    SKCanvas canvas = surface.Canvas;

    canvas.Clear (SKColors.White);
    
    // set up drawing tools
    using (var paint = new SKPaint ()) {
        paint.IsAntialias = true;
        paint.Color = new SKColor (0x2c, 0x3e, 0x50);
        paint.StrokeCap = SKStrokeCap.Round;
        
        // create the Xamagon path
        using (var path = new SKPath ()) {
            path.MoveTo (71.4311121f, 56f);
            path.CubicTo (68.6763107f, 56.0058575f, 65.9796704f, 57.5737917f, 64.5928855f, 59.965729f);
            path.LineTo (43.0238921f, 97.5342563f);
            path.CubicTo (41.6587026f, 99.9325978f, 41.6587026f, 103.067402f, 43.0238921f, 105.465744f);
            path.LineTo (64.5928855f, 143.034271f);
            path.CubicTo (65.9798162f, 145.426228f, 68.6763107f, 146.994582f, 71.4311121f, 147f);
            path.LineTo (114.568946f, 147f);
            path.CubicTo (117.323748f, 146.994143f, 120.020241f, 145.426228f, 121.407172f, 143.034271f);
            path.LineTo (142.976161f, 105.465744f);
            path.CubicTo (144.34135f, 103.067402f, 144.341209f, 99.9325978f, 142.976161f, 97.5342563f);
            path.LineTo (121.407172f, 59.965729f);
            path.CubicTo (120.020241f, 57.5737917f, 117.323748f, 56.0054182f, 114.568946f, 56f);
            path.LineTo (71.4311121f, 56f);
            path.Close ();
          
            // draw the Xamagon path
            canvas.DrawPath (path, paint);
        }
    }
}

Requirements

Namespace: SkiaSharp
Assembly: SkiaSharp (in SkiaSharp.dll)
Assembly Versions: 1.0.0.0, 1.49.0.0, 1.52.0.0, 1.53.0.0, 1.54.0.0, 1.54.0.0, 1.55.0.0, 1.56.0.0, 1.57.0.0, 1.58.0.0, 1.59.0.0

The members of SkiaSharp.SKCanvas are listed below.

See Also: SKObject

Public Constructors

Creates a canvas with the specified bitmap to draw into.

Public Properties

[read-only]
ClipBoundsSKRect. Gets the bounds of the current clip (in local coordinates).
[read-only]
ClipDeviceBoundsSKRectI. Gets the bounds of the current clip (in device coordinates).
[read-only]
DeviceClipBoundsSKRectI. Gets the bounds of the current clip (in device coordinates).
[read-only]
LocalClipBoundsSKRect. Gets the bounds of the current clip (in local coordinates).
[read-only]
SaveCountInt32. Gets the number of matrix/clip states on the canvas' private stack.
[read-only]
TotalMatrixSKMatrix. Gets the current matrix on the canvas.

Public Methods

Clear()
Replaces all the pixels in the canvas’ current clip with the SKColors.Empty color.
Clear(SKColor)
Replaces all the pixels in the canvas’ current clip with the specified color.
ClipPath(SKPath)
Modify the current clip with the specified path.
ClipPath(SKPath, SKClipOperation, Boolean)
Modify the current clip with the specified path.
ClipPath(SKPath, SKRegionOperation, Boolean)
Modify the current clip with the specified path.
ClipRect(SKRect)
Modify the current clip with the specified rectangle.
ClipRect(SKRect, SKClipOperation, Boolean)
Modify the current clip with the specified rectangle.
ClipRect(SKRect, SKRegionOperation, Boolean)
Modify the current clip with the specified rectangle.
ClipRegion(SKRegion, SKClipOperation)
Modify the current clip with the specified region.
Concat(ref SKMatrix)
Pre-concatenates the provided transformation matrix with the current transformation matrix.
DrawAnnotation(SKRect, String, SKData)
Send an key/value pair "annotation" to the canvas.
DrawBitmap(SKBitmap, SKRect, SKPaint)
Draws a bitmap on the canvas.
DrawBitmap(SKBitmap, SKRect, SKRect, SKPaint)
Draws a bitmap on the canvas.
DrawBitmap(SKBitmap, Single, Single, SKPaint)
Draws a bitmap on the canvas.
DrawBitmapLattice(SKBitmap, SKLattice, SKRect, SKPaint)
Draws the bitmap, stretched or shrunk differentially to fit into the destination rectangle.
DrawBitmapLattice(SKBitmap, Int32[], Int32[], SKRect, SKPaint)
Draws the bitmap, stretched or shrunk differentially to fit into the destination rectangle.
DrawBitmapNinePatch(SKBitmap, SKRectI, SKRect, SKPaint)
Draws the bitmap, stretched or shrunk differentially to fit into the destination rectangle.
DrawCircle(Single, Single, Single, SKPaint)
Draws a circle on the canvas.
DrawColor(SKColor, SKBlendMode)
Fills the current clipping area with the specified color using the specified color and blend mode.
DrawColor(SKColor, SKXferMode)
Fills the current clipping area with the specified color using the specified color and transfer mode.
DrawImage(SKImage, SKRect, SKPaint)
Draws an image on the canvas.
DrawImage(SKImage, SKRect, SKRect, SKPaint)
Draws an image on the canvas.
DrawImage(SKImage, Single, Single, SKPaint)
Draws an image on the canvas.
DrawImageLattice(SKImage, SKLattice, SKRect, SKPaint)
Draws the image, stretched or shrunk differentially to fit into the destination rectangle.
DrawImageLattice(SKImage, Int32[], Int32[], SKRect, SKPaint)
Draws the image, stretched or shrunk differentially to fit into the destination rectangle.
DrawImageNinePatch(SKImage, SKRectI, SKRect, SKPaint)
Draws the image, stretched or shrunk differentially to fit into the destination rectangle.
DrawLine(Single, Single, Single, Single, SKPaint)
Draws a line on the canvas.
DrawLinkDestinationAnnotation(SKRect, SKData)
Annotates the canvas by making the specified rectangle link to a named destination (see SKCanvas.DrawNamedDestinationAnnotation).
DrawLinkDestinationAnnotation(SKRect, String) : SKData
Annotates the canvas by making the specified rectangle link to a named destination (see SKCanvas.DrawNamedDestinationAnnotation).
DrawNamedDestinationAnnotation(SKPoint, SKData)
Annotates the canvas by associating a name with the specified point (see SKCanvas.DrawLinkDestinationAnnotation).
DrawNamedDestinationAnnotation(SKPoint, String) : SKData
Annotates the canvas by associating a name with the specified point (see SKCanvas.DrawLinkDestinationAnnotation).
DrawOval(SKRect, SKPaint)
Draws an oval on the canvas.
DrawOval(Single, Single, Single, Single, SKPaint)
Draws an oval on the canvas.
DrawPaint(SKPaint)
Fills the current clipping path with the specified paint.
DrawPath(SKPath, SKPaint)
Draws a path in the canvas.
DrawPicture(SKPicture, SKPaint)
Draws a picture on the canvas.
DrawPicture(SKPicture, ref SKMatrix, SKPaint)
Draws a picture on the canvas.
DrawPoint(Single, Single, SKColor)
Draws a point in the canvas with the specified color.
DrawPoint(Single, Single, SKPaint)
Draws a point in the canvas with the specified color.
DrawPoints(SKPointMode, SKPoint[], SKPaint)
Draws an array of points, lines or a polygon in the canvas, one at a time.
DrawPositionedText(Byte[], SKPoint[], SKPaint)
Draws glyphs of the text at specified locations on the canvas.
DrawPositionedText(String, SKPoint[], SKPaint)
Draws glyphs of the text at specified locations on the canvas.
DrawPositionedText(IntPtr, Int32, SKPoint[], SKPaint)
Draws text from a UTF-8 buffer at specified locations on the canvas.
DrawRect(SKRect, SKPaint)
Draws a rectangle in the canvas.
DrawRegion(SKRegion, SKPaint)
Draws the outline of the specified region using the specified paint.
DrawRoundRect(SKRect, Single, Single, SKPaint)
Draws a rounded rectangle in the canvas.
DrawSurface(SKSurface, Single, Single, SKPaint)
Draws a surface on the canvas.
DrawText(String, SKPoint[], SKPaint)
Draws glyphs of the text at specified locations on the canvas.
DrawText(Byte[], Single, Single, SKPaint)
Draws text on the canvas at the specified coordinates.
DrawText(IntPtr, Int32, SKPoint[], SKPaint)
Draws text from a UTF-8 buffer at specified locations on the canvas.
DrawText(String, Single, Single, SKPaint)
Draws text on the canvas at the specified coordinates.
DrawText(Byte[], SKPath, Single, Single, SKPaint)
Draws text on the canvas following a path.
DrawText(IntPtr, Int32, Single, Single, SKPaint)
Draws text encoded in a UTF-8 buffer on the canvas at the specified coordinates.
DrawText(String, SKPath, Single, Single, SKPaint)
Draws text on the canvas following a path.
DrawText(IntPtr, Int32, SKPath, Single, Single, SKPaint)
Draws text encoded in a UTF-8 buffer on the canvas following a path.
DrawTextOnPath(Byte[], SKPath, Single, Single, SKPaint)
Draws text on the canvas following a path.
DrawTextOnPath(String, SKPath, Single, Single, SKPaint)
Draws text on the canvas following a path.
DrawTextOnPath(IntPtr, Int32, SKPath, Single, Single, SKPaint)
Draws text encoded in a UTF-8 buffer on the canvas following a path.
DrawUrlAnnotation(SKRect, SKData)
Annotates the canvas by associating the specified URL with the specified rectangle (in local coordinates, just like SKCanvas.DrawRect).
DrawUrlAnnotation(SKRect, String) : SKData
Annotates the canvas by associating the specified URL with the specified rectangle (in local coordinates, just like SKCanvas.DrawRect).
DrawVertices(SKVertices, SKBlendMode, SKPaint)
Draws a set of vertices.
DrawVertices(SKVertexMode, SKPoint[], SKColor[], SKPaint)
Draws an array of vertices, interpreted as triangles (based on mode).
DrawVertices(SKVertexMode, SKPoint[], SKPoint[], SKColor[], SKPaint)
Draws an array of vertices, interpreted as triangles (based on mode).
DrawVertices(SKVertexMode, SKPoint[], SKPoint[], SKColor[], UInt16[], SKPaint)
Draws an array of vertices, interpreted as triangles (based on mode).
DrawVertices(SKVertexMode, SKPoint[], SKPoint[], SKColor[], SKBlendMode, UInt16[], SKPaint)
Draws an array of vertices, interpreted as triangles (based on mode).
Flush()
Triggers the immediate execution of all pending draw operations.
GetClipBounds(ref SKRect) : Boolean
Returns the bounds of the current clip (in local coordinates).
GetClipDeviceBounds(ref SKRectI) : Boolean
Returns the bounds of the current clip (in device coordinates).
GetDeviceClipBounds(out SKRectI) : Boolean
Returns the bounds of the current clip (in device coordinates).
GetLocalClipBounds(out SKRect) : Boolean
Returns the bounds of the current clip (in local coordinates).
QuickReject(SKPath) : Boolean
Checks to see if the specified path, after being transformed by the current matrix, would lie completely outside of the current clip.
QuickReject(SKRect) : Boolean
Checks to see if the specified rectangle, after being transformed by the current matrix, would lie completely outside of the current clip.
ResetMatrix()
Sets the current matrix to identity.
Restore()
Restore the canvas state.
RestoreToCount(Int32)
Efficiently restores the state to a specific level.
RotateDegrees(Single)
Pre-concats the current matrix with the specified rotation.
RotateDegrees(Single, Single, Single)
Pre-concats the current matrix with the specified rotation, around the specified point.
RotateRadians(Single)
Pre-concats the current matrix with the specified rotation.
RotateRadians(Single, Single, Single)
Pre-concats the current matrix with the specified rotation, around the specified point.
Save() : Int32
Saves the canvas state.
SaveLayer(SKPaint) : Int32
Saves the canvas state and allocates an offscreen bitmap.
SaveLayer(SKRect, SKPaint) : Int32
Saves the canvas state and allocates an offscreen bitmap.
Scale(SKPoint)
Pre-concats the current matrix with the specified scale.
Scale(Single)
Pre-concats the current matrix with the specified scale.
Scale(Single, Single)
Pre-concats the current matrix with the specified scale.
Scale(Single, Single, Single, Single)
Pre-concats the current matrix with the specified scale, at the specific offset.
SetMatrix(SKMatrix)
Replaces the current matrix with a copy of the specified matrix.
Skew(SKPoint)
Pre-concats the current matrix with the specified skew.
Skew(Single, Single)
Pre-concats the current matrix with the specified skew.
Translate(SKPoint)
Pre-concats the current matrix with the specified translation.
Translate(Single, Single)
Pre-concats the current matrix with the specified translation.

Protected Methods

override
Dispose(Boolean)