# logchan's Blog

### Draw curves in WPF efficiently

Log of rendering a lot of curves in WPF Canvas.

I have a customized canvas inheriting Canvas and overrides the OnRender(DrawingContext dc) method. And I need to draw some curves. I have precomputed the points on the curve and stored them in a List<Tuple<double, double>> because I am drawing different parts of the curve at different time. So, it is natural to come up with

However this approach is slow and CPU-intensive as hell. Guess what? I had a naive test and wrote

Basically the code constructs a Path Markup, parse it to a Geometry object and draw it. It looks terrible - that’s a lot of string operations… but it is actually very, very fast. In fact, much faster than the multiple DrawLine calls.

One possible optimization is to, apparently, precompute all the Geometry objects, although it may result in worse performance because that’s a lot of objects. So I’ll just stick to this construct-on-the-fly thing at the moment :P