View Single Post
 
Old 04-14-2004, 10:25 AM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Smile GDI+ Backbuffers in VB.NET - Projectile Demo

For a more basic tutorial of GDI+ in VB.NET, go here:
GDI+ - What's That?

Backbuffers - How to make and use them

What I will be talking about is how to use a backbuffer in your drawings for making smoother animation in .NET.
I'm sure you have used a Graphics object in order to draw to your form... it's important to note that a Graphics object does not hold the drawings that it makes... the drawings are held by whatever the Graphics object is aimed at... for example, if you draw a box with a Graphics object... the box is not held in the Graphics object....

If you were on a form and used:
GFX = Me.CreateGraphics()
The Graphics object is aimed at the form, and any drawings you make will be drawn to the display on the form.
Also:
GFX = Graphics.FromImage(Bmp)
The Graphics object is aimed at a bitmap, and drawings are made to the bitmap.
Since the bitmap is not immediately drawn to memory, you can actually do some other things with it... like save it to file... or draw it somewhere else.
In fact, this technique allows you to make a backbuffer for your image display (as I discovered last month)... a backbuffer is just an offscreen representation of what you want to draw to the screen.

In the example below, you will see the line:
GFX = Graphics.FromImage(BackBuffer)
Where BackBuffer is a bitmap object, and GFX is aimed at it... so that means we can draw on the image.

In order to get this image to the display, I'm sure you have heard of the Graphics.DrawImage() method for drawing images... and since we have drawn on the BackBuffer, if I wrote backbuffer as the image to draw, then our newly modified backbuffer image is drawn onto anything that the Graphics object can draw to (which is practically anything)...
so, to draw to a form... you would use:
GFXForm = Me.CreateGraphics()
GFXForm.DrawImage(BackBuffer)

to draw the finalized buffer to the screen.

Drawing to offscreen surface is considerably faster than drawing directly to the visible surface... and this will allow your GDI+ app to run faster than it would with a single Graphics aimed at the form.

Check out my demonstration below... which is heavily commented
(you can just add the form to one of your projects and show it to see how it works... or you can just open the solution included )
Attached Files
File Type: zip Package.zip (10.9 KB, 506 views)
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote