views:

195

answers:

1

I need to constantly display large amounts of colored text in WPF or Silverlight, and I need the user to percieve it as showing up instantly.

Currently I'm doing this using a canvas and textblocks, one textblock per 80-character line, with multiple runs to a textblock to handle the different colors. There can often be 20+ runs to a line.

In a 45x80 grid of text, this gets a framerate of about 12 in Silverlight, which is sluggish. Oddly enough, it's even slower in WPF... I can only speculate that this has something to do with my dev machine's lousy video card. Maybe it's trying to hardware accelerate and actually slowing down because of it?

So, my question is: is there a better way to do this?

+2  A: 

The best way to do this is to pre-render the text to a bitmap, cache the bitmap, and then blit it to the display. The framerate will go way up.

codekaizen
Thanks, could you point me in the right direction on how to get started with that?
Brian MacKay
It will probably take some expirimentation on your part to figure out what the best caching strategy is, and this will be quite complex if you have a lot of combinations of text to display, but the basic principle is to create a FormattedText object and then render it to a RenderTargetBitmap and store the bitmap to display later. To do this last step, there are two basic approaches: use a Visual to contain the bitmap and just add the Visual to the visual tree via a container (like Canvas) or else use WritableBitmap to blit the cached bitmap to directly. The latter approach will be faster.
codekaizen
That should give me enough to get started, thanks!
Brian MacKay
+1 WriteableBitmap is good way to go here. And as for caching, I wouldn't worry about it, at least from start.
Anvaka
@Anvaka - but if he doesn't hold on to the bitmap that he renders, he'll have to rerender the text, giving up the performance benefit of not having to render it every time.
codekaizen
I assume that by caching you guys are just talking about holding a reference to the bitmap in memory, right?
Brian MacKay
What I don't know about graphics could (and does) fill a small library.
Brian MacKay
@Brian - Holding onto the bitmap by a handle, yep, that's what I'm talking about by 'caching'.
codekaizen
@Brian - I've got that small library, and it turns out that once you get it, there becomes evident an even larger library of ignorance about graphics.
codekaizen