ImageBrush中使用ViewBox裁剪图片
当为了创建特定的效果,以不同的方式重用同一图像的多个部分时,使用ViewBox属性有时会非常有用。然而,如果事先知道只需要使用图像的一部分进行绘制,显然在喜欢的图形软件中将该部分剪裁下来更加合理。
下面示例中将内容是"ABCDE"的图片裁剪,只留下完整的"C"。效果如下:
代码如下:
<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel Orientation="Horizontal">
<Ellipse Width="200" Height="200" Stroke="Black">
<Ellipse.Fill>
<ImageBrush ImageSource="Images/comments.png"/>
</Ellipse.Fill>
</Ellipse>
<Ellipse Width="200" Height="200" Stroke="Black">
<Ellipse.Fill>
<ImageBrush ImageSource="Images/comments.png" Viewbox="0.28,0.28,0.5,0.5"/>
</Ellipse.Fill>
</Ellipse>
</StackPanel>
</Window>

浙公网安备 33010602011771号