469,318 Members | 1,968 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,318 developers. It's quick & easy.

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
paintg)
{
Graphics gxOff; //create an Offscreen graphics
if (m_bmpOffscreen == null) //Bitmap for doublebuffering, create if not
created
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.Clear(this.BackColor);
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);
base.OnPaint(paintg);
}
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
flicker?
Thanks in anticipation
Abhishek

Jul 21 '05 #1
2 1973
Hi,

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

Ken
-------------------
"Abhishek" <ga*****@rpi.edu> wrote in message
news:uQ**************@TK2MSFTNGP11.phx.gbl...
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
paintg)
{
Graphics gxOff; //create an Offscreen graphics
if (m_bmpOffscreen == null) //Bitmap for doublebuffering, create if not
created
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.Clear(this.BackColor);
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);
base.OnPaint(paintg);
}
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
flicker?
Thanks in anticipation
Abhishek

Jul 21 '05 #2
Hi,

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);

HTH

Eddie de Bear
MCSD
-----Original Message-----
My objective is quite simple. I want to display a JPG image and then on itoverlay a polygon. This polygon can be moved by a user. The even is trappedon the mouse down event of the control. I can make the polygon move but thescreen flickers because I call invalidate() on the mouse down event. I diddo offscreen buffering but there is still flashing in the screen. Let meexplain what I am doing. in the pain even handler of the control I have thefollowing approach. please tell me if nothing can be done or i am goingwrong somewhere

private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgspaintg)
{
Graphics gxOff; //create an Offscreen graphics
if (m_bmpOffscreen == null) //Bitmap for doublebuffering, create if notcreated
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.Clear(this.BackColor);
gxOff.DrawImage(m_bmpOrg, 0, 0, rectBmpOrg, GraphicsUnit.Pixel); // hereI 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 andopens another.
gxOff.DrawPolygon(blackPen, (Point[]) rose.GenerateRoseCurvePts());//this is where I draw the polygon. This is the part that changes with eachmouse down and mouse move.
paintg.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
base.OnPaint(paintg);
}
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 calledfor everymovement of the mouse.
}
}

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

.

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michael | last post: by
4 posts views Thread by Marek Mnd | last post: by
3 posts views Thread by seamlyne | last post: by
17 posts views Thread by pigeonrandle | last post: by
4 posts views Thread by Frank Rizzo | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.