周银辉
图形合并
有时候我们需要将多个图形合并成一个然后进行绘制,比如将一个圆形与一个矩形进行合并等. 在WPF的自定义绘制中,有三种方法可以做到,分别是(1)利用GeometryGroup对象;(2)利用CombinedGeometry对象;(3)使用Geometry.Combin()静态方法。其中第一种方式是利用集合并可以向集合中添加任意多个元素,而后面两种方式只能两两合并,但后面两者的合并方式更灵活,可以是两图形的“交集”“并集”“差集”以及“异或”。
1 ,使用GeometryGroup对象进行图形合并
参考以下代码,我们合并了一个椭圆与一个矩形:
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);
}
效果图如下:{
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);
}
我们只是简单地将两个图形以相加的方式合并在了一起,注意到合并后的图形中间部分出现一个镂空的无色区域,是由合并后的图形的FillRull决定的,如果我们把group.FillRule = FillRule.EvenOdd;更改为 group.FillRule = FillRule.Nonzero;则填充效果将是如下这样:
2, 使用CombinedGeometry对象进行图形合并
参考以下代码,我们合并了一个椭圆与一个矩形:
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
//public sealed class CombinedGeometry : Geometry
EllipseGeometry ellipse = new EllipseGeometry(new Point(50, 50), 50, 20);
RectangleGeometry rect = new RectangleGeometry(new Rect(50, 50, 50, 20), 5, 5);
CombinedGeometry combin = new CombinedGeometry(GeometryCombineMode.Xor, ellipse, rect);
dc.DrawGeometry(Brushes.LightBlue, new Pen(Brushes.Green, 2), combin);
}
注意到GeometryCombineMode枚举,它有四个枚举值,如果我们有A,B分别表示两个图形的话,那么:{
base.OnRender(dc);
//public sealed class CombinedGeometry : Geometry
EllipseGeometry ellipse = new EllipseGeometry(new Point(50, 50), 50, 20);
RectangleGeometry rect = new RectangleGeometry(new Rect(50, 50, 50, 20), 5, 5);
CombinedGeometry combin = new CombinedGeometry(GeometryCombineMode.Xor, ellipse, rect);
dc.DrawGeometry(Brushes.LightBlue, new Pen(Brushes.Green, 2), combin);
}
GeometryCombineMode.Exclude: 合并结果为A-B
GeometryCombineMode.Intersect: 合并结果为A与B的相交部分
GeometryCombineMode.Union: 合并结果为A+B
GeometryCombineMode.Xor: 合并结果为(A-B)+(B-A)
3, 使用Geometry.Combin()静态方法进行图形合并
Geometry.Combin()静态方法与使用CombinedGeometry对象进行图形合并差不多
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
//Geometry.Combin()
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);
dc.DrawGeometry(Brushes.LightBlue, new Pen(Brushes.Green, 2), combin);
}
其中Geometry.Combin()静态方法中有用于图形变换的参数(旋转,缩放等),如果不需要变换则填写null{
base.OnRender(dc);
//Geometry.Combin()
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);
dc.DrawGeometry(Brushes.LightBlue, new Pen(Brushes.Green, 2), combin);
}