WPF 使用Path绘制几何图形

33 篇文章 3 订阅

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>

运行效果如下:

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WPF(Windows Presentation Foundation)是一种用于创建用户界面的技术,而GDI(Graphics Device Interface)是一种用于绘制图形的API。在WPF中,可以使用GDI来绘制图形,通过使用GDI绘制图形,我们可以实现更加定制化和高级的图形效果。 在WPF中,可以使用GDI绘制各种类型的图形,如直线、矩形、椭圆、多边形等。通过使用GDI,我们可以设置各种样式和属性,例如线条的颜色、线宽、填充颜色等,以及阴影、渐变等效果。使用GDI绘制图形的过程是通过在WPF中创建一个GDI绘图对象,然后调用其相应的方法和属性来实现绘制。 在使用GDI绘制图形时,需要注意GDI是基于像素的,因此绘制的图形会受到屏幕分辨率的影响。在WPF中,可以使用Transform矩阵类来实现图形的缩放、平移和旋转等操作,以适应不同分辨率的屏幕。 尽管WPF本身提供了丰富的图形绘制功能,但在某些情况下,使用GDI绘制图形可能会更加灵活和高效。例如,如果需要实现一些特殊的效果,如镜像、叠加等,可以使用GDI来实现。此外,如果需要与现有的GDI代码进行交互,使用GDI绘制图形也是一种不错的选择。 总之,使用WPF和GDI结合绘制图形,可以实现更加丰富和高级的效果。通过使用GDI绘制图形,我们可以更好地控制图形的样式和属性,并且可以适应不同的分辨率和交互需求。这种组合使用可以使我们在图形绘制方面拥有更大的灵活性和创造力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚楚3107

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值