Customize your drawing in WPF (V)

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.