HowTo Avoid Flicker
-
Heya, I have the following code that stretches a control to its parent size in a given number of steps. However, the panel1.update() method causes the screen to flicker. Is there a workaround for this, so that the animation will run smooth?
public void Animate(Control Ctrl, int Steps)
{
Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
int ParentWidth = (int)Ctrl.Parent.Width;
int ParentHeight = (int)Ctrl.Parent.Height;
int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
panel1.BackgroundImage = OriginalView;
Ctrl.Visible = false;for (int i = 0; i < Steps; i++) { Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth)); panel1.BackgroundImage = NewRender; panel1.Update(); } GC.Collect(); Ctrl.Visible = true; panel1.BackgroundImage = null; panel1.Update(); }
Thanks in advance, Zaegra
Motivation is the key to software development.
-
Heya, I have the following code that stretches a control to its parent size in a given number of steps. However, the panel1.update() method causes the screen to flicker. Is there a workaround for this, so that the animation will run smooth?
public void Animate(Control Ctrl, int Steps)
{
Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
int ParentWidth = (int)Ctrl.Parent.Width;
int ParentHeight = (int)Ctrl.Parent.Height;
int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
panel1.BackgroundImage = OriginalView;
Ctrl.Visible = false;for (int i = 0; i < Steps; i++) { Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth)); panel1.BackgroundImage = NewRender; panel1.Update(); } GC.Collect(); Ctrl.Visible = true; panel1.BackgroundImage = null; panel1.Update(); }
Thanks in advance, Zaegra
Motivation is the key to software development.
-
Heya, I have the following code that stretches a control to its parent size in a given number of steps. However, the panel1.update() method causes the screen to flicker. Is there a workaround for this, so that the animation will run smooth?
public void Animate(Control Ctrl, int Steps)
{
Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
int ParentWidth = (int)Ctrl.Parent.Width;
int ParentHeight = (int)Ctrl.Parent.Height;
int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
panel1.BackgroundImage = OriginalView;
Ctrl.Visible = false;for (int i = 0; i < Steps; i++) { Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth)); panel1.BackgroundImage = NewRender; panel1.Update(); } GC.Collect(); Ctrl.Visible = true; panel1.BackgroundImage = null; panel1.Update(); }
Thanks in advance, Zaegra
Motivation is the key to software development.
What is the point of the for loop, there is no pause in between updates, thus you will get little visual effect from it. This will likely be one reason for your flickering too. I would suggest using a timer (of some sort) and use the paint event of the panel to do your drawing. That way you can calculate any resize values when you start the animation, and apply the values with each tick of the timer along with a call to panel.Invalidate();
Life goes very fast. Tomorrow, today is already yesterday.
-
Heya, I have the following code that stretches a control to its parent size in a given number of steps. However, the panel1.update() method causes the screen to flicker. Is there a workaround for this, so that the animation will run smooth?
public void Animate(Control Ctrl, int Steps)
{
Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
int ParentWidth = (int)Ctrl.Parent.Width;
int ParentHeight = (int)Ctrl.Parent.Height;
int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
panel1.BackgroundImage = OriginalView;
Ctrl.Visible = false;for (int i = 0; i < Steps; i++) { Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth)); panel1.BackgroundImage = NewRender; panel1.Update(); } GC.Collect(); Ctrl.Visible = true; panel1.BackgroundImage = null; panel1.Update(); }
Thanks in advance, Zaegra
Motivation is the key to software development.
-
Heya, I have the following code that stretches a control to its parent size in a given number of steps. However, the panel1.update() method causes the screen to flicker. Is there a workaround for this, so that the animation will run smooth?
public void Animate(Control Ctrl, int Steps)
{
Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
int ParentWidth = (int)Ctrl.Parent.Width;
int ParentHeight = (int)Ctrl.Parent.Height;
int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
panel1.BackgroundImage = OriginalView;
Ctrl.Visible = false;for (int i = 0; i < Steps; i++) { Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth)); panel1.BackgroundImage = NewRender; panel1.Update(); } GC.Collect(); Ctrl.Visible = true; panel1.BackgroundImage = null; panel1.Update(); }
Thanks in advance, Zaegra
Motivation is the key to software development.
the biggest issue I see is the
panel.BackgroundImage = null;
delete that... what you are getting by setting the null is : image no image image no image hence the flicker. all you need to do is change the image. when you do, it will throw the Paint command automatically(if memory serves if not throw it manually). What I usually do(manual double buffering)bitmap bmp0,bmp1;
control.Image = bmp0;
bmp1 = bmp0;
(modify bmp1}
bmp0 = bmp1;
control.Image = bmp0;no flicker!