用渐变绘制线条 [英] Drawing a line with gradient
本文介绍了用渐变绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在我的C#/ WPF应用程序中绘制线条,其中线条跟随鼠标光标移动。
我能够跟踪鼠标光标并显示蓝色路径。
如何使用渐变来创建线条?由于我不知道线的长度,我无法预设渐变的各个颜色。有没有办法我可以说逐渐改变蓝黄 - 红 - 蓝的线条颜色,然后重复?
谢谢!
Hi,
I need to draw lines in my C#/WPF application, where the lines follow the mouse cursor movement.
I am able to trace the mouse cursor and show a blue-colored path.
How can I use gradient to create the line? Since I don't know the length of the line, I can't preset the individual colors of the gradient. Is there a way I can say "Change the line color from blue-yellow-red-blue gradually,and then repeat"?
Thanks!
推荐答案
请参阅 http:// msdn.microsoft.com/en-us/library/aa970904(v=vs.110).aspx [ ^ ]。
那样的东西呢?
Something like this then?
System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
Line gradientLine;
LinearGradientBrush gradBrush = new LinearGradientBrush();
void Window_Loaded(object sender, RoutedEventArgs e)
{
gradBrush.GradientStops.Add(new GradientStop(Colors.Red, 0));
gradBrush.GradientStops.Add(new GradientStop(Colors.Orange, 0.5));
gradBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 1));
gradBrush.GradientStops.Add(new GradientStop(Colors.Green, 1.5));
gradBrush.GradientStops.Add(new GradientStop(Colors.Blue, 2));
gradBrush.GradientStops.Add(new GradientStop(Colors.Indigo, 2.5));
gradBrush.GradientStops.Add(new GradientStop(Colors.Violet, 3));
gradBrush.GradientStops.Add(new GradientStop(Colors.Purple, 3.5));
gradientLine = new Line() { X1 = 0, Y1 = 0, X2 = 100, Y2 = 100, StrokeThickness = 1, Stroke = gradBrush };
cnvCanvas.Children.Add(gradientLine);
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(0, 0, 0, 0, 1000/30);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
for (int i = 0; i < gradBrush.GradientStops.Count; ++i)
{
gradBrush.GradientStops[i].Offset = gradBrush.GradientStops[i].Offset - 0.01;
if (gradBrush.GradientStops[i].Offset < -1) gradBrush.GradientStops[i].Offset += 4;
}
}
这篇关于用渐变绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文