SKShader Class

Definition

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:

Linear Gradient

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:

Radial Gradient

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:

Two-point Conical Gradient

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:

Sweep Gradient

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:

Fractal Perlin Noise

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:

Fractal Perlin Noise

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:

Compose Shader

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

CreateBitmap(SKBitmap)

Creates a new shader that will draw with the specified bitmap.

CreateBitmap(SKBitmap, SKShaderTileMode, SKShaderTileMode)

Creates a new shader that will draw with the specified bitmap.

CreateBitmap(SKBitmap, SKShaderTileMode, SKShaderTileMode, SKMatrix)

Creates a new shader that will draw with the specified bitmap.

CreateColor(SKColor)

Creates a new shader that just draws the specified color.

CreateColor(SKColorF, SKColorSpace)

Creates a new shader that just draws the specified color.

CreateColorFilter(SKShader, SKColorFilter)

Creates a new shader that produces the same colors as invoking this shader and then applying the color filter.

CreateCompose(SKShader, SKShader)

Create a new compose shader, which combines two shaders by the SrcOver blend mode.

CreateCompose(SKShader, SKShader, SKBlendMode)

Create a new compose shader, which combines two shaders by a blend mode.

CreateEmpty()

Creates a new "empty" shader that will not draw anything.

CreateImage(SKImage)
CreateImage(SKImage, SKShaderTileMode, SKShaderTileMode)
CreateImage(SKImage, SKShaderTileMode, SKShaderTileMode, SKMatrix)
CreateLerp(Single, SKShader, SKShader)
CreateLinearGradient(SKPoint, SKPoint, SKColor[], Single[], SKShaderTileMode)

Creates a shader that generates a linear gradient between the two specified points.

CreateLinearGradient(SKPoint, SKPoint, SKColor[], Single[], SKShaderTileMode, SKMatrix)

Creates a shader that generates a linear gradient between the two specified points.

CreateLinearGradient(SKPoint, SKPoint, SKColor[], SKShaderTileMode)

Creates a shader that generates a linear gradient between the two specified points.

CreateLinearGradient(SKPoint, SKPoint, SKColorF[], SKColorSpace, Single[], SKShaderTileMode)
CreateLinearGradient(SKPoint, SKPoint, SKColorF[], SKColorSpace, Single[], SKShaderTileMode, SKMatrix)
CreateLinearGradient(SKPoint, SKPoint, SKColorF[], SKColorSpace, SKShaderTileMode)
CreateLocalMatrix(SKShader, SKMatrix)

Creates a shader that first applies the specified matrix and then applies the shader.

CreatePerlinNoiseFractalNoise(Single, Single, Int32, Single)

Creates a new shader that draws Perlin fractal noise.

CreatePerlinNoiseFractalNoise(Single, Single, Int32, Single, SKPointI)

Creates a new shader that draws Perlin fractal noise.

CreatePerlinNoiseFractalNoise(Single, Single, Int32, Single, SKSizeI)
CreatePerlinNoiseImprovedNoise(Single, Single, Int32, Single)
CreatePerlinNoiseTurbulence(Single, Single, Int32, Single)

Creates a new shader that draws Perlin turbulence noise.

CreatePerlinNoiseTurbulence(Single, Single, Int32, Single, SKPointI)

Creates a new shader that draws Perlin turbulence noise.

CreatePerlinNoiseTurbulence(Single, Single, Int32, Single, SKSizeI)
CreatePicture(SKPicture)
CreatePicture(SKPicture, SKShaderTileMode, SKShaderTileMode)

Creates a new shader that will draw with the specified picture.

CreatePicture(SKPicture, SKShaderTileMode, SKShaderTileMode, SKMatrix, SKRect)

Creates a new shader that will draw with the specified picture.

CreatePicture(SKPicture, SKShaderTileMode, SKShaderTileMode, SKRect)

Creates a new shader that will draw with the specified picture.

CreateRadialGradient(SKPoint, Single, SKColor[], Single[], SKShaderTileMode)

Creates a shader that generates a radial gradient given the center and radius.

CreateRadialGradient(SKPoint, Single, SKColor[], Single[], SKShaderTileMode, SKMatrix)

Creates a shader that generates a radial gradient given the center and radius.

CreateRadialGradient(SKPoint, Single, SKColor[], SKShaderTileMode)

Creates a shader that generates a radial gradient given the center and radius.

CreateRadialGradient(SKPoint, Single, SKColorF[], SKColorSpace, Single[], SKShaderTileMode)
CreateRadialGradient(SKPoint, Single, SKColorF[], SKColorSpace, Single[], SKShaderTileMode, SKMatrix)
CreateRadialGradient(SKPoint, Single, SKColorF[], SKColorSpace, SKShaderTileMode)
CreateSweepGradient(SKPoint, SKColor[])

Creates a shader that generates a sweep gradient given a center.

CreateSweepGradient(SKPoint, SKColor[], Single[])

Creates a shader that generates a sweep gradient given a center.

CreateSweepGradient(SKPoint, SKColor[], Single[], SKMatrix)

Creates a shader that generates a sweep gradient given a center.

CreateSweepGradient(SKPoint, SKColor[], Single[], SKShaderTileMode, Single, Single)

Creates a shader that generates a sweep gradient given a center.

CreateSweepGradient(SKPoint, SKColor[], Single[], SKShaderTileMode, Single, Single, SKMatrix)

Creates a shader that generates a sweep gradient given a center.

CreateSweepGradient(SKPoint, SKColor[], SKShaderTileMode, Single, Single)

Creates a shader that generates a sweep gradient given a center.

CreateSweepGradient(SKPoint, SKColorF[], SKColorSpace)
CreateSweepGradient(SKPoint, SKColorF[], SKColorSpace, Single[])
CreateSweepGradient(SKPoint, SKColorF[], SKColorSpace, Single[], SKMatrix)
CreateSweepGradient(SKPoint, SKColorF[], SKColorSpace, Single[], SKShaderTileMode, Single, Single)
CreateSweepGradient(SKPoint, SKColorF[], SKColorSpace, Single[], SKShaderTileMode, Single, Single, SKMatrix)
CreateSweepGradient(SKPoint, SKColorF[], SKColorSpace, SKShaderTileMode, Single, Single)
CreateTwoPointConicalGradient(SKPoint, Single, SKPoint, Single, SKColor[], Single[], SKShaderTileMode)

Creates a shader that generates a conical gradient given two circles.

CreateTwoPointConicalGradient(SKPoint, Single, SKPoint, Single, SKColor[], Single[], SKShaderTileMode, SKMatrix)

Creates a shader that generates a conical gradient given two circles.

CreateTwoPointConicalGradient(SKPoint, Single, SKPoint, Single, SKColor[], SKShaderTileMode)

Creates a shader that generates a conical gradient given two circles.

CreateTwoPointConicalGradient(SKPoint, Single, SKPoint, Single, SKColorF[], SKColorSpace, Single[], SKShaderTileMode)
CreateTwoPointConicalGradient(SKPoint, Single, SKPoint, Single, SKColorF[], SKColorSpace, Single[], SKShaderTileMode, SKMatrix)
CreateTwoPointConicalGradient(SKPoint, Single, SKPoint, Single, SKColorF[], SKColorSpace, SKShaderTileMode)
Dispose()

Releases all resources used by this SKNativeObject.

(Inherited from SKNativeObject)
Dispose(Boolean)

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

DisposeInternal()

Triggers a dispose, ignoring the value of IgnorePublicDispose.

(Inherited from SKNativeObject)
DisposeManaged()

Implemented by derived SKObject types to destroy any managed objects.

(Inherited from SKObject)
DisposeNative()

Implemented by derived SKObject types to destroy any native objects.

(Inherited from SKObject)
DisposeUnownedManaged() (Inherited from SKObject)
WithColorFilter(SKColorFilter)
WithLocalMatrix(SKMatrix)

Applies to