Path类继承自Shape,可以绘制很多简单的,复合的图形。Path类通过提供的Data属性,Data属性接受一个Geometry对象(我的理解就是Data要装什么集合图形呀),Geometry一共有7个派生类,说明如下:

名称 说明
LineGeometry 绘制直线
RectangleGeometry 绘制矩形(包括原型拐角的举行)
EllipseGeometry 绘制椭圆
GeometryGroup

通过组合的形式添加多个Geometry对象,使用EvenOdd(具有穿透效果)或NonZero填充规则确定填充区域;

CombinedGeomotry 合并两个几何图形,通过CombineMode属性控制合并方式。
PathGeometry 制作复杂的几何图形,弧线,曲线等。
StreamGeometry 不太常用,不介绍啦

下面 通过图例的方式一次介绍这些Geometry。

1.LineGeometry。通过StartPoint,EndPoint两个点确定一条直线。

2.RectangleGeometry。通过Rect属性确定左上角,右下角两个点位置画出矩形。

3.EllipseGeometry。RadiousX确定横向半径,RadiousY确定纵向半径,Center确定椭圆的圆心位置。以下为示例:

<Grid><Grid.RowDefinitions><RowDefinition ></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"></ColumnDefinition><ColumnDefinition Width="Auto"></ColumnDefinition></Grid.ColumnDefinitions><Label Grid.Row="0" Grid.Column="0" Content="LineGeometry:" VerticalAlignment="Center"></Label><Canvas Grid.Row="0" Grid.Column="1"><Path Fill="Yellow" Stroke="Blue" StrokeThickness="5"><Path.Data><LineGeometry StartPoint="10,50" EndPoint="200,50"></LineGeometry></Path.Data></Path></Canvas><Label Grid.Row="1" Grid.Column="0" Content="RectangleGeometry:" VerticalAlignment="Center"></Label><Canvas Grid.Row="1" Grid.Column="1"><Path Fill="Yellow" Stroke="Blue" StrokeThickness="5"><Path.Data><RectangleGeometry Rect="40,10 100,70"></RectangleGeometry></Path.Data></Path></Canvas><Label Grid.Row="2" Grid.Column="0" Content="EllipseGeometry:" VerticalAlignment="Center"></Label><Canvas Grid.Row="2" Grid.Column="2"><Path Fill="Yellow" Stroke="Blue" StrokeThickness="5"><Path.Data><EllipseGeometry RadiusX="35" RadiusY="35" Center="90,45"></EllipseGeometry></Path.Data></Path></Canvas></Grid>

运行效果:

4.GeometryGroup。在Group中添加多个图形,这样的好处是降低开销。这个示例在我的上一篇有介绍,这里就不在重复说明了,链接如下:https://blog.csdn.net/chulijun3107/article/details/105351693

5.CombinedGeomotry。Combined的字面意思是联合,那这个就是要取两个图形联合的情况了,那究竟是取相交部分?联合部分?还是其他呢?GeometryCombineMode属性来体现,枚举值如下:

枚举值 说明
Union 两个图形所有部分
Intersect 两个图形相交部分
Xor 两个图形的非公有部分
Exclude 第一个图形的所有部分,不包含第二个图形的分部

下边看一个例子来说明这几个枚举值的作用:

  <Window.Resources><RectangleGeometry x:Key="rect" Rect="0 0 100 100"></RectangleGeometry><EllipseGeometry x:Key="ellipse" Center="85 50" RadiusX="65" RadiusY="35"></EllipseGeometry></Window.Resources><Grid Margin="5" TextBlock.FontSize="16"><Grid.RowDefinitions><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="Auto"></RowDefinition><RowDefinition Height="Auto"></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"></ColumnDefinition><ColumnDefinition Width="Auto"></ColumnDefinition></Grid.ColumnDefinitions><TextBlock Grid.Column="0" Margin="10" VerticalAlignment="Center">Union</TextBlock><Path Grid.Column="1" Fill="Yellow" Stroke="Blue" Margin="5"><Path.Data><CombinedGeometry GeometryCombineMode="Union" CombinedGeometry.Geometry1="{StaticResource rect}" CombinedGeometry.Geometry2="{StaticResource ellipse}"></CombinedGeometry></Path.Data></Path><TextBlock Grid.Row="1" Grid.Column="0" Margin="10" VerticalAlignment="Center">Intersect</TextBlock><Path Grid.Row="1" Grid.Column="1" Fill="Yellow" Stroke="Blue" Margin="5"><Path.Data><CombinedGeometry GeometryCombineMode="Intersect"  CombinedGeometry.Geometry1="{StaticResource rect}" CombinedGeometry.Geometry2="{StaticResource ellipse}"></CombinedGeometry></Path.Data></Path><TextBlock Grid.Row="2" Grid.Column="0" Margin="10" VerticalAlignment="Center">Xor</TextBlock><Path Grid.Row="2" Grid.Column="2" Fill="Yellow" Stroke="Blue" Margin="5"><Path.Data><CombinedGeometry GeometryCombineMode="Xor"  CombinedGeometry.Geometry1="{StaticResource rect}" CombinedGeometry.Geometry2="{StaticResource ellipse}"></CombinedGeometry></Path.Data></Path><TextBlock Grid.Row="3" Grid.Column="0" Margin="10" VerticalAlignment="Center">Exclude</TextBlock><Path Grid.Row="3" Grid.Column="1" Fill="Yellow" Stroke="Blue" Margin="5"><Path.Data><CombinedGeometry GeometryCombineMode="Exclude"  CombinedGeometry.Geometry1="{StaticResource rect}"  CombinedGeometry.Geometry2="{StaticResource ellipse}"></CombinedGeometry></Path.Data></Path></Grid>

运行效果:

下面我们做一个稍微复杂的图形,禁止停车的图案。原图如下,这个图案的组成是外边一个红色的圆,StrokeThickness值设置大一些,Fill用蓝色,然后两个左右倾斜45°的矩形组成。当然你也可以用两条足够粗的线段。

好的,我们看一下代码如何实现:

先实现红色圆部分和两个旋转的矩形:

<Canvas><Path Fill="Red" ><Path.Data><CombinedGeometry GeometryCombineMode="Union"><CombinedGeometry.Geometry1><CombinedGeometry GeometryCombineMode="Exclude"><CombinedGeometry.Geometry1><EllipseGeometry Center="50,50" RadiusX="50" RadiusY="50"></EllipseGeometry></CombinedGeometry.Geometry1><CombinedGeometry.Geometry2><EllipseGeometry Center="50,50" RadiusX="37" RadiusY="37"></EllipseGeometry></CombinedGeometry.Geometry2></CombinedGeometry></CombinedGeometry.Geometry1><CombinedGeometry.Geometry2><CombinedGeometry GeometryCombineMode="Union"><CombinedGeometry.Geometry1><RectangleGeometry Rect="44,5 10,90"><RectangleGeometry.Transform><RotateTransform Angle="45" CenterX="50" CenterY="50"></RotateTransform></RectangleGeometry.Transform></RectangleGeometry></CombinedGeometry.Geometry1><CombinedGeometry.Geometry2><RectangleGeometry Rect="44,5 10,90"><RectangleGeometry.Transform><RotateTransform Angle="135" CenterX="50" CenterY="50"></RotateTransform></RectangleGeometry.Transform></RectangleGeometry></CombinedGeometry.Geometry2></CombinedGeometry></CombinedGeometry.Geometry2></CombinedGeometry></Path.Data></Path></Canvas>

运行效果是这样的,还没有在中间添加蓝色,因为要用到接下来的PathGeometry。

6.PathGeometry。绘图逻辑。PathFigure是几何图形的子部分,它可以包含LineSegment,ArcSegment,以下图为例,StartPoint设置了扇形的起点。然后通过LineSegment绘制一条直线。ArcSegment 的Point属性设置扇形的起点,Size设置扇形的横向,纵向圆的半径。然后绘制4个扇形组成上边的禁止停车标志。

<Path Fill="Blue"><Path.Data><PathGeometry><PathFigure StartPoint="57,48.5"><LineSegment Point="79,26"></LineSegment><ArcSegment Point="80,72.5" Size="68,50" SweepDirection="Clockwise"></ArcSegment><LineSegment Point="57,48.5"></LineSegment></PathFigure></PathGeometry></Path.Data></Path>

看看绘制四个扇形的效果:

好,我们最后把这红色和蓝色的组合在一起:

图标源码地址:

我们再做个联系,通过旋转图形绘制三菱车车标。这是我在网上找到的原图标图案:

做三个菱形,然后旋转-115度,115度。就可以了。

 <Canvas Width="120" Height="120" ><Path Stroke="Red" Fill="Red"><Path.Data><PathGeometry><PathFigure IsClosed="True" StartPoint="50,0"><LineSegment Point="75,50"></LineSegment><LineSegment Point="50,100"></LineSegment><LineSegment Point="25,50"></LineSegment></PathFigure></PathGeometry></Path.Data></Path><Path Stroke="Red" Fill="Red"><Path.Data><PathGeometry><PathFigure IsClosed="True" StartPoint="50,0"><LineSegment Point="75,50"></LineSegment><LineSegment Point="50,100"></LineSegment><LineSegment Point="25,50"></LineSegment></PathFigure><PathGeometry.Transform><RotateTransform Angle="-115" CenterX="50" CenterY="100"></RotateTransform></PathGeometry.Transform></PathGeometry></Path.Data></Path><Path Stroke="Red" Fill="Red"><Path.Data><PathGeometry><PathFigure IsClosed="True" StartPoint="50,0"><LineSegment Point="75,50"></LineSegment><LineSegment Point="50,100"></LineSegment><LineSegment Point="25,50"></LineSegment></PathFigure><PathGeometry.Transform><RotateTransform Angle="115" CenterX="50" CenterY="100"></RotateTransform></PathGeometry.Transform></PathGeometry></Path.Data></Path></Canvas>

运行效果如下:

WPF 使用Path绘制几何图形相关推荐

  1. WPF之路——绘制几何图形

    一.Geometry和Share Geometry类(几何绘图)包括,LineGeometry(几何线条).RectangleGeometry(几何矩形).EllipesGeometry(几何椭圆图形 ...

  2. WPF组合Path绘制

    假设我们需要绘制如图的组合图形: 一般的解决方案是如下代码,创建一个Canvas包含这三部分,如左图所示. <Canvas Margin="508,604,-508,-604" ...

  3. Sliverlight中使用Path绘制复杂几何图形

    Sliverlight中使用Path绘制复杂几何图形 转自http://www.riafan.com/article/silverlight/draw-with-path.html 在Sliverli ...

  4. C# 画(绘制)直线 C#如何画直线 C#绘制直线 WPF 画(绘制)直线

    C#画直线 1.winform 使用的是 Graphics private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g ...

  5. 【OpenCV 4开发详解】图像上绘制几何图形

    本文首发于"小白学视觉"微信公众号,欢迎关注公众号 本文作者为小白,版权归人民邮电出版社发行所有,禁止转载,侵权必究! 经过几个月的努力,小白终于完成了市面上第一本OpenCV 4 ...

  6. 【Android开发】图形图像处理技术-绘制几何图形

    常见的几何图形包括点.线.弧.矩形等.在Android中,Canvas类提供了丰富的绘制几何图形的方法,通过这些方法,可以绘制出各种几何图形.常用的几何图形的绘制方法如下所示: 1. 画一个圆使用的是 ...

  7. html5 绘制图形,HTML5绘制几何图形

    绘制几何图形 var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext(" ...

  8. 通过path绘制点击区域

    通过path绘制点击区域 效果 源码 https://github.com/YouXianMing/Animations // // TapDrawImageView.h // TapDrawImag ...

  9. WPF使用Canvas绘制可变矩形

    WPF使用Canvas绘制可变矩形 原文:WPF使用Canvas绘制可变矩形 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/WANGYAN9110/ ...

最新文章

  1. 分布式存储系统的关键技术-存储层级内的优化技术
  2. php ios视频文件上传,iOS实现视频和图片的上传思路
  3. 实践 HTML5 的 CSS3 Media Queries
  4. pandas实现分类汇总,查找不重复的一 一对应数据
  5. 使用strace查看后台程序stdout输出
  6. 如何把自己的苹果手机屏幕投射到电脑上
  7. 英特尔重入代工行业的底气和挑战,台积电,三星有点慌。
  8. 【154期】面试官问:请你说说 B 树、B+ 树的原理及区别?
  9. sftp常用命令介绍
  10. 公司年会到底参不参加
  11. CHOPS 音乐驱动动画
  12. 关于抽象类说法以下哪些是正确的?
  13. 普通人可以做的3个靠谱副业,副业成刚需,上班不易
  14. 原声微信小程序自定义顶部导航栏 . 自定义navigationBar。 (单页面设置)
  15. 明略数据吴明辉探案记,破解行业AI落地迷局
  16. Golang 基础之基础语法梳理 (一)
  17. 六款顶级桌面美化软件推荐(Windows)
  18. 低功耗蓝牙模块在智能水表中的应用
  19. 超级法线凹凸生成软件
  20. 通过ES文件管理器用手机访问共享。

热门文章

  1. python turtle笛卡尔心形线_一个浪漫又悲情的爱情故事——笛卡尔心形线
  2. 如果编程界推行中文标准的话
  3. 计算机启动时桌面没显示器,电脑开机显示器不亮,教您电脑显示器不亮怎么解决...
  4. rosdep update 的解决
  5. 数据库是个什么鬼?常见的数据库特点及其类别
  6. arduino uno r3单片机封装图_【arduino】arduino ISP下载程序方法,用arduino uno给arduino nano下载程序...
  7. UITableView是不会响应touchesBegan:方法的
  8. springboot+基于vue的响应式代购商城APP的设计与实现 毕业设计-附源码191654
  9. 浏览器的 ActiveX 设置
  10. 国际交流学术英文写作hnu(仅供参考)