EDN Admin
Well-known member
I need to draw a bitmap background with one or more moving bitmaps in the foreground; something like a chessboard where pieces move across it. I use a timer-tick event to redraw the background and then draw the relocated foreground image like the code below.
But I get flicker, even though I set the form to use double-buffering. The culprit seems to be redrawing the background to refresh where the foreground images used to be; no flicker if I replace the background DrawImage with a simple Graphics.Clear. I think
what I want to do is draw the background, and the foreground bitmap(s), in memory and then render it all. I used to do this with double-buffering, BitBlt, and .Refresh in VB6 but I havent found the equivalent in VB2010. This must be a pretty common graphics
requirement; whats the trick to it?
Dim Gfx As Graphics<br/>
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer, True)<br/>
Dim rectBrd As Rectangle use as clipping region for drawing<br/>
rectBrd = New Rectangle(panlBrd.Left, panlBrd.Top, panlBrd.Width, panlBrd.Height) for clipping region = panel control
Gfx = panlBrd.CreateGraphics associate Graphics with panel on form<br/>
Gfx.SmoothingMode = SmoothingMode.HighSpeed <br/>
Gfx.SetClip(rectBrd)
<br/>
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick<br/>
Static intPhaseCtr As Integer = 0<br/>
Gfx.Clear(Color.WhiteSmoke) test; shows that draw-board causes flicker, Clear does not<br/>
Gfx.DrawImage(bmpBoard, panlBrd.Left, panlBrd.Top, panlBrd.Width, panlBrd.Height)<br/>
Gfx.DrawImage(arybmpBalls(intPhaseCtr), panlBrd.Left + 100, panlBrd.Top + intPhaseCtr))<br/>
intPhaseCtr += 1<br/>
intPhaseCtr = intPhaseCtr Mod 25 goes 0 to 24 and repeats<br/>
End Sub
View the full article
But I get flicker, even though I set the form to use double-buffering. The culprit seems to be redrawing the background to refresh where the foreground images used to be; no flicker if I replace the background DrawImage with a simple Graphics.Clear. I think
what I want to do is draw the background, and the foreground bitmap(s), in memory and then render it all. I used to do this with double-buffering, BitBlt, and .Refresh in VB6 but I havent found the equivalent in VB2010. This must be a pretty common graphics
requirement; whats the trick to it?
Dim Gfx As Graphics<br/>
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer, True)<br/>
Dim rectBrd As Rectangle use as clipping region for drawing<br/>
rectBrd = New Rectangle(panlBrd.Left, panlBrd.Top, panlBrd.Width, panlBrd.Height) for clipping region = panel control
Gfx = panlBrd.CreateGraphics associate Graphics with panel on form<br/>
Gfx.SmoothingMode = SmoothingMode.HighSpeed <br/>
Gfx.SetClip(rectBrd)
<br/>
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick<br/>
Static intPhaseCtr As Integer = 0<br/>
Gfx.Clear(Color.WhiteSmoke) test; shows that draw-board causes flicker, Clear does not<br/>
Gfx.DrawImage(bmpBoard, panlBrd.Left, panlBrd.Top, panlBrd.Width, panlBrd.Height)<br/>
Gfx.DrawImage(arybmpBalls(intPhaseCtr), panlBrd.Left + 100, panlBrd.Top + intPhaseCtr))<br/>
intPhaseCtr += 1<br/>
intPhaseCtr = intPhaseCtr Mod 25 goes 0 to 24 and repeats<br/>
End Sub
View the full article