在WPF图形体系中,Geometry类表示几何图形的基类,使用的时候是实例化它的一些子类,具体的有:
子类介绍:
基本几何图形
线段:LineGeometry
矩形:RectangleGeometry
椭圆:EllipseGeometry
几何图形集合
路径集合图形PathGeometry里可以包含一系列几何图形集合,常见的有:
线段: LineSegment
弧:ArcSegment
贝塞尔曲线系列:
BezierSegment:在两个点之间创建一条三次方贝塞尔曲线。
PolyBezierSegment:创建一系列三次方贝塞尔曲线。
PolyQuadraticBezierSegment:创建一系列二次贝塞尔曲线。
QuadraticBezierSegment:创建一条二次贝塞尔曲线。
几何图形集合的另一种形式
除了这种组合的方式之外,系统还提供了一个通过一系列API来绘制的StreamGeometry。它不支持绑定,动画,相应也更加灵活而高效。
复合几何图形
使用 GeometryGroup、CombinedGeometry 或者通过调用静态的 Geometry 方法 Combine,可以创建复合几何图形对象。它们主要的区别是:
CombinedGeometry 对子图形进行叠加操作,没有面积的子图形将被丢弃。只能组合两个子图形(但是这两个子图形也可以是复合几何图形)。
GeometryGroup 只进行组合,而不进行面积叠加。可以添加多个子图形。有关示例,请参见如何:创建复合形状。
CombinedGeometry的叠加方式有四种:Union、Intersect、Exclude 和 Xor,它们的效果为:
圈起来的Line Rectangle Ellipse 说的是Shape,比较简单,可以直接在xaml中显示,这里不赘述:
呈现方式
Geometry对象并不能作为图像独立呈现出来,它一般有如下几种呈现方式:
在Path中呈现
可以作为GeometryDrawing.Geometry的参数呈现为Path对象,以下xaml演示了这一方法:
基本几何图形
<Path Grid.Row="0" Grid.Column="0" Stroke="Blue" StrokeThickness="2"> <Path.Data> <LineGeometry StartPoint="20,20" EndPoint="140,140" /> </Path.Data> </Path> <Path Grid.Row="0" Grid.Column="1" Fill="Yellow" Stroke="Orange"> <Path.Data> <RectangleGeometry RadiusX="10" RadiusY="10" Rect="20,20,120,120" /> </Path.Data> </Path> <Path Grid.Row="1" Grid.Column="0" Fill="LawnGreen" Stroke="Green"> <Path.Data> <EllipseGeometry Center="80,80" RadiusX="60" RadiusY="40" /> </Path.Data> </Path>几何图形集合
<Path Grid.Row="3" Grid.Column="1" Fill="LawnGreen" Stroke="Green"> <Path.Data> <PathGeometry> <PathGeometry.Figures> <PathFigure IsClosed="True" StartPoint="25,140"> <!-- 以上一条的终点为起点 --> <LineSegment Point="20,40" /> <LineSegment Point="40,110" /> <LineSegment Point="50,20" /> <LineSegment Point="80,110" /> <LineSegment Point="110,20" /> <LineSegment Point="120,110" /> <LineSegment Point="140,40" /> <LineSegment Point="135,140" /> </PathFigure> </PathGeometry.Figures> </PathGeometry> </Path.Data> </Path>简写:
<Path Grid.Row="3" Grid.Column="1" Fill="LawnGreen" Stroke="Green"> <Path.Data> <PathGeometry Figures="M 25,140 L 20,40 40,110 50,20 80,110 110,20 120,110 140,40 135,140 Z"/> </Path.Data> </Path>继续简写:
<Path Grid.Row="3" Grid.Column="1" Fill="LawnGreen" Stroke="Green" Data="M 25,140 L 20,40 40,110 50,20 80,110 110,20 120,110 140,40 135,140 Z"/>几何图形集合的另一种形式
StreamGeometry不支持数据绑定,没有什么依赖属性,所以在xaml中没什么写的,更多是在代码中操作。不过在xaml中可以写成下面的形式:
<Path Grid.Row="2" Grid.Column="1" Stroke="Black" StrokeThickness="2"> <Path.Data> <StreamGeometry>M 10,10 L 100,10 100,100 10,100 Z</StreamGeometry> </Path.Data> </Path>复合几何图形
<Path Grid.Row="3" Grid.Column="0" Stroke="Black" StrokeThickness="2" Fill="LightBlue"> <Path.Data> <GeometryGroup> <!-- 矩形 --> <RectangleGeometry Rect="10,10,100,50"/> <!-- 椭圆 --> <EllipseGeometry Center="60,60" RadiusX="40" RadiusY="20"/> <!-- 线条路径 --> <PathGeometry> <PathGeometry.Figures> <PathFigure StartPoint="100,100"> <LineSegment Point="150,50"/> <LineSegment Point="200,100"/> </PathFigure> </PathGeometry.Figures> </PathGeometry> <!-- 简写线条路径 --> <PathGeometry Figures="M 50,120 L 100,80 150,120"/> </GeometryGroup> </Path.Data> </Path>在DrawingContext中呈现
可以作为DrawingContext. DrawGeometry的参数呈现
在GeometryDrawing中呈现
可以作为GeometryDrawing.Geometry的参数呈现为Drawing对象
图为GeometryDrawing中的属性定义:因为继承自Drawing的类不是元素(没有继承UIElement类),不能将它们放置到用户界面中,还需要用于显示图画的类; 所以,Drawing对象也不能独立呈现,一般是作为DrawingBrush或作为DrawingContext.DrawDrawing的参数来使用的;
直接写在xaml中报错:
作为DrawingBrush:
<Button> <Button.Background> <DrawingBrush Stretch="Fill" Viewport="0.1 0.1 0.8 0.8"> <DrawingBrush.Drawing> <GeometryDrawing Brush="Red"> <GeometryDrawing.Pen> <Pen Brush="Black" Thickness="0" /> </GeometryDrawing.Pen> <GeometryDrawing.Geometry> <EllipseGeometry Center="50 50" RadiusX="10" RadiusY="5" /> </GeometryDrawing.Geometry> </GeometryDrawing> </DrawingBrush.Drawing> </DrawingBrush> </Button.Background> </Button>DrawingContext的DrawDrawing方法,接受Drawing对象:
引用:
WPF的二维绘图(二)——几何图形Geometry - MaxBruce - 博客园
https://blog.csdn.net/zhudaokuan/article/details/110633190?fromshare=blogdetail&sharetype=blogdetail&sharerId=110633190&sharerefer=PC&sharesource=qq_59062726&sharefrom=from_link