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
- Gradients: Smooth transitions between two or more colors. SkiaSharp provides various gradient types to achieve different visual effects.
- Image Shaders: Repeating or scaling an image to fill a region.
- Procedural Shaders: Generating colors based on mathematical functions or noise, allowing for dynamic and complex textures.
- Composition: Combining multiple shaders using various blending modes.
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:
SKShaderTileMode.Clamp
: The edge color is repeated.SKShaderTileMode.Repeat
: The shader pattern is tiled.SKShaderTileMode.Mirror
: The shader pattern is mirrored and tiled.
Methods
The SKShader
class itself is abstract. You create specific shader instances using static factory methods:
static SKShader CreateLinearGradient(...)
static SKShader CreateRadialGradient(...)
static SKShader CreateImage(...)
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.