如何在WPF中的线或路径上将图像用作ImageBrush [英] How to Use an image as ImageBrush on a Line or Path in WPF
问题描述
我有一个Canvas,可在其上绘制线(通过线"或路径")来指示用户要遵循的道路.有没有一种方法可以更改线条,以使其显示重复的图像?我正在寻找的是具有一个台阶图标(透明的png)而不是一条直线.
I have a Canvas on which I draw lines (via Lines or Paths) that indicates a road to follow for users. Is there a way to change the line so that it displays a repetitive image instead? What I'm looking for is to have a step icon (png with transparency) instead of a straight line.
谢谢!
推荐答案
这将为Path
绘制重复的图像作为描边.尝试使用Viewport
的值来实现您想要的
This will draw a repeated image as Stroke for a Path
. Laborate with the values of Viewport
to achieve what you're looking for
<Path StrokeThickness="10" Data="M 10,10 100,10" Stretch="Fill">
<Path.Stroke>
<ImageBrush ImageSource="C:\arrow.gif" Viewport="0,0,0.1,1" TileMode="Tile"/>
</Path.Stroke>
</Path>
更新
要旋转ImageBrush,您可以向其添加RotateTransform
To rotate the ImageBrush you can add a RotateTransform to it
<ImageBrush ImageSource="C:\arrow.gif" Viewport="0,0,1,0.1" TileMode="Tile">
<ImageBrush.Transform>
<RotateTransform Angle="90"/>
</ImageBrush.Transform>
</ImageBrush>
If you want an Image that follow a Path, you can use a similar approach as the answer to this question. I modified it a bit to use an Image instead
<Window.Resources>
<PathGeometry x:Key="Path" x:Shared="False" Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100"/>
<Image x:Key="followPathImage" x:Shared="False" x:Name="Arrow1" Source="C:\arrow.gif" Width="16" Height="16">
<Image.RenderTransform>
<TransformGroup>
<TranslateTransform X="-8" Y="-8"/>
<MatrixTransform>
<MatrixTransform.Matrix>
<Matrix/>
</MatrixTransform.Matrix>
</MatrixTransform>
</TransformGroup>
</Image.RenderTransform>
<Image.Triggers>
<EventTrigger RoutedEvent="Image.Loaded">
<BeginStoryboard>
<Storyboard>
<MatrixAnimationUsingPath Storyboard.Target="{Binding RelativeSource={RelativeSource AncestorType={x:Type Image}}}"
Storyboard.TargetProperty="RenderTransform.Children[1].Matrix"
DoesRotateWithTangent="True"
Duration="0:0:5"
BeginTime="0:0:0"
RepeatBehavior="Forever"
PathGeometry="{StaticResource Path}" >
</MatrixAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
</Window.Resources>
<Canvas Width="400" Height="400" Name="canvas">
<Path Data="{StaticResource Path}" Opacity="0" Stroke="Blue" StrokeThickness="1"/>
<Button Canvas.Left="184" Canvas.Top="253" Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
</Canvas>
后面还有一些代码来添加动画箭头
And some code behind to add the animated arrows
public MainWindow()
{
InitializeComponent();
var addAnimationThread = new Thread(new ThreadStart(() =>
{
for (int i = 0; i < 25; i++)
{
Dispatcher.Invoke(new Action(() =>
{
Image image = this.FindResource("followPathImage") as Image;
canvas.Children.Add(image);
}));
Thread.Sleep(199);
}
}));
addAnimationThread.Start();
}
这篇关于如何在WPF中的线或路径上将图像用作ImageBrush的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!