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?

Developer technologies | Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,341 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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.