PDA

Click to See Complete Forum and Search --> : .NET's BUILT-IN DOUBLE BUFFERING and FASTER GRAPHICS DRAWING in GDI+


Hu Flung Dung
Mar 9th, 2003, 10:05 PM
Alright, I've seen many examples from people all over the net trying to mimic double buffering by using Image/bitmap and graphics objects! However, all of those examples are VERY slow and inefficient!!!!!

Here's the absolute BEST way to do double buffering with the GDI+:

First, add the following 3 lines into your form's constructor (the New method, where it says "put initialization code here"):

SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.UserPaint, True)

Now, draw all of your graphics in the Paint event. They will appear only after the Paint event terminates!


Finally, as I said, Image and Bitmap objects are slow. Their good for image manipulation, but definately NOT for speed. You'll be very lucky to render them at about 10 FPS (even with a fast computer), and it will take up all of your processing power and much of your RAM. If you need extra speed and more efficient use of system resources, create TextureBrush objects from your Image/Bitmap objects, and use those in your loops in place of the image and bitmap objects, or wherever you need the speed.


I havn't seen either of these tricks documented very well, and I've seen too many people make futile attempts to mimic them. I hope this helps you all out!

MrPolite
Mar 9th, 2003, 11:22 PM
did you read that in c-sharpcorner?;)

Hu Flung Dung
Mar 10th, 2003, 12:58 AM
Originally posted by MrPolite
did you read that in c-sharpcorner?;)

Umm, I've never even visited that site yet! This is all what I discovered by reading MSDN and doing some experimentation. This is mainly a response to the "back buffering" thread posted earlier.

EDIT:

Hey, that is a kewl site! I'll have to check it out more often!

Redth
Jul 25th, 2003, 03:55 PM
wow!!

i know this is an older post, but i was searching around for double buffering information, and stumbled upon this post.. i thought it was gonna be a lot harder to implement double buffering.. but if .net does it built in, alright, that's awesome!!!

just wanted to thank you 'Hu Flung Dung' for posting this... i think it should be posted somewhere more prominent as well.. like submit it to pscode or something... it was a life-saver.. i had almost given up on writing my own control since it kept flickering...

anyways, thanks again :)

Hu Flung Dung
Jul 25th, 2003, 06:44 PM
Your welcome!

I was developing a shoot'em up game using nothing but GDI+ for graphics. It's already playable and runs great, but it still needs real levels (I just have 1 test level now), a scrolling background, and a user-friendly interface. I havn't worked on it for about a month though, as I don't like working on one thing for very long. I always switch between several projects. Maybe I should go finnish it and post it on PSC!

Anyway, Mr. Polite said that technique is already mentioned on c-sharpcorner.com

wyrd
Jul 26th, 2003, 11:39 PM
I can't seem to draw the TextureBrush correctly (using Graphics.FillRectangle()). When using it, it "wraps" the image as it moves.

Can you post a snippet of your code so we can see how you're using it?

Hu Flung Dung
Jul 29th, 2003, 03:03 AM
Originally posted by wyrd
I can't seem to draw the TextureBrush correctly (using Graphics.FillRectangle()). When using it, it "wraps" the image as it moves.

Can you post a snippet of your code so we can see how you're using it?

Sorry for the late reply, been out for the past couple days.

You have to set the WarpMode property to Clamp

Also, keep in mind that it will GREATLY improve performance if the region you specify in FillRectange is the same size as the image represented by the TextureBrush object.

TextureBrush is a really cool class, which allows for easy image rotation, flipping, tiling, stretching, etc... Although MS could have provided better documentation about how to use it. (Oh, and keep in mind that you have to use the TranslateTransform method to move the image where you want. Look up on MSDN for details - this aspect is kinda weired)