前面我们提到了LinearGradientBrush可以用来画渐变填充图,那么我们同时也可以使用ImageBrush来填充图片,它的使用方法相对比较简单
下面我们先来看一段代码
<Window x:Class="WPF.SimpleGraph.PathFill.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Canvas>
<Path Stroke="DarkGray" StrokeThickness="2">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="40,40" IsClosed="True">
<LineSegment Point="40,130"></LineSegment>
<ArcSegment Point="60,150" Size="20,20" ></ArcSegment>
<LineSegment Point="200,150"></LineSegment>
<ArcSegment Point="220,130" Size="20,20"></ArcSegment>
<LineSegment Point="220,40"></LineSegment>
<ArcSegment Point="200,20" Size="20,20"></ArcSegment>
<LineSegment Point="60,20"></LineSegment>
<ArcSegment Point="40,40" Size="20,20"></ArcSegment>
</PathFigure>
</PathGeometry>
</Path.Data>
<Path.Fill>
<ImageBrush ImageSource="d:\我的文档\图片收藏\Azul.jpg" ></ImageBrush>
</Path.Fill>
</Path>
</Canvas>
</Window>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Canvas>
<Path Stroke="DarkGray" StrokeThickness="2">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="40,40" IsClosed="True">
<LineSegment Point="40,130"></LineSegment>
<ArcSegment Point="60,150" Size="20,20" ></ArcSegment>
<LineSegment Point="200,150"></LineSegment>
<ArcSegment Point="220,130" Size="20,20"></ArcSegment>
<LineSegment Point="220,40"></LineSegment>
<ArcSegment Point="200,20" Size="20,20"></ArcSegment>
<LineSegment Point="60,20"></LineSegment>
<ArcSegment Point="40,40" Size="20,20"></ArcSegment>
</PathFigure>
</PathGeometry>
</Path.Data>
<Path.Fill>
<ImageBrush ImageSource="d:\我的文档\图片收藏\Azul.jpg" ></ImageBrush>
</Path.Fill>
</Path>
</Canvas>
</Window>
执行结果:
首先我们使用Path.Data来描绘一个弧形角的矩形,使用Path.Fill来填充该矩形。
Path.Fill中使用ImageBrush来指定图片,其中ImageSource指定的是图片的地址,它可以是本地的物理地址,也可以是网络的web地址。