WPF Geometry

在WPF图形体系中,Geometry类表示几何图形的基类,使用的时候是实例化它的一些子类,具体的有:

子类介绍:

基本几何图形

几何图形集合

路径集合图形PathGeometry里可以包含一系列几何图形集合,常见的有:

  1. BezierSegment:在两个点之间创建一条三次方贝塞尔曲线。

  2. PolyBezierSegment:创建一系列三次方贝塞尔曲线。

  3. PolyQuadraticBezierSegment:创建一系列二次贝塞尔曲线。

  4. QuadraticBezierSegment:创建一条二次贝塞尔曲线。

几何图形集合的另一种形式

除了这种组合的方式之外,系统还提供了一个通过一系列API来绘制的StreamGeometry。它不支持绑定,动画,相应也更加灵活而高效。

复合几何图形

使用 GeometryGroupCombinedGeometry 或者通过调用静态的 Geometry 方法 Combine,可以创建复合几何图形对象。它们主要的区别是:

  • CombinedGeometry 对子图形进行叠加操作,没有面积的子图形将被丢弃。只能组合两个子图形(但是这两个子图形也可以是复合几何图形)。

  • GeometryGroup 只进行组合,而不进行面积叠加。可以添加多个子图形。有关示例,请参见如何:创建复合形状

CombinedGeometry的叠加方式有四种:UnionIntersectExcludeXor,它们的效果为:

圈起来的Line Rectangle Ellipse 说的是Shape,比较简单,可以直接在xaml中显示,这里不赘述:

呈现方式

Geometry对象并不能作为图像独立呈现出来,它一般有如下几种呈现方式:

在Path中呈现

可以作为GeometryDrawing.Geometry的参数呈现为Path对象,以下xaml演示了这一方法:

基本几何图形

XML 复制代码
  <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>
复制代码
  

几何图形集合

XML 复制代码
 <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>

简写:

XML 复制代码
<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>

继续简写:

XML 复制代码
  <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中可以写成下面的形式:

XML 复制代码
  <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>

复合几何图形

XML 复制代码
 <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:

XML 复制代码
  <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

相关推荐
武藤一雄16 小时前
WPF/C# 应对消息洪峰与数据抖动的 8 种“抗压”策略
windows·微软·c#·wpf·.netcore·防抖·鲁棒性
武藤一雄2 天前
WPF深度解析Behavior
windows·c#·.net·wpf·.netcore
Maybe_ch2 天前
WPF的STA线程模型、APM与TAP:从线程约束到现代异步
c#·.net·wpf
FuckPatience2 天前
WPF 实现windows文件压缩文件解压过程动画
wpf
会飞的大可2 天前
Spring Cloud Alibaba全景:Nacos、Sentinel、Seata整合实战
sentinel·wpf
baivfhpwxf20232 天前
DataGrid 中增加选择列 功能实现
ui·wpf
czhc11400756633 天前
winform 330 跨线程 异步
wpf·线程·winform
想你依然心痛3 天前
HarmonyOS 5.0教育行业解决方案:基于分布式能力的沉浸式智慧课堂系统
分布式·wpf·harmonyos
Maybe_ch3 天前
深度解析 WPF 线程模型:告别 UI 卡死,掌握 Dispatcher 核心机制
ui·wpf
code bean3 天前
【Halcon 】用 Halcon 实现涂抹:Region、仿射变换与 WPF 交互
wpf·交互·halcon