flicker problem



My objective is quite simple. I want to display a JPG image and then on it
overlay a polygon. This polygon can be moved by a user. The even is trapped
on the mouse down event of the control. I can make the polygon move but the
screen flickers because I call invalidate() on the mouse down event. I did
do offscreen buffering but there is still flashing in the screen. Let me
explain what I am doing. in the pain even handler of the control I have the
following approach. please tell me if nothing can be done or i am going
wrong somewhere

private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs
Graphics gxOff; //create an Offscreen graphics
if (m_bmpOffscreen == null) //Bitmap for doublebuffering, create if not
m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
//m_bmpOffscreen is a private member of the class
gxOff = Graphics.FromImage(m_bmpOffscreen);//Attach image
gxOff.DrawImage(m_bmpOrg, 0, 0, rectBmpOrg, GraphicsUnit.Pixel); // here
I draw my JPG image which fills the cliend area of the control
//this image does not change except when one goes to the file menu and
opens another.
gxOff.DrawPolygon(blackPen, (Point[])rose.GenerateRoseCurvePts());
//this is where I draw the polygon. This is the part that changes with each
mouse down and mouse move.
paintg.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
Rest on the mouse move
private void panel1_MouseMove(object sender, MouseEventArgs e)
if (dragging) // if the polygon is being dragged
//compute new set of point
panel1.Invalidate(); //Now this causes the paint even to be called
for everymovement of the mouse.

Where am I going wrong? Is there are a better alternative. can I control the
rate at which refreshing is taking place? the image in the background needs
not be repainted again and again but since I am drawing the polygon on it
and to move the polygon to a new position I need to do it. also how can I
control the rate at which refreshing takes place so that there is no
Thanks in anticipation



Ken Tucker [MVP]


The panel is flickering because you are invalidating the whole
panel. Only invalidate what you want redrawn.






As well as Ken's suggested fix, there is another thing
you can do that may improve the flickering.

Create a new control that derives from the build in Panel
control. In the constructor for the control, use the
SetStyle method to enable double buffering. Double
buffering makes the image draw off screen before being
copied onto the screen.

The syntax (c#) is something like this:
SetStyle(ControlStyle.DoubleBuffering, true);


Eddie de Bear

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question