In my application I have to draw a line that contains of a lot of points (up to 100.000 if needed). Initially I used a Polyline but this turned out to be way too slow for the larger amounts of points.
I've been doing some reading up and came to DrawingVisual and StreamGeometry however its not working.
I followed a simple tutorial and created the following class:
class ConcentratieGrafiek : FrameworkElement
{
VisualCollection Visuals;
PointCollection PuntenCollectie;
public ConcentratieGrafiek(PointCollection Punten)
{
Visuals = new VisualCollection(this);
PuntenCollectie = Punten;
this.Loaded += new RoutedEventHandler(ConcentratieGrafiekLoaded);
}
void ConcentratieGrafiekLoaded(object sender, RoutedEventArgs e)
{
DrawingVisual Visual = new DrawingVisual();
StreamGeometry g = new StreamGeometry();
using (StreamGeometryContext cr = g.Open())
{
foreach (Point Punt in PuntenCollectie)
{
if (PuntenCollectie.IndexOf(Punt).Equals(0))
{
cr.BeginFigure(Punt, false, false);
}
else
{
cr.LineTo(Punt, false, false);
}
}
}
using (DrawingContext dc = Visual.RenderOpen())
{
Pen p = new Pen(Brushes.Black, 1);
dc.DrawGeometry(Brushes.Black, p, g);
}
Visuals.Add(Visual);
}
protected override Visual GetVisualChild(int index)
{
return Visuals[index];
}
protected override int VisualChildrenCount
{
get
{
return Visuals.Count;
}
}
}
All the points of the line I need are in the PointCollection called Punten. I then try to add this FrameworkElement to my Canvas using.
ActieveCanvas.Children.Add(new ConcentratieGrafiek(ConcentratiePunten) { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch });
Where "ActieveCanvas" is the Canvas.
For information: all the points are X,Y coordinates within (relative to) the Canvas.
What am I doing wrong?