PatternBrush Class

A brush painting a drawing that repeats: a tile drawn by a callback, laid side by side in both directions.

public sealed class PatternBrush : Brush

Remarks

Tile. PatternBrush.Tile is the rectangle that repeats, in the coordinates of the pattern, and PatternBrush.Content draws it, in those coordinates: with a tile at (0, 0) of 16 by 16, the callback draws between 0 and 16, and the same drawing is at (16, 0), (0, 16) and so on. What the callback draws outside the tile is not painted. PatternBrush.Transform places the pattern on the canvas, as it places a gradient or an image; without it, the coordinates of the pattern are those of the canvas, whatever the shape that is painted.

Rendering. When a shape is painted, the tile is drawn once, on an image of the size the transformation (the one of the brush, then the one of the canvas) gives it, with the spacing (PatternBrush.SpacingX, PatternBrush.SpacingY) on its right and below it, transparent, then that image is laid over the shape like the image of an ImageBrush with the wrap modes of the pattern (ImageWrapMode.Repeat by default): a pattern that is enlarged stays as sharp as its drawing. The image has the width ceil(w - 1/1024), where w is the width of the tile and of its spacing on the canvas (and the same for its height), so that a tile whose size is a whole number of pixels is drawn on exactly that many: its pixels are then those of the drawing, exactly, when it is placed on whole pixels. Otherwise the image is interpolated (ImageSampling.Bilinear), also across the edges of the tiles. The image has colors and alpha, with 8-bit samples on an 8-bit color canvas and 16-bit samples otherwise, as a layer has, or 8-bit samples on every canvas with PatternBrush.TilePrecision. The canvas the callback receives is that image: its DrawingCanvas.Width and DrawingCanvas.Height are its size in pixels, not the size of the tile, and filling it fills the tile.

A tile whose image would have more than four times the pixels the clip can show (and 65,536), or would exceed the limits of the image, is drawn where the canvas shows it: along each axis where the canvas shows less than a tile, the image holds that part of the pattern, at the same scale, and the callback runs once for each tile it shows (two at most, mirrored for a tile that ImageWrapMode.Reflect turns), clipped to its tile. Under a projective transformation, the image of the tile is drawn at the resolution those limits allow instead.

Callback. The callback runs while a shape is drawn, before any pixel of that shape changes, and may run once for each shape: it must draw the same thing every time, must not be asynchronous and must not keep the canvas it receives. It can use any brush except, directly or not, the pattern itself. A brush that is kept and reused keeps its callback, and everything the callback refers to.

Inheritance
Brush PatternBrush

Constructors

NameDescription
PatternBrush(HatchStyle, Rgba64, Rgba64)Initializes a new instance of the PatternBrush class that repeats a hatch: a tile of 8 by 8 pixels of two colors.
PatternBrush(RectangleD, DrawingAction)Initializes a new instance of the PatternBrush class.

PatternBrush overloads

PatternBrush(HatchStyle, Rgba64, Rgba64)

PatternBrush.cs

Initializes a new instance of the PatternBrush class that repeats a hatch: a tile of 8 by 8 pixels of two colors.

public PatternBrush(HatchStyle style, Rgba64 foreground, Rgba64 background)
Parameters
style HatchStyle
The pixels of the tile that have the foreground color.
foreground Rgba64
The color of the lines or of the pixels style lights, with straight alpha. An Rgba32 converts implicitly.
background Rgba64
The color of the other pixels, with straight alpha: a transparent color leaves them unpainted.
Exceptions
ArgumentOutOfRangeException
style is not a defined HatchStyle.
Remarks

The tile is the rectangle from (0, 0) to (8, 8), and each of its pixels is painted with one of the two colors: without a PatternBrush.Transform and on a canvas without transformation, the pixel (x, y) of the canvas has the color of the pixel (x mod 8, y mod 8) of the tile, exactly, whatever the shape that is painted. A transformation scales and turns the hatch like any pattern.

PatternBrush(RectangleD, DrawingAction)

PatternBrush.cs

Initializes a new instance of the PatternBrush class.

public PatternBrush(RectangleD tile, DrawingAction content)
Parameters
tile RectangleD
The rectangle that repeats, in the coordinates of the pattern. A Rectangle converts implicitly.
content DrawingAction
The callback that draws the tile, in the coordinates of the pattern.
Exceptions
ArgumentNullException
content is null.
ArgumentException
A coordinate of tile is not a finite number.
ArgumentOutOfRangeException
The magnitude of a coordinate of tile exceeds 16777216, or its width or its height is not positive.

Properties

NameDescription
ContentGets the callback that draws the tile.
SpacingXGets the gap between two tiles along x, in the coordinates of the pattern: nothing is painted there (background-repeat: space of CSS, once the gap is computed). Defaults to 0.
SpacingYGets the gap between two tiles along y, in the coordinates of the pattern. Defaults to 0.
TileGets the rectangle that repeats, in the coordinates of the pattern.
TilePrecisionGets the samples of the image the tile is drawn on: those of the canvas (PatternTilePrecision.Canvas, the default), or 8-bit samples on every canvas (PatternTilePrecision.EightBit), as a browser rasterizes the drawing of a canvas into an 8-bit bitmap before it paints it.
TransformGets the transformation that places the pattern on the canvas. Defaults to the identity.
WrapModeXGets how the tile is laid along x: ImageWrapMode.Repeat (the default) lays it side by side, ImageWrapMode.Transparent paints the tile once along x (background-repeat: repeat-y of CSS with PatternBrush.WrapModeY left to repeat), ImageWrapMode.Reflect mirrors every other tile, and ImageWrapMode.Pad extends the pixels of the edges of the image of the tile.
WrapModeYGets how the tile is laid along y. Defaults to ImageWrapMode.Repeat.

Gets the callback that draws the tile.

public DrawingAction Content { get; }

Property value

DrawingAction

Gets the gap between two tiles along x, in the coordinates of the pattern: nothing is painted there (background-repeat: space of CSS, once the gap is computed). Defaults to 0.

public double SpacingX { get; init; }

Property value

double

Exceptions

ArgumentException
The value is not a finite number.
ArgumentOutOfRangeException
The value is negative or exceeds 16777216.

Gets the gap between two tiles along y, in the coordinates of the pattern. Defaults to 0.

public double SpacingY { get; init; }

Property value

double

Exceptions

ArgumentException
The value is not a finite number.
ArgumentOutOfRangeException
The value is negative or exceeds 16777216.

Gets the rectangle that repeats, in the coordinates of the pattern.

public RectangleD Tile { get; }

Property value

RectangleD

TilePrecision

PatternBrush.cs

Gets the samples of the image the tile is drawn on: those of the canvas (PatternTilePrecision.Canvas, the default), or 8-bit samples on every canvas (PatternTilePrecision.EightBit), as a browser rasterizes the drawing of a canvas into an 8-bit bitmap before it paints it.

public PatternTilePrecision TilePrecision { get; init; }

Property value

PatternTilePrecision

Exceptions

ArgumentOutOfRangeException
The value is not a defined PatternTilePrecision.

Remarks

With 8-bit samples, the callback draws as on an 8-bit color canvas (its colors are rounded to 8 bits, and so is every compositing), and the shape is painted with that image as with an ImageBrush of an Rgba32 image. On an 8-bit color canvas, both values draw the same pixels.

Gets the transformation that places the pattern on the canvas. Defaults to the identity.

public AffineTransform Transform { get; init; }

Property value

AffineTransform

Remarks

With a transformation that flattens the plane to a line or a point, the brush paints nothing.

Gets how the tile is laid along x: ImageWrapMode.Repeat (the default) lays it side by side, ImageWrapMode.Transparent paints the tile once along x (background-repeat: repeat-y of CSS with PatternBrush.WrapModeY left to repeat), ImageWrapMode.Reflect mirrors every other tile, and ImageWrapMode.Pad extends the pixels of the edges of the image of the tile.

public ImageWrapMode WrapModeX { get; init; }

Property value

ImageWrapMode

Exceptions

ArgumentOutOfRangeException
The value is not a defined ImageWrapMode.

Gets how the tile is laid along y. Defaults to ImageWrapMode.Repeat.

public ImageWrapMode WrapModeY { get; init; }

Property value

ImageWrapMode

Exceptions

ArgumentOutOfRangeException
The value is not a defined ImageWrapMode.