views:

596

answers:

5

I want to render a lot of circles or small graphics within either silverlight or wpf (around 1000-10000) as fast and as frequently as possible. If I have to go to DX or OGL, that's fine, but I'm wondering about doing this within either of those two frameworks first (read: it's OK if an answer is WPF-only or Silverlight-only). Also, if there is a way to access DX through WPF and render on a surface that way, I would be interested in that as well.

So, what's the fastest way to draw a load of circles? They can be as plain as necessary, but they do need to have a radius. Currently I'm using DrawingVisual and a DrawingContext.DrawEllipse() command for each circle, then rendering the visual to a RenderTargetBItmap, but it becomes very slow as the number of circles rises.

By the way, these circles move every frame, so caching isn't really an option unless you're going to suggest caching the individual circles . . . But their sizes are dynamic, so I'm not sure that's a great approach.

+3  A: 

Hi Walt,

In Silverlight 3.0+, most likely, you will use WriteableBitmap for this. In WPF it also may be a good choice.

I wrote two demo applications in Silverlight. They may be little buggy, but they demonstrate the point.

  1. Hello world application. Definitely can be optimized. Performance isn't that good, but that's because I made something stupid. I believe it has ~2 500 ellipses:

alt text

  1. Slide show application. I can't recall number of objects here, but it's way more than 10 000.

alt text

Anvaka
I'm wondering if it's okay to say "way more" in English. If it's silly I meant a lot more :).
Anvaka
way more works :) I'll look at these, though I'm looking for pretty high framerates, and there it looks like it's taking a second per frame for the 2500 ellipses.
Walt W
Thank you :). A second isn't a framerate there. It's initialization time. Framerate is pretty high. No freezing, no jumping, all smooth.
Anvaka
Alright, well I'll definitely take a look later, thanks.
Walt W
+3  A: 

Check out this article by Charles Petzold. It describes how to do pretty much exactly what you're looking for.

Bryan Anderson
This might work, though I'm a little hesitant to re-implement my circles as a datapoint scatter plot, which I would think would be optimized for static content, as opposed to the dynamic content I'm talking about.
Walt W
I think you missed the point. The article isn't really about the scatter plot, it's about common ways to speed up displaying a lot of simple items in WPF and the potential pitfalls along the way.
Bryan Anderson
I see what you mean; sorry, earlier look was pretty quick. I'll look at it more.
Walt W
Ok, this is the accepted answer because it utilizes more of the framework (which is nice; it allows for more built-in drawing possibilities) and I can get decent speeds with a lot of drawings. Frankly, the article pointed to isn't the absolute best... I'd recommend http://msdn.microsoft.com/en-us/library/ms749021.aspx , http://msdn.microsoft.com/en-us/library/bb613591%28VS.100%29.aspx , and http://www.michaelflanakin.com/Weblog/tabid/142/articleType/ArticleView/articleId/1015/WPF-Performance-Tips.aspx instead.
Walt W
+2  A: 

Check out the WriteableBitmapEx library for Silverlight which will surely work with WPF too. The circle functionality was introtuced in this blog post including a sample.

Rene Schulte
Question about this - does it support filling the shapes? I only see outlines in the demos.
Walt W
Which may not be so bad, as a note, but I was curious.
Walt W
At the moment there's no support for filling, but it's on the list and will be implemented in the near future. Although I have a lot of other projects as well. :)
Rene Schulte
+2  A: 

If you want speed do not use draw Ellipse - pre generate your circles for sizes and colours and use write bitmap and you will get full BitBlt acceleration.

This may be difficult depending on your background , transparency needs or anti aliasing but it will be orders of magnitudes faster.

Using anti aliased line drawing is slow but is ok for most thigs since it odesnt change the results can be cached and you get multiple frames with the result.

However if you want to draw as fast as possible use non transparent Bitmaps.

ben
+2  A: 

While you have accepted an answer already, if you find you need to use DirectX9 (inter-op with later versions is a bit harder but can still be done (depending on hardware)) you can use the D3DImage component. Which is detailed on CodeProject Here.

The other option is you can just use a host a WinForms control and use the hWnd of that for D3D device creation.

Courtney de Lautour