MSDN Documentation

Microsoft Developer Network

SkiaSharp SKShader

The SKShader class in SkiaSharp represents a shader, which is a mechanism for generating colors or other shader outputs. Shaders are fundamental to Skia's advanced graphics capabilities, allowing for complex fills, gradients, and procedural textures.

Overview

Shaders in SkiaSharp are used to define how a shape or region is filled. Instead of a solid color, you can use a shader to create gradients, patterns, images, or even procedurally generated effects. This class serves as an abstract base class, and you will typically work with specific implementations like SKLinearGradientShader, SKRadialGradientShader, SKTwoPointConicalGradientShader, SKImageShader, and SKComposeShader.

Key Concepts

Common Shader Types

Linear Gradients (SKLinearGradientShader)

Creates a gradient that transitions colors along a straight line.


// Example: Creating a linear gradient from left to right
var colors = new SKColor[] { SKColors.Red, SKColors.Blue };
var points = new SKPoint[] { new SKPoint(0, 0), new SKPoint(100, 0) };
var shader = SKShader.CreateLinearGradient(
    points,
    colors,
    null, // SKShaderTileMode.Clamp is default
    SKShaderTileMode.Clamp
);
            

Radial Gradients (SKRadialGradientShader)

Creates a gradient that emanates from a central point outwards in a circular pattern.


// Example: Creating a radial gradient from the center
var colors = new SKColor[] { SKColors.Yellow, SKColors.Green };
var center = new SKPoint(50, 50);
var radius = 50f;
var shader = SKShader.CreateRadialGradient(
    center,
    radius,
    colors,
    null, // SKShaderTileMode.Clamp is default
    SKShaderTileMode.Clamp
);
            

Image Shaders (SKImageShader)

Uses an SKImage to fill a region, allowing for tiling or stretching.


// Assuming 'image' is an SKImage
var shader = SKShader.CreateImage(
    image,
    SKShaderTileMode.Repeat, // or SKShaderTileMode.Clamp, SKShaderTileMode.Mirror
    SKShaderTileMode.Repeat,
    SKPaintTileMode.Grid // or SKPaintTileMode.Linear
);
            

Working with Shaders

To use a shader, you typically assign it to the Shader property of an SKPaint object:


var paint = new SKPaint();
paint.Shader = /* your created SKShader */;
canvas.DrawRect(rect, paint);
            

Shader Tile Modes

The SKShaderTileMode enum controls how the shader's output is applied when the drawing region extends beyond the shader's defined bounds:

Methods

The SKShader class itself is abstract. You create specific shader instances using static factory methods:

static SKShader CreateLinearGradient(...)

SKPoint[] points: The start and end points of the gradient line. SKColor[] colors: An array of colors to use in the gradient. float[]? colorPos: Optional array of positions (0.0 to 1.0) for each color. SKShaderTileMode tileMode: How to tile the gradient. SKMatrix? matrix: Optional matrix to transform the shader.

static SKShader CreateRadialGradient(...)

SKPoint center: The center point of the radial gradient. float radius: The radius of the radial gradient. SKColor[] colors: An array of colors. float[]? colorPos: Optional array of positions (0.0 to 1.0). SKShaderTileMode tileMode: How to tile the gradient. SKMatrix? matrix: Optional matrix to transform the shader.

static SKShader CreateImage(...)

SKImage image: The image to use as a shader. SKShaderTileMode tx: Tile mode for the horizontal axis. SKShaderTileMode ty: Tile mode for the vertical axis. SKPaintTileMode filterQuality: The quality of filtering for the image. SKMatrix? matrix: Optional matrix to transform the shader.

Note: Always dispose of SKShader objects when you are finished with them to free up native resources, typically by using a using statement.

Warning: Complex shaders can have a performance impact. Profile your application to ensure optimal usage.