ImageBrush Class

A brush painting the pixels of an image.

public sealed class ImageBrush : Brush

Remarks

Placement. The image is in its own coordinates: its pixel (i, j) covers the square from (i, j) to (i + 1, j + 1), the pixel at the top-left corner of the frame being at the origin, and ImageBrush.Transform places it on the canvas. Without transformation, the pixels of the image are on the pixels of the canvas. ImageBrush.SourceRegion limits the image to some of its pixels, which stay where they are; ImageBrush.WrapModeX and ImageBrush.WrapModeY give the colors around them.

Colors. The color of a pixel of the canvas is the color of the image at the position its center comes from. With ImageSampling.Bilinear, the four pixels of the image around that position are interpolated at 16 bits, on alpha-premultiplied values, and the result is rounded to nearest; where the centers of the pixels of the image are on the centers of the pixels of the canvas (no transformation, or a translation by whole pixels), the colors are those of the image, exactly. The colors are then converted to the pixel format of the canvas like any brush color: they are sample values, and no color profile is applied. Floating-point images retain their range and precision on floating-point canvases. On integer canvases their samples are read as 16-bit colors: a sample outside [0, 1] or that is not a number has none, and the drawing that reads it throws an UnsupportedImageFeatureException instead of clipping it.

An image drawn smaller than it is skips pixels: each pixel of the canvas reads four pixels of the image at most. ImageSampling.Smooth reads every pixel a pixel of the canvas covers instead, for a result without aliasing, and ImageSampling.Box averages them; reduce an image that is drawn many times first (Resize).

Lifetime. The brush refers to the frame and does not own it. The pixels are read when a shape is drawn: the frame must still belong to a live image, and must not be one of the frames being drawn on.

Inheritance
Brush ImageBrush

Constructors

NameDescription
ImageBrush(ImageFrame)Initializes a new instance of the ImageBrush class.

ImageBrush(ImageFrame)

ImageBrush.cs

Initializes a new instance of the ImageBrush class.

public ImageBrush(ImageFrame source)

Parameters

source ImageFrame
The frame whose pixels are painted. It is not copied.

Exceptions

ArgumentNullException
source is null.

Properties

NameDescription
OpacityGets the opacity of the image, from 0 (nothing is painted) to 1 (the default: the image as it is).
SamplingGets how the image is read between the centers of its pixels. Defaults to ImageSampling.Bilinear.
SourceGets the frame whose pixels are painted.
SourceRegionGets the pixels of the frame the brush paints, or null (the default) for all of them. It is checked when a shape is drawn: it must then be inside the frame.
TransformGets the transformation that places the image on the canvas. Defaults to the identity.
WrapModeGets what the brush paints outside the pixels of the image, in both directions: setting it sets ImageBrush.WrapModeX and ImageBrush.WrapModeY, and getting it gets ImageBrush.WrapModeX. Defaults to ImageWrapMode.Transparent.
WrapModeXGets what the brush paints on the left and on the right of the pixels of the image: with ImageWrapMode.Repeat along x and ImageWrapMode.Transparent along y, a row of tiles (background-repeat: repeat-x of CSS). Defaults to ImageWrapMode.Transparent.
WrapModeYGets what the brush paints above and below the pixels of the image. Defaults to ImageWrapMode.Transparent.

Gets the opacity of the image, from 0 (nothing is painted) to 1 (the default: the image as it is).

public new double Opacity { get; init; }

Property value

double

Exceptions

ArgumentOutOfRangeException
The value is not between 0 and 1.

Remarks

The opacity of every brush (Brush.Opacity): it multiplies the alpha of the paint.

Gets how the image is read between the centers of its pixels. Defaults to ImageSampling.Bilinear.

public ImageSampling Sampling { get; init; }

Property value

ImageSampling

Exceptions

ArgumentOutOfRangeException
The value is not a defined ImageSampling.

Gets the frame whose pixels are painted.

public ImageFrame Source { get; }

Property value

ImageFrame

SourceRegion

ImageBrush.cs

Gets the pixels of the frame the brush paints, or null (the default) for all of them. It is checked when a shape is drawn: it must then be inside the frame.

public Rectangle? SourceRegion { get; init; }

Property value

Rectangle?

Transform

ImageBrush.cs

Gets the transformation that places the image 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 what the brush paints outside the pixels of the image, in both directions: setting it sets ImageBrush.WrapModeX and ImageBrush.WrapModeY, and getting it gets ImageBrush.WrapModeX. Defaults to ImageWrapMode.Transparent.

public ImageWrapMode WrapMode { get; init; }

Property value

ImageWrapMode

Exceptions

ArgumentOutOfRangeException
The value is not a defined ImageWrapMode.

WrapModeX

ImageBrush.cs

Gets what the brush paints on the left and on the right of the pixels of the image: with ImageWrapMode.Repeat along x and ImageWrapMode.Transparent along y, a row of tiles (background-repeat: repeat-x of CSS). Defaults to ImageWrapMode.Transparent.

public ImageWrapMode WrapModeX { get; init; }

Property value

ImageWrapMode

Exceptions

ArgumentOutOfRangeException
The value is not a defined ImageWrapMode.

WrapModeY

ImageBrush.cs

Gets what the brush paints above and below the pixels of the image. Defaults to ImageWrapMode.Transparent.

public ImageWrapMode WrapModeY { get; init; }

Property value

ImageWrapMode

Exceptions

ArgumentOutOfRangeException
The value is not a defined ImageWrapMode.