Table of Contents

Class TextureCanvas

Namespace
Stride.CommunityToolkit.Rendering.Utilities
Assembly
Stride.CommunityToolkit.dll

Provides functionality for drawing and manipulating textures on a canvas. This class supports various operations such as drawing rectangles, drawing textures, applying filters, transforming colors, and resampling textures. It is designed to work with the Stride graphics framework and provides methods for loading and storing texture data.

public class TextureCanvas : IDisposable
Inheritance
TextureCanvas
Implements

Constructors

TextureCanvas(RenderContext, Size2?, PixelFormat)

Initializes a new instance of the TextureCanvas class.

public TextureCanvas(RenderContext renderContext, Size2? size, PixelFormat pixelFormat = PixelFormat.R8G8B8A8_UNorm)

Parameters

renderContext RenderContext
size Size2?
pixelFormat PixelFormat

Properties

ByteSize

Calculates the texture size in bytes

public int ByteSize { get; }

Property Value

int

PixelFormat

Gets or sets the current pixel format

public PixelFormat PixelFormat { get; set; }

Property Value

PixelFormat

Remarks

Setting a different pixel format may cause a resample of the already drawn back buffer or call the setter before you start drawing.
You may consider using Resample(Size2, PixelFormat, SamplingPattern) if you also want to change the size in a single operation.

Size

Gets or sets the current texture size

public Size2 Size { get; set; }

Property Value

Size2

Remarks

Setting a different size may cause a resample of the already drawn back buffer or call the setter before you start drawing.
You may consider using Resample(Size2, PixelFormat, SamplingPattern) if you also want to change the pixel format in a single operation.

Methods

Apply(ImageEffect, params Texture?[]?)

Applies an image effect to the buffer texture

public void Apply(ImageEffect effect, params Texture?[]? inputs)

Parameters

effect ImageEffect

The image effect to apply

inputs Texture[]

The input textures

Remarks

Null, Empty arrays or array entries with null as a value will be replaced by the current drawing buffer
The output will always be replaced by the TextureCanvas as a single output.

BrightFilter(float, float, Color3?)

Applies a BrightFilter effect.

public void BrightFilter(float threshold = 0.2, float steepness = 1, Color3? colorMultiplier = null)

Parameters

threshold float

The threshold relative to the white point

steepness float

the smooth-step steepness for bright pass filtering.

colorMultiplier Color3?

Modulates bright areas with the provided color. It affects the color of sub-sequent bloom, light-streak effects

Clear()

Clears the current drawing context and releases internal texture buffers

public void Clear()

CoCMapBlur(int)

Applies a CoCMapBlur effect.

public void CoCMapBlur(int radius = 4)

Parameters

radius int

The radius.

Colorize(Color4)

Applies a color multiply effect.

public void Colorize(Color4 colorMultiplier)

Parameters

colorMultiplier Color4

The color multiplier

Combine(Texture?[]?, float[]?, Color3[]?)

Applies a ColorCombiner effect.

public void Combine(Texture?[]? textures = null, float[]? factors = null, Color3[]? colorMultipliers = null)

Parameters

textures Texture[]

The textures combine (use null to use the current drawing buffer as a texture input)

factors float[]

The factors used to multiply the colors.

colorMultipliers Color3[]

The color multiplier of each texture. Default is White

Remarks

Null, Empty arrays or array entries with null as a value will be replaced by the current drawing buffer

Dispose()

Releases all resources used by the TextureCanvas object.

public void Dispose()

DrawRect(Rectangle, Stretch, Anchor, SamplingPattern)

Draws a rectangle on the canvas.

public void DrawRect(Rectangle destinationRect, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)

Parameters

destinationRect Rectangle

The destination rectangle where the rectangle will be drawn.

stretch TextureCanvas.Stretch

The stretch mode to apply to the rectangle.

anchor TextureCanvas.Anchor

The anchor point for the rectangle.

samplingPattern SamplingPattern

The sampling pattern to use for drawing.

DrawTexture(Texture, Rectangle, Rectangle, Color4?, Stretch, Anchor, SamplingPattern)

Draws a texture to the TextureCanvas

public void DrawTexture(Texture sourceTexture, Rectangle sourceRect, Rectangle destinationRect, Color4? colorMultiplier = null, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)

Parameters

sourceTexture Texture

The source texture to draw

sourceRect Rectangle

The sub rectangle of the source texture to draw

destinationRect Rectangle

The sub rectangle of the target texture

colorMultiplier Color4?

The color multiplier. Default is White

stretch TextureCanvas.Stretch

The stretch mode

anchor TextureCanvas.Anchor

The anchor mode

samplingPattern SamplingPattern

The sampling pattern

DrawTexture(Texture, Rectangle, Color4?, Stretch, Anchor, SamplingPattern)

Draws a texture to the TextureCanvas

public void DrawTexture(Texture sourceTexture, Rectangle destinationRect, Color4? colorMultiplier = null, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)

Parameters

sourceTexture Texture

The source texture to draw

destinationRect Rectangle

The sub rectangle of the target texture in percentages

colorMultiplier Color4?

The color multiplier. Default is White

stretch TextureCanvas.Stretch

The stretch mode

anchor TextureCanvas.Anchor

The anchor mode

samplingPattern SamplingPattern

The sampling pattern

DrawTexture(Texture, RectangleF, RectangleF, Color4?, Stretch, Anchor, SamplingPattern)

Draws a texture to the TextureCanvas using rectangles with percentage values

public void DrawTexture(Texture sourceTexture, RectangleF relativeSourceRect, RectangleF relativeDestinationRect, Color4? colorMultiplier = null, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)

Parameters

sourceTexture Texture

The source texture to draw

relativeSourceRect RectangleF

The sub rectangle of the source texture to draw in percentages

relativeDestinationRect RectangleF

The sub rectangle of the target texture in percentages

colorMultiplier Color4?

The color multiplier. Default is White

stretch TextureCanvas.Stretch

The stretch mode

anchor TextureCanvas.Anchor

The anchor mode

samplingPattern SamplingPattern

The sampling pattern

DrawTexture(Texture, RectangleF, Color4?, Stretch, Anchor, SamplingPattern)

Draws a texture to the TextureCanvas using a rectangle with percentage values

public void DrawTexture(Texture sourceTexture, RectangleF relativeDestinationRect, Color4? colorMultiplier = null, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)

Parameters

sourceTexture Texture

The source texture to draw

relativeDestinationRect RectangleF

The sub rectangle of the target texture in percentages

colorMultiplier Color4?

The color multiplier. Default is White

stretch TextureCanvas.Stretch

The stretch mode

anchor TextureCanvas.Anchor

The anchor mode

samplingPattern SamplingPattern

The sampling pattern

DrawTexture(Texture, Color4?, Stretch, Anchor, SamplingPattern)

Draws a texture to the drawing context

public void DrawTexture(Texture sourceTexture, Color4? colorMultiplier = null, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)

Parameters

sourceTexture Texture

The source texture to draw

colorMultiplier Color4?

The color multiplier. Default is White

stretch TextureCanvas.Stretch

The stretch mode

anchor TextureCanvas.Anchor

The anchor mode

samplingPattern SamplingPattern

The sampling pattern

GaussianBlur(int, float)

Applies a GaussianBlur effect.

public void GaussianBlur(int radius = 4, float sigmaRatio = 2)

Parameters

radius int

The radius.

sigmaRatio float

The sigma ratio. The sigma ratio is used to calculate the sigma based on the radius: The actual formula is sigma = radius / SigmaRatio. The default value is 2.0f.

GetData()

Copies the current texture data to a byte array

public byte[] GetData()

Returns

byte[]

GetData(byte[])

Copies the current texture data to a byte array

public void GetData(byte[] data)

Parameters

data byte[]

The data array to store the data

Load(byte[])

Replaces the current TextureCanvas with a texture loaded from a byte[]

public void Load(byte[] data)

Parameters

data byte[]

The byte[] containing file data

Remarks

This method also replaces the current size and pixel format. Use Resample(Size2, PixelFormat, SamplingPattern) to change it afterwards.

Load(Stream)

Replaces the current TextureCanvas with a texture loaded from a Stream

public void Load(Stream stream)

Parameters

stream Stream

The Stream containing file data

Remarks

This method also replaces the current size and pixel format. Use Resample(Size2, PixelFormat, SamplingPattern) to change it afterwards.

Load(string)

Replaces the current TextureCanvas with a texture loaded from the local file system

public void Load(string path)

Parameters

path string

The file path

Remarks

This method also replaces the current size and pixel format. Use Resample(Size2, PixelFormat, SamplingPattern) to change it afterwards.

Recolorize(Color4?)

Applies a grey scale effect by copying the red channel to blue and green and optionally also recolors the resulting image.

public void Recolorize(Color4? colorMultiplier = null)

Parameters

colorMultiplier Color4?

The color multiplier. Default is White

Resample(Size2, PixelFormat, SamplingPattern)

Resamples the texture in a different size or pixel format

public void Resample(Size2 size, PixelFormat pixelFormat, SamplingPattern samplingPattern = SamplingPattern.Linear)

Parameters

size Size2

The new size

pixelFormat PixelFormat

The new pixel format

samplingPattern SamplingPattern

The sampling pattern

SetData(byte[], int, int, PixelFormat)

Replaces the current TextureCanvas with a texture loaded from a byte[] containing pixel data

public void SetData(byte[] data, int width, int height, PixelFormat pixelFormat)

Parameters

data byte[]

The byte[] containing pixel data

width int

The width of the new texture

height int

The height of the new texture

pixelFormat PixelFormat

The pixel format of the new texture

Remarks

This method also replaces the current size and pixel format. Use Resample(Size2, PixelFormat, SamplingPattern) to change it afterwards.

Store(Stream, ImageFileType)

Stores the current texture to a Stream in the selected file format

public void Store(Stream stream, ImageFileType fileType)

Parameters

stream Stream

The target stream

fileType ImageFileType

The file type to write

Store(string, ImageFileType?)

Stores the current texture to a file in the local file system.

public void Store(string path, ImageFileType? fileType = null)

Parameters

path string

The target file path

fileType ImageFileType?

The file type to write or null for automatic inference based on the file extension

ToTexture(TextureFlags)

Copies the current texture data to a new texture

public Texture ToTexture(TextureFlags flags = TextureFlags.ShaderResource)

Parameters

flags TextureFlags

Returns

Texture

Transform(IEnumerable<ColorTransform>?, IEnumerable<ColorTransform>?, IEnumerable<ColorTransform>?)

Applies a ColorTransformGroup effect.

public void Transform(IEnumerable<ColorTransform>? transforms = null, IEnumerable<ColorTransform>? preTransforms = null, IEnumerable<ColorTransform>? postTransforms = null)

Parameters

transforms IEnumerable<ColorTransform>

The color transforms to apply

preTransforms IEnumerable<ColorTransform>

The color pre-transforms to apply

postTransforms IEnumerable<ColorTransform>

The color post-transforms to apply