2017-11-06 13 views
0

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; 
} 
+0

ポインタのカップル: 'MouseMove'は**非常に**しばしば、不必要な詳細を削除するには同様のポイントのカップルをフィルタリング考える呼び出されます。ポイントを変更するたびに、ポリラインがジオメトリ全体を再計算します。代わりにポリラインをWriteableBitmapに描画します(または[WriteableBitmapEx](https://github.com/teichgraf/WriteableBitmapEx/))。簡単な図形を描く)。 –

+0

あなたは私にそれのためのサンプルコードを教えてください。 –

+0

2つのオプションのうちのどれですか?後で完全なポリラインが必要ですか?あなたはそれで何をやっていますか。あなたが実際にやろうとしていることを知るためには、さらに詳細が必要です。 –

答えて

1

あなたができることは、互いに非常に近いものを捨ててポイントの数を減らすことです。下の例では、前のの10ピクセル以内にある点をすべて削除します。ポイントです。

private Brush _color = new SolidColorBrush(Colors.Red); 
private double _thickness = 4.0; 
private int _previousSignificantPoint = 0; 
private Polyline _freeLine; 

private void canvas_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
    // Capture mouse 
    canvas.CaptureMouse(); 
    Point startPoint = e.GetPosition(canvas); 
    _freeLine = new Polyline 
    { 
     StrokeDashCap = PenLineCap.Square, 
     StrokeLineJoin = PenLineJoin.Round, 
     Stroke = _color, 
     StrokeThickness = _thickness 
    }; 

    // Add first point 
    _freeLine.Points.Add(startPoint); 

    // make it the first "significant" point 
    _previousSignificantPoint = 0; 

    canvas.Children.Add(_freeLine); 
} 

private void canvas_MouseMove(object sender, MouseEventArgs e) 
{ 
    // Make sure the mouse is pressed and we have a polyline to work with 
    if (_freeLine == null) return; 
    if (e.LeftButton != MouseButtonState.Pressed) return; 

    // Get previous significant point to determine distance 
    Point previousPoint = _freeLine.Points[_previousSignificantPoint]; 
    Point currentPoint = e.GetPosition(canvas); 

    // If we have a new significant point (distance > 10) remove all intermediate points 
    if (Distance(currentPoint, previousPoint) > 10) 
    { 
     for(int i = _freeLine.Points.Count - 1; i > _previousSignificantPoint; i--) 
      _freeLine.Points.RemoveAt(i); 

     // and set the new point as the latest significant point 
     _previousSignificantPoint = _freeLine.Points.Count; 
    } 

    _freeLine.Points.Add(currentPoint); 
} 

private void canvas_MouseUp(object sender, MouseButtonEventArgs e) 
{ 
    // release mouse capture 
    canvas.ReleaseMouseCapture(); 
    _freeLine = null; 
} 

private static double Distance(Point pointA, Point pointB) 
{ 
    return Math.Sqrt(Math.Pow(pointA.X - pointB.X, 2) + Math.Pow(pointA.Y - pointB.Y, 2)); 
} 

結果:

Example result of above code

+0

ありがとうございます。 –

関連する問題