Improving WPF Canvas performance

自古美人都是妖i 提交于 2019-11-30 06:38:31

问题


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.



来源:https://stackoverflow.com/questions/7560922/improving-wpf-canvas-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!