WPF 性能:在画布上显示数千个路径/形状

发布于 2024-08-05 21:42:04 字数 682 浏览 8 评论 0原文

我目前正在开发一个可视化工具,可以在画布上绘制 WPF 形状,例如路径、椭圆等。我已经实现了一种虚拟化方法,其中根据形状的可见性动态地销毁和创建形状。然而,即使只有 600 个可见的省略号,该应用程序似乎也很困难。

我有什么选择可以加快速度?我正在考虑将分组的形状(假设一次 500 个)渲染为透明位图,并且仅将它们绘制在画布上。但我不知道这是否是一个好主意...据我所知,如果应用转换,这需要某种黑客:

     VisualBrush shapeBrush = new VisualBrush(shape);  

     DrawingVisual drawingVisual = new DrawingVisual();  
     DrawingContext drawingContext = drawingVisual.RenderOpen();  

     using (drawingContext)  
     {  
        drawingContext.DrawRectangle(shapeBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));  
     }  
     renderTarget.Render(drawingVisual);  

使用大的 WritableBitmap 怎么样?这是另一种方法吗?

I'm currently developing a visualization tool that draws WPF shapes such as paths, ellipses etc. on a Canvas. I've already implemented a virtualized approach where Shapes are being destroyed and created on the fly depending on their visibility. However, even with only like 600 ellipses visible, the application seems to struggle.

What are my options to speed things up? I'm thinking rendering grouped Shapes (let's say 500 at a time) as transparent bitmaps and only painting these on the Canvas. But I don't know whether that's a good idea... From what I gather this requires some sort of hack, if transformations were applied:

     VisualBrush shapeBrush = new VisualBrush(shape);  

     DrawingVisual drawingVisual = new DrawingVisual();  
     DrawingContext drawingContext = drawingVisual.RenderOpen();  

     using (drawingContext)  
     {  
        drawingContext.DrawRectangle(shapeBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));  
     }  
     renderTarget.Render(drawingVisual);  

What about using a big WritableBitmap? Would that be another approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷鸟归林 2024-08-12 21:42:04

WPF 在幕后可以处理绘图和几何图形 - 当您说您正在处理形状时,这些是实际的 UIElements 吗?这样的元素相当重量级。如果您仅使用基本(最好是流)几何图形来绘制绘图,根据我的经验,您将获得最佳性能。

通过这种方法,我成功地以合理的帧速率获得了大约 10000 个点,但任何比点更复杂的东西都会开始减慢速度(例如,圆点甚至只是矩形)。尽管如此,如果您想尽可能避免 WPF 开销,基本几何图形和基本绘图仍然是不错的选择。

可写位图显然最终更快,但这意味着您自己渲染所有这些形状,或者缓存结果位图(如果它大部分是静态的)。此外,您通常希望在渲染位图之前应用转换,而不是将它们应用到渲染的位图本身。

WPF under the covers works with drawings and geometries - when you say you're working with shapes, are these actual UIElements? Such elements are quite a bit more heavy-weight. If you use just basic (preferably stream-) geometries to draw drawings, you'll get the best performance in my experience.

I managed to get up to around 10000 dots with a reasonable framerate with that approach, but anything more complex than a dot starts slowing things down (say, round dots or even just rectangles). Still, basic geometries and basic drawings are the way to go if you want to avoid as much WPF overhead as possible.

A Writeable bitmap is clearly eventually faster, but that means rending all those shapes yourself, or, caching the result bitmap if it's mostly static. Also, you'll generally want to apply transformations before rendering to bitmap rather than applying them to the rendered bitmap itself.

土豪 2024-08-12 21:42:04

我知道这是一个老问题,我只是为了社区的利益而回答。

我对这个主题进行了一些研究,我发现最好的方法是像你说的那样手动创建 DrawingVisuals。它节省了 WPF 的大量内部工作,因此速度更快。我使用该技术创建了一个可以包含数百个点的轻量级图表。这是我启发自己的文章,您可能已经知道了。

http://blogs.microsoft.co.il/blogs/tamir/archive/2008/03/02/how-to-high-performance-graphics-in-wpf.aspx

编辑:新 URL < a href="http://khason.net/blog/how-to-high-performance-graphics-in-wpf/" rel="nofollow noreferrer">http://khason.net/blog/how-to- wpf 中的高性能图形/
编辑:较新的网址: http:// /dedjo.blogspot.com/2008/03/how-to-high-performance-graphics-in-wpf.html

祝你好运。

I'm aware this is an old question, I'm just answering in the interest of the community.

I researched the topic a bit, and the best I've found is to manually create DrawingVisuals like you say. It saves a lot of internal work to WPF, so it ends up being a lot faster. I used the technique to create a lightweight chart that can have couple hundred points. Here's the article I inspired myself from, you might already know about it.

http://blogs.microsoft.co.il/blogs/tamir/archive/2008/03/02/how-to-high-performance-graphics-in-wpf.aspx

EDIT: New URL http://khason.net/blog/how-to-high-performance-graphics-in-wpf/
EDIT: Newer URL: http://dedjo.blogspot.com/2008/03/how-to-high-performance-graphics-in-wpf.html

Good luck.

夏见 2024-08-12 21:42:04

一种强力方法可能是实现 ActiveX 控件并直接使用 Win32 渲染图形。然而,这会有些繁琐。 QT 的画布控件可能是达到相同目的的更温暖和蓬松的方法,并且它以相当快地渲染此类事物而闻名。 Troll 为商业版本的 QT 提供了 ActiveX 包装器,因此它可能更容易集成。

A brute-force approach might be to implement an ActiveX control and render the graphics directly using Win32. However, this will be somewhat fiddly. QT's canvas control might be a more warm and fluffy approach to the same end and it's noted for rendering this type of thing fairly quickly. Troll provide an ActiveX wrapper for the commercial versions of QT so it might be easier to integate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文