I have done tests. My findings aren't benchmarks using timers but rather real world performance.
First, I did a .net way of double buffering. off screen bitmap etc. results...
single sprite: 55 frames before flicker
500 sprites; 24 frames before flicker
1000 sprites: 18 fps before flicker
I added a calculation to detect THAT in fact, the bottleneck WAS indeed graphics.DrawImage(); and NOT the other routines for moving, calculating etc. It incremented every time the other routines needed to paint, but were waiting on onPaint() to do its graphics.DrawImage(); bit.
Now, painting the window using a gdi bitblt:
1000 sprites; 29 frames per second.
An interesting tidbit here, to add a bunch of sprites I simply created a sprite class, locations, image, etc and then added them to a list. Every draw routine I randomly change the drawing location to simulate movement. You know, to make sure the screen redraws. The interesting part is to be quick I just did a foreach loop on the list and did a graphics.DrawImage(x,y,h,w) for each of the up to 1000 sprites.
Point is, I may have gotten better performance out of the drawing routine by bitblt-ing all the sprites, then blitting the full thing. But I felt like just proving a point on the whole .net double buffer vs. gdi performance thing.
Ive gotten some really good performance using the standard graphics class. But I had to do a lot of calculations to basically determine exactly what regions to invalidate etc. Just to speed up the paint. But you'll still have problems when you have 500 sprites taking up the whole screen and half are updating. Which usually means most of the screen being invalidated.
I found a simpler solution for getting double buffering that was even easier to implement:
Add the following style in the forms constructor:
It seamed to me like the performance was better than I archived using the mechanisms you described in "The .NET way". I think this was caused by the fact that the Off-screen surface was in the computers memory in your example and hopefully in the display card memory as an off-screen surface. This would increase the blitting performance from back to front buffer significantly.
worked but failed
Posted by sudhir_kr
on 05/18/2006 07:23am
I added the above mentioned line in my form's constructor and it really worked fine........But when i tried to scroll(axis scroll of my graph) my graph still flickers, any help ????
I am desperetely seeking the solution....
Thanks in advance
Very good article, thank you!
For the pure .Net way, remember to call Dispose()
on your Graphics and Bitmap instances.
Simply letting the garbage collector remove them
won't be enough.
This is especially important for "temporary"
Graphics context or Bitmaps that are created in a
loop, otherwise you may notice your app behaving
erratically after a while when resources get low
(I've actually only started to notice that bug
once I ported my game to Pocket PC using the .Net
All .Net classes mapping GDI+ inherit IDisposable
and must be disposed properly says the
hi, I used this in a UserControl for a pretty high intensive gfx app (an oscillascope - yes, I know...) anyway... I was getting bizzare 'object in use elsewhere errors' until I realised that if you get a hdc using these classes you MUST place a monitor lock onto it until you're finished otherwise any old random windows-ism can frick with your device contect. FYI..
I have inconsistent results getting an image from an AxWebBrowser control.
It works as expected if it the form it is on is activated and it is not
covered by any
other form or control, but if I have another control over the top of it or
if another application
is covering it, it captures the front forms or windows. If it is half off
screen, half of the
image is missing.
Ideally, what I would like to do is to capture it in a hidden window and
show the image
in another control that can be scaled and manipulated.
I am perplexed.
Below is a code sample.
Thanks in advance for insight into this problem
Sr. Software Developer
Iteration Software, Inc.
I am trying to emulate the way windows drags a window using only the border. I have tried all kinds of stuff, one of which actually works. (Using 4 forms declared in the class and moving them around with the mouse events) But that is slow an painful to watch the flicker. Is there any way to just draw all over the screen, and not be limited to the form?