DrMPS

DrMPS

  • NA
  • 1
  • 4.2k

Flicker-free drawing problem (yes,double buffering)?

Oct 5 2010 1:15 AM
So I've read plenty of forums describing the utility of double buffering.  I *believe* I am double buffering while trying to move some bitmaps around on a picturebox, but I'm still getting flicker...what do I do about this?

For example, I've got a picturebox and a VScrollBar next to it.  There are several bitmaps painted onto the picturebox in a column.  When the VScrollBar's value is changed (i.e. VScrollBar_Scroll is invoked), the column of bitmaps moves up and down--think smoothly scrolling through a list of images, that's what I'm trying to generate.  Here's example code:

   Private Sub VScrollBar_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar.Scroll

        Dim targetPictureBox As Graphics = PictureBox1.CreateGraphics
        Dim BmpElement As Bitmap
        BmpElement = New Bitmap("C:\whatever.jpg")
        targetPictureBox.FillRectangle(Brushes.Black, 0, 0, PictureBox1.Width,PictureBox1.Height)

        For z As Integer = 0 To 19
            Dim destinationPoints As Point() = {New Point(50, 50 + 100 * z - VScrollBar.Value),
                     _ New Point(125, 50 + 100 * z - VScrollBar.Value), New Point(50, 125 + 100 * z - VScrollBar.Value)}
            targetPictureBox.DrawImage(BmpElement, destinationPoints)
        Next z

    End Sub

The numbers (50+100*z, etc) are chosen to scale down the source bitmap(s) and space them out uniformly in a column; for simplicity this version would draw a column containing 20 of the same source picture ("C:\whatever.jpg"), each 50x50 pixels, spaced vertically.  When the scrollbar's value is changed, the column is redrawn in a new position.

This DOES actually do what I say it does, it just flickers a LOT when it does.  And it's not like I'm drawing sophisticated high-res figures or even a majority of the picturebox! 

Any help would be greatly, greatly appreciated!  If there is a better way of doing what I'm trying to do, I'd love to learn it.

Using VB 2010 Express, if that matters...