Help with GDI+ Double Buffer

Denaes

Well-known member
Joined
Jun 10, 2003
Messages
956
Why does it seem that when you know how to do something, then that control is taken away for "ease of use", it just seems harder?

Im trying to do a double buffer to my picturebox. Currently Im having no problem drawing 50+ shapes to the picturebox, but I want everything to appear at once. This was pretty easy in VB6, you just used a double buffer and flipped it.

Ok, I have to do things the .Net way (or at least learn how).

I saw another post about this and I found out that .Net does it for you, if you jump through its hoops ::groan::

Ok, so I read that you needed to turn on DoubleBuffer and AllPaintingInWmPaint on, which I believe I did here:

Code:
Sub EnableDoubleBuffering()
Me.SetStyle(ControlStyles.DoubleBuffer _
Or ControlStyles.UserPaint _
Or ControlStyles.AllPaintingInWmPaint, _
True)
Me.UpdateStyles()
End Sub

I call this procedure on form load.

I read that you have to do all of your drawing in the Paint or OnPaint Methods, using e for all of your drawing.

This is my attempt:

Code:
picScreen_Paint(ByVal sender as object, ByVal e as System.Windows. Forms.PaintEventArgs) Handles picScreen.Paint
Dim g as graphics = e.graphics

dim bmap as Bitmap
bmap = new Bitmap(FilePath)
dim Rect as Rectangle
Rect = new Rectanble(0,0,16,16)

g.drawimage(bmap, rect, 0,0,16,16, GraphicsUnit.Pixel)
End Sub

This actually draws a gameblock on my picturebox (picScreen).

My main problem is that I have all of this bitmap info in a class, in which I normally pass the picturebox reference across. If the drawing has to be done from e.graphics, can you just pass the e across instead of the picturebox?
 
Yes.
2 things:
I never had to call this: Me.UpdateStyles()
and
I always paint in an overriden OnPaint event.
 
You can just pass the e.Graphics across if you need to. If you want double buffering to work your drawing needs to go in the paint event of the control/form that is double buffered.

Using a Paint event instead of OnPaint is okay.. but the SetStyles must be on the control that the paint event comes from (I believe). Do not just set double buffering on in the form and expect it to work for all the controls.

Pete
 
Back
Top