To turn our drawings into brushes.
The brushes in WPF are much more powerful than GDI +, except for the commonly used solid brushes and gradient brushes, which also support more tile brushes, including DrawingBrush, ImageBrush, VisualBrush, Where DrawingBrush allows us to use custom drawing for any place where a common brush can be used.
For example, the following code, we will customize an ellipse and a rectangle, and then merge them into a graph and
To change a graphic as a background brush for a window:
public Window1()
{
InitializeComponent();
//background
EllipseGeometry ellipse = new EllipseGeometry(new Point(50, 50), 50, 20);
RectangleGeometry rect = new RectangleGeometry(new Rect(50, 50, 50, 20), 5, 5);
PathGeometry combin = Geometry.Combine(ellipse, rect, GeometryCombineMode.Xor, null);
GeometryDrawing drawing = new GeometryDrawing(Brushes.LightBlue, new Pen(Brushes.Green, 2), combin);
DrawingBrush background = new DrawingBrush(drawing);
this.Background = background;
}
The operation effect is as follows:
Maybe it's not the result we want to fill the entire area with, and it doesn't matter, we can adjust the tile effect by setting the viewport (Viewport) for the Peaceful Way (Tilemode).
Viewport has instructed us how to project the graph onto the drawing surface, if we define the upper-left corner of the drawing surface as (0,0) and the lower-right corner defines me (1,1), then you can set a rectangle (Rect) that is equivalent to that value to indicate where our graphics should be projected. For example, rect (0,0,0.5,0.5) indicates that the length of the upper-left corner of the drawing surface is a rectangular area of 1/2 of the drawing surface. Such rectangular rect (0,0,0.5,0.5) is what we call the viewport (similar to the viewport in 3D). So to get the effect of the above diagram, we just have to set our DrawingBrush on it:
background.Viewport = new Rect(0, 0, 0.15, 0.15);
background.TileMode = TileMode.Tile;
In addition, DrawingBrush has an interesting attribute Viewbox (inherited from TileBrush), which indicates which part of the graph is taken as the viewing area (which differs from clip) and is defined in the same way as viewport. But it is not relative to the drawing surface but is equivalent to our graphics.
This explains the Viewbox in the SDK: