WPF faster DrawingVisuals initialization.
-
Hello, We are planning to migrate to WPF. For a few use cases, drawingspeed is an issue. Drawing 10'000 mostly static lines on a zoomable/panable canvas is about as much as I need. Using DrawingVisuals, the performance is not acceptable, because the initialization takes around 10 seconds (which is ridiculous). After the initalization, the paning/zooming works fine. Please have a look at my sample code and tell me which part is terribly wrong...
Pen pen = new Pen(Brushes.Red, 5); Random random = new Random(); DrawingVisual visual = new DrawingVisual(); using (DrawingContext dc = visual.RenderOpen()) { for (int i = 0; i < 10000; i++) { Point p1 = new Point(random.Next(0, 5000), random.Next(0, 5000)); Point p2 = new Point(random.Next(0, 100), random.Next(0, 100)); dc.DrawLine(pen, p1, new Point(p1.X - p2.X, p1.Y - p2.Y)); } dc.Close(); } MyCanvas.AddVisual(visual);
where MyCanvas is an overload of Canvas
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;namespace WpfApplication1
{
public class DrawingCanvas : Canvas
{
private List _visuals = new List();protected override int VisualChildrenCount { get { return \_visuals.Count; } } protected override Visual GetVisualChild(int index) { return \_visuals\[index\]; } public void AddVisual(Visual visual) { \_visuals.Add(visual); base.AddVisualChild(visual); base.AddLogicalChild(visual); } }
}
thanks a lot. regards. mike.
-
Hello, We are planning to migrate to WPF. For a few use cases, drawingspeed is an issue. Drawing 10'000 mostly static lines on a zoomable/panable canvas is about as much as I need. Using DrawingVisuals, the performance is not acceptable, because the initialization takes around 10 seconds (which is ridiculous). After the initalization, the paning/zooming works fine. Please have a look at my sample code and tell me which part is terribly wrong...
Pen pen = new Pen(Brushes.Red, 5); Random random = new Random(); DrawingVisual visual = new DrawingVisual(); using (DrawingContext dc = visual.RenderOpen()) { for (int i = 0; i < 10000; i++) { Point p1 = new Point(random.Next(0, 5000), random.Next(0, 5000)); Point p2 = new Point(random.Next(0, 100), random.Next(0, 100)); dc.DrawLine(pen, p1, new Point(p1.X - p2.X, p1.Y - p2.Y)); } dc.Close(); } MyCanvas.AddVisual(visual);
where MyCanvas is an overload of Canvas
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;namespace WpfApplication1
{
public class DrawingCanvas : Canvas
{
private List _visuals = new List();protected override int VisualChildrenCount { get { return \_visuals.Count; } } protected override Visual GetVisualChild(int index) { return \_visuals\[index\]; } public void AddVisual(Visual visual) { \_visuals.Add(visual); base.AddVisualChild(visual); base.AddLogicalChild(visual); } }
}
thanks a lot. regards. mike.
This is an old discussion but might be helpful for you: drawingcontext very slow many geometries
-
This is an old discussion but might be helpful for you: drawingcontext very slow many geometries