Graphics merge
Sometimes we need to combine multiple graphs into one and then draw them, such as merging a circle with a rectangle. In the custom drawing of WPF, there are three ways to do it, respectively (1) using the GeometryGroup object, (2) using the Combinedgeometry object, and (3) using the Geometry.combin () static method. The first is to use set consolidation to add as many elements as you can to the collection, and the following two methods can only be merged in 22, but the latter are more flexible to merge, and can be the intersection of two graphics and set "difference Sets" and "XOR."
1, using the GeometryGroup object for graphic merging
Referring to the following code, we combine an ellipse with a rectangle:
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
//public sealed class GeometryGroup : Geometry
EllipseGeometry ellipse = new EllipseGeometry(new Point(50, 50), 50, 20);
RectangleGeometry rect = new RectangleGeometry(new Rect(50, 50, 50, 20), 5, 5);
GeometryGroup group = new GeometryGroup();
group.FillRule = FillRule.EvenOdd;
group.Children.Add(ellipse);
group.Children.Add(rect);
dc.DrawGeometry(Brushes.LightBlue, new Pen(Brushes.Green, 2), group);
}
The effect chart is as follows:
We simply merged the two graphs together, noting that a hollow, colorless area appears in the middle part of the merged graph, which is determined by the fillrull of the merged graph, if we put group. FillRule = fillrule.evenodd, change to group. FillRule = Fillrule.nonzero; the fill effect will be as follows: