SKShader Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Shaders specify the source color(s) for what is being drawn in the SKPaint.
public class SKShader : SkiaSharp.SKObject
- Inheritance
Examples
Linear Gradient Shader Example
var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.White);
// create the shader
var colors = new SKColor[] {
new SKColor(0, 0, 255),
new SKColor(0, 255, 0)
};
var shader = SKShader.CreateLinearGradient(
new SKPoint(0, 0),
new SKPoint(255, 255),
colors,
null,
SKShaderTileMode.Clamp);
// use the shader
var paint = new SKPaint {
Shader = shader
};
canvas.DrawPaint(paint);
}
The example above produces the following:
Radial Gradient Shader Example
var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.White);
// create the shader
var colors = new SKColor[] {
new SKColor(0, 0, 255),
new SKColor(0, 255, 0)
};
var shader = SKShader.CreateRadialGradient(
new SKPoint(128, 128),
180,
colors,
null,
SKShaderTileMode.Clamp);
// use the shader
var paint = new SKPaint {
Shader = shader
};
canvas.DrawPaint(paint);
}
The example above produces the following:
Two-point Conical Gradient Shader Example
var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.White);
// create the shader
var colors = new SKColor[] {
new SKColor(0, 0, 255),
new SKColor(0, 255, 0)
};
var shader = SKShader.CreateTwoPointConicalGradient(
new SKPoint(128, 128),
128,
new SKPoint(128, 16),
16,
colors,
null,
SKShaderTileMode.Clamp);
// use the shader
var paint = new SKPaint {
Shader = shader
};
canvas.DrawPaint(paint);
}
The example above produces the following:
Sweep Gradient Shader Example
var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.White);
// create the shader
var colors = new SKColor[] {
new SKColor(0, 255, 255),
new SKColor(255, 0, 255),
new SKColor(255, 255, 0),
new SKColor(0, 255, 255)
};
var shader = SKShader.CreateSweepGradient(
new SKPoint(128, 128),
colors,
null);
// use the shader
var paint = new SKPaint {
Shader = shader
};
canvas.DrawPaint(paint);
}
The example above produces the following:
Fractal Perlin Noise Shader Example
var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.White);
// create the shader
var shader = SKShader.CreatePerlinNoiseFractalNoise(0.5f, 0.5f, 4, 0);
// use the shader
var paint = new SKPaint {
Shader = shader
};
canvas.DrawPaint(paint);
}
The example above produces the following:
Perlin Noise Turbulence Shader Example
var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.White);
// create the shader
var shader = SKShader.CreatePerlinNoiseTurbulence(0.05f, 0.05f, 4, 0);
// use the shader
var paint = new SKPaint {
Shader = shader
};
canvas.DrawPaint(paint);
}
The example above produces the following:
Compose Shader Example
var info = new SKImageInfo(256, 256);
using (var surface = SKSurface.Create(info)) {
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.White);
// create the first shader
var colors = new SKColor[] {
new SKColor(0, 255, 255),
new SKColor(255, 0, 255),
new SKColor(255, 255, 0),
new SKColor(0, 255, 255)
};
var sweep = SKShader.CreateSweepGradient(new SKPoint(128, 128), colors, null);
// create the second shader
var turbulence = SKShader.CreatePerlinNoiseTurbulence(0.05f, 0.05f, 4, 0);
// create the compose shader
var shader = SKShader.CreateCompose(sweep, turbulence, SKBlendMode.SrcOver);
// use the compose shader
var paint = new SKPaint {
Shader = shader
};
canvas.DrawPaint(paint);
}
The example above produces the following:
Remarks
Shaders specify the source colors for what is being drawn. If a paint has no shader, then the paint's color is used. If the paint has a shader, then the shader's colors are used instead, but they are modulated by the paint's alpha.
This makes it easy to create a shader once (for example, bitmap tiling or gradient) and then change its transparency without having to modify the original shader, only the paint's alpha needs to be modified.
Shaders are created by calling one of the static "Create" methods.
Properties
Handle |
Gets or sets the handle to the underlying native object. (Inherited from SKObject) |
IgnorePublicDispose |
Gets or sets a value indicating whether the call the public Dispose() should be no-op. (Inherited from SKNativeObject) |
IsDisposed |
Gets or sets a value indicating whether the object has already been disposed. (Inherited from SKNativeObject) |
OwnsHandle |
Gets a value indicating whether this object should destroy the underlying native object. (Inherited from SKObject) |
Methods
Applies to
SkiaSharp