在 WPF 程序中使用矢量图

我们可以在 WPF 程序中全面地使用矢量图,除了少量特别精细的大图之外,软件内的图标等都可以使用由 Geometry 描述的矢量图。

DrawingImage

DrawingImage 继承自 ImageSource,所以 DrawingImage 可以作为 Image 的 Source,DrawingImage 对象有一个 Drawing 类型的属性 Drawing。

 

Drawing

Drawing 是描述 2D 绘图的抽象类,系统提供的派生类包括:

  • System.Windows.Media.DrawingGroup
  • System.Windows.Media.GeometryDrawing
  • System.Windows.Media.GlyphRunDrawing
  • System.Windows.Media.ImageDrawing
  • System.Windows.Media.VideoDrawing

其中 DrawingGroup 和 GeometryDrawing 比较常用。

DrawingGroup 作为 DrawingImage 的 Drawing

下面的示例中,使用了三个 LineGeometry 和一个 RectangleGeometry,使用了两种不同的颜色。注意,针对 LineGeomtry,一定要设置 GeometryDrawing 的 Pen,否则无法呈现出来。

<DrawingImage>
    <DrawingImage.Drawing>
        <DrawingGroup>
            <GeometryDrawing>
                <GeometryDrawing.Pen>
                    <Pen Thickness="2" Brush="Teal"/>
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                    <LineGeometry StartPoint="0,3" EndPoint="15,3"/>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
            <GeometryDrawing>
                <GeometryDrawing.Pen>
                    <Pen Thickness="2" Brush="Teal"/>
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                    <LineGeometry StartPoint="0,6" EndPoint="15,6"/>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
            <GeometryDrawing>
                <GeometryDrawing.Pen>
                    <Pen Thickness="2" Brush="Teal"/>
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                    <LineGeometry StartPoint="0,9" EndPoint="15,9"/>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
            <GeometryDrawing Brush="Crimson">
                <GeometryDrawing.Geometry>
                    <RectangleGeometry Rect="17,2 22,8"></RectangleGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

 

GeometryDrawing 作为 DrawingImage 的 Drawing

 下面使用到 PathGeometry,然后该 PathGeometry 作为资源被 GeometryDrawing 使用。

<!--下载-->
<PathGeometry x:Key="DownloadIconGeometry" Figures="M40.909,0 V44.444 H27.272 L56.818,77.777 86.363,44.444 H72.727 V0 Z M9.090,66.666 V100 H109.090 V66.666 H100.000 V88.888 H18.181 V66.666 Z"/>

<DrawingImage x:Key="DownloadImage">
    <DrawingImage.Drawing>
        <GeometryDrawing Brush="{DynamicResource IconAccentBrush}" Geometry="{DynamicResource DownloadIconGeometry}"/>
    </DrawingImage.Drawing>
</DrawingImage>

而 GeometryDrawing 的形状用 Geometry 来描述,那么 Geometry 又是什么呢?

Geometry 的派生类

  • System.Windows.Media.CombinedGeometry
  • System.Windows.Media.EllipseGeometry
  • System.Windows.Media.GeometryGroup
  • System.Windows.Media.LineGeometry
  • System.Windows.Media.PathGeometry
  • System.Windows.Media.RectangleGeometry
  • System.Windows.Media.StreamGeometry

转载于:https://www.cnblogs.com/fujinxiang/p/GeometryImage.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值