如何提高Canvas的渲染性能? [英] How to improve Canvas rendering performance?

查看:579
本文介绍了如何提高Canvas的渲染性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绘制很多 Shape (约1个/ 2十万)作为[画布] [2]的孩子。我在我的WPF应用程序中将工作划分为两部分:第一件事,我通过设置其中的每个属性(例如Margin,Fill,Width等等),在我添加形状作为Canvas的孩子后,创建形状。 p>

I have to draw a lot of Shape (about 1/2 hundred thousand) as [Canvas][2]'s childrens. I make this in my WPF application dividing work in two parts: first thing I create shapes by setting the properties of each of them (like Margin, Fill, Width, etc...), after I add shapes as Canvas's children.

MyCanvas.Children.Add(MyShape)

现在我想提高第二部分的性能,因为当我绘制形状我的应用程序被阻塞很长一段时间。所以我试图使用调度程序及其方法[BeginInvoke] [ 4]与不同的[优先级] [5]:只有如果我使用的背景优先级的主要应用程序不阻止,否则应用程序保持阻止和图片不显示,直到所有的形状都添加到我的画布,但如果我使用Background优先级显然一切都更慢。我也尝试创建一个新的线程,而不是使用Dispatcher,但没有重大的变化。

Now i want to improve the performance of the second part, because when i draw the shapes my application is blocked for a long period of time. So i tried to use the Dispatcher and its method [BeginInvoke][4] with different [priorities][5]: only if I use the Background priority the main application does not block, otherwise the application remains blocked and the "picture" is not displayed until all shapes are added to my Canvas, but if I use the Background priority obviously everything is slower. I also tried to create a new thread instead of using the Dispatcher, but there was no significant change.

如何解决这个问题,一般来说提高我的性能应用程序当我将形状添加到Canvas?

How can I fix this problem, and generally improve the performance of my application when I add my shapes to Canvas?

谢谢。

推荐答案

需要使用视觉对象,而不是< a href =http://msdn.microsoft.com/en-us/library/system.windows.shapes.shape.aspx =nofollow>形状;特别是,如所建议的, DrawingVisual :a可用于渲染矢量图形的视觉对象。事实上,如在MSDN库中写的:

Need to use Visual objects instead of Shape; in particular, as suggested, DrawingVisual: a visual object that can be used to render vector graphics. In fact, as written in the MSDN library:


DrawingVisual是一个轻量级绘图类,用于渲染形状,图像或文本。此类被认为是轻量级的,因为它不提供布局,输入,焦点或事件处理,从而提高其性能。因此,图纸非常适合背景和剪贴画。

DrawingVisual is a lightweight drawing class that is used to render shapes, images, or text. This class is considered lightweight because it does not provide layout, input, focus, or event handling, which improves its performance. For this reason, drawings are ideal for backgrounds and clip art.

因此,例如,要创建一个包含矩形的DrawingVisual:

So, for example, to create a DrawingVisual that contains a rectangle:

private DrawingVisual CreateDrawingVisualRectangle()
{
   DrawingVisual drawingVisual = new DrawingVisual();

   // Retrieve the DrawingContext in order to create new drawing content.
   DrawingContext drawingContext = drawingVisual.RenderOpen();

   // Create a rectangle and draw it in the DrawingContext.
   Rect rect = new Rect(new System.Windows.Point(160, 100), new System.Windows.Size(320, 80));
   drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect);

   // Persist the drawing content.
   drawingContext.Close();

   return drawingVisual;
}

为了使用DrawingVisual对象,需要创建一个对象。主机容器对象必须从FrameworkElement类派生,它提供DrawingVisual类缺少的布局和事件处理支持。为可视对象创建主机容器对象时,需要将可视对象引用存储在 VisualCollection

In order to use DrawingVisual objects, you need to create a host container for the objects. The host container object must derive from the FrameworkElement class, which provides the layout and event handling support that the DrawingVisual class lacks. When you create a host container object for visual objects, you need to store the visual object references in a VisualCollection.

public class MyVisualHost : FrameworkElement
{
   // Create a collection of child visual objects.
   private VisualCollection _children;

   public MyVisualHost()
   {
       _children = new VisualCollection(this);
       _children.Add(CreateDrawingVisualRectangle());

       // Add the event handler for MouseLeftButtonUp.
       this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(MyVisualHost_MouseLeftButtonUp);
   }
}

然后事件处理例程可以通过调用 HitTest 方法。方法的HitTestResultCallback参数指的是用户定义的过程,您可以使用它来确定命中测试的结果操作。

The event handling routine can then implement hit testing by invoking the HitTest method. The method's HitTestResultCallback parameter refers to a user-defined procedure that you can use to determine the resulting action of a hit test.

这篇关于如何提高Canvas的渲染性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆