1. Create 2 geometries - a drawing area and an area to clear.
2. Use PushClip of DrawingContext to clip the combined geometry of the first 2 ones combined by Exclude mode.
Warning: Use GetOutlinedPathGeometry()! Operations may be done without it too, but it will get more artifacts with complex geometries, if you don't use it. Artifacts will appear by anyway, but much more less than without GetOutlinedPathGeometry().
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
visuals = new VisualCollection(this);
DrawIt();
}
VisualCollection visuals;
void DrawIt()
{
DrawingVisual visual = new DrawingVisual();
using (DrawingContext dc = visual.RenderOpen())
{
RectangleGeometry rect1 = new RectangleGeometry();
rect1.Rect = new Rect(10, 10, 100, 100);
RectangleGeometry rect2 = new RectangleGeometry();
rect2.Rect = new Rect(50, 50, 20, 20);
Geometry geom1 = rect1.GetOutlinedPathGeometry();
Geometry geom2 = rect2.GetOutlinedPathGeometry();
Geometry combined2 = new CombinedGeometry(GeometryCombineMode.Exclude, geom1, geom2);
dc.PushClip(combined2);
dc.DrawRectangle(
Brushes.White, new Pen(),
new Rect(10, 10, 100, 100));
}
visuals.Add(visual);
}
protected override Visual GetVisualChild(int index)
{
return visuals[index];
}
protected override int VisualChildrenCount
{
get { return visuals.Count; }
}
}