I'm trying to create a smal paint component( similar to MSpaint but not the same thing) in vb.net.
I have a panel that holds the background image and a picturebox over the panel that I draw on. I'm using graphics to draw shapes and I have my own custom double buffer, meaning that I draw on the picturebox while the mouse ismoving and on mouse up I draw the final image on the buffer image and on paint event the buffer image replaces the picturebox image.

also, i have set the form properties to enable double buffering

Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.UpdateStyles()


my problem is that no matter what i've tried the picture box keeps flickering. as long as the mouse is moving, the paint event keeps getting called and the image flickers like crazy

what i need is more ideas(or pseudocode) on how to avoid flickering.

thanks

Edited 6 Years Ago by Mariandi: n/a

Guessing if you already tryed to suspend the picturebox layout (SuspendLayout method) while drawing and resume the layout (ResumeLayout method) when you are ready to paint.

Not sure if this helps.

Edited 6 Years Ago by lolafuertes: n/a

yeah it doesn't really help because if i use it in the mousemove event it won't do nothing and if i suspendlayout in the mousedown event and resumelayout in the mouseup event it won't draw the growning shape. it will just give me the final shape

Hi Mariandi. I've recently had similar issues painting over a form with a gradient fill background. I found this link very helpful: http://www.bobpowell.net/doublebuffer.htm. I think the trick is often to stop forms and controls painting when you don't want them to. Perhaps create you own picturebox that only paints when you decide it should paint?? Something like:

Public Class PicBox
    Inherits PictureBox

    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
        'Rem out mybase.paint
        'MyBase.OnPaint(pe)
    End Sub
    Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseMove(e)
        'If left mouse button is down then
        'write code to paint as you wish
        'perhaps only a rect around the mouse position?
    End Sub
    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)
        'Handle painting here also
    End Sub
End Class
This article has been dead for over six months. Start a new discussion instead.