【发布时间】:2018-04-18 07:40:56
【问题描述】:
在 C# WPF 中使用鼠标向下、移动和向上事件绘制折线时遇到性能问题。请看代码。我的代码工作正常。我面临的问题是,当我想在一段时间后画一条连续的线时,线的移动会变慢。我想知道您是否有更好的解决方案来更快地画线,或者是否有更好的解决方案来改进现有代码。
谢谢。
Polyline freeLine;
Point _startPoint;
public void canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(canvas);
freeLine = new Polyline();
freeLine.StrokeDashCap = PenLineCap.Square;
freeLine.Stroke = color;
freeLine.StrokeThickness = thickness;
canvas.Children.Add(freeLine);
freeLine.StrokeLineJoin = PenLineJoin.Round;
}
public void canvas_MouseMove(object sender, MouseButtonEventArgs e)
{
Point currentPoint = e.GetPosition(canvas1);
if (_startPoint != currentPoint)
{
arrowfreeLine.Points.Add(currentPoint1);
}
}
public void canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
freeLine = null;
}
【问题讨论】:
-
几个指针:
MouseMove经常非常被调用,考虑过滤掉几个相似点以删除不必要的细节。每次更改点时,Polyline 都会重新计算其整个几何图形,可能会将 Polyline 绘制到 WriteableBitmap(或 WriteableBitmapEx - 当您必须绘制简单的几何图形时,它具有出色的性能)。 -
你能给我一个示例代码吗
-
这两个选项中的哪一个?之后你需要完整的折线吗?你在用它做什么等等。需要更多细节来了解你的实际目标。
-
首先我抓住我的电脑屏幕,然后使用画布事件在图片上画线。它被添加到画布子中,并在移动鼠标时添加点到线。
-
抱歉,这并不能回答我的问题 - 请再次阅读我的上一条评论。