WPF draw line, polyline and path in realtime

Code Wanderer 396 Reputation points
2020-08-25T14:47:08.523+00:00

I want draw line represent wave audio and display it in realtime. Aslo for other audio representation as audio spectrum and other audio graphs (osciloscope etc..)

In WPF documentation is only how to draw line, polyline paths etc.. but how to draw it in realtime? If I want change point position in polyline or path, I have to recreate it completly. Isn't it performance loss?

Here is how I draw path (it is not realtime, but it is computed every time as I change a slider and it redraw the line)

    void CreateGraph()
    {
       // ...

        CanvasView.Children.Clear();
        if (pline != null) // pline = PolyLine
        {
            pline.Stroke = new SolidColorBrush(Color.FromRgb(60, 125, 200));
            pline.StrokeThickness = 1;

            pointCollection = new PointCollection();
            for (int i = 0; i < max; i++)
            {
                    // ...
                    pointCollection.Add(new Point(i * sirka, v)); // pointCollection = PointCollection
            }

            pline.Points = pointCollection;
            CanvasView.Children.Add(pline);
        }

    }

Is it this code good or there is alternative which save performance?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,760 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,321 Reputation points
    2020-08-26T08:12:59.787+00:00

    Hi,
    there'sno problem to show PolyLine in real time. Try following MVVM demo:

    XAML:

    20399-x.png

    ViewModel:

    20460-x.png

    See attached text.20445-x.txt


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.