Improving WPF Canvas performance

2019-01-22 15:48发布

问题:

I am developing a Maps like application using WPF. I have ~10,000 PathGeometry, Shapes added to the canvas. I have added ScaleTransform and TranslateTransform for zooming and panning controls.

The problem I'm facing is, when I zoom or pan, there is slight lag. Is there a way to organize the data so that I handle only the Shapes that are visible?

Any hints on making it more efficient will be helpful and appreciated.

回答1:

I suggest you have a look at this interesting article, with the concept of Virtual Canvas: ZoomableApplication2: A Million Items. It also has a live (xbap) demo online.



回答2:

What kind f stuff are you putting on the canvas? If using pathGeometry, are you enclosing them in Path class? If so, Path has FrameworkElement in its superclass hierarchy, which is responsible for massive performance loss.

Take a look at my question here. Although it is about Shape class, but the reason of performance degradation is the same, FrameworkElement.

If you are doing so, the solution is to use PathGeometry instead, and enclose it in DrawingContext of a DrawingVisual using DrawingContext.DrawGeometry() method.

Here are some links that should help. Path Geometry

DrawingContext.DrawGeometry()

Optimizing Performance: 2D Graphics and Imaging

And draw the shapes yourself, using combination of lines, and other things provided by classes derived from Geometry class (ArcGeometry, PathGeometry etc).

This should help.



回答3:

If you want the ultimate in performance for immediate drawing in WPF, then check out WriteableBitmapEx. This is an excellent open source library, which I recently contributed to. it provides GDI-like drawing capabilities on WriteableBitmap and is compatible with Windows Phone, WPF and Silverlight. The API is simple, you get blitting, polygons, lines and simple shapes etc... You won't get datatemplates and gradient brushes however.