Filter Class

An effect applied to a drawing as a whole, or to what is behind it: a blur, a color matrix, a drop shadow, or a chain of them, as the filter and backdrop-filter properties of CSS.

public abstract class Filter

Remarks

A filter is applied by DrawingCanvas.Filter, to what a callback draws, and by DrawingCanvas.BackdropLayer, to what is already drawn behind a rectangle. Its lengths (the deviation of a blur, the offset of a shadow) are lengths of the drawing: a transformation of the canvas that enlarges the drawing enlarges them.

Arithmetic. The pixels are read as 16-bit colors and filtered as colors multiplied by their alpha, as fractions of one, kept in single precision between two filters of a chain and summed in double precision. The result is rounded to the samples of the layer it is written to: the alpha as floor(M a + 1/2 + 2^-20) and each color as floor(M p / a + 1/2 + 2^-20), with M the largest sample value. Filters are immutable and thread-safe.

Methods

NameDescription
TryGetBounds(RectangleD, RectangleD)Gets a rectangle that contains everything the filter paints when what it filters stays inside a rectangle: how far a blur or a shadow spreads, to size an image or a layer that keeps all of it.

TryGetBounds(RectangleD, RectangleD)

Filter.cs

Gets a rectangle that contains everything the filter paints when what it filters stays inside a rectangle: how far a blur or a shadow spreads, to size an image or a layer that keeps all of it.

public bool TryGetBounds(RectangleD bounds, out RectangleD result)

Parameters

bounds RectangleD
A rectangle that contains what is filtered.
result RectangleD
When the method returns true, a rectangle that contains what the filter paints; otherwise, an empty rectangle.

Returns

bool true when the filter paints within a rectangle; false when it can paint anywhere on the canvas.

Exceptions

ArgumentException
An edge of bounds is not a finite number.
ArgumentOutOfRangeException
The magnitude of an edge of bounds exceeds 16777216.

Remarks

The rectangles are in the coordinates of the drawing the filter is applied to, as its lengths are: before the transformation of the canvas. Filtered pixels are whole pixels: the pixels the result changes are within the rectangle rounded out to whole pixels.

A blur spreads by three times its standard deviation, rounded up; a drop shadow covers what it filters (unless DropShadowFilter.ShadowOnly) and the same rectangle moved by its offset and spread by its blur; a color matrix stays in the rectangle; a chain combines the rectangles of its filters; a filter graph stays in its FilterGraph.Region.

Some filters paint where nothing is drawn, so nothing bounds them but the canvas: a color matrix that adds to the alpha, a blur whose BlurFilter.EdgeMode reads the edges of the canvas, and a filter graph without a region (a flood or a turbulence fills it). The method returns false for those.