Hello, I'm having a problem with animating a bitmap on my form.
There is a massive flickering when animating the bitmap.
Is there anything I can do about this?

Bitmap bmp = new Bitmap(@"1.png");
aTimer.Interval =1;
aTimer.Tick += new EventHandler(t_Tick);
aTimer.Start();

public void t_Tick(object sender, EventArgs e){
          gfx.DrawImage(bmp,new Point(x,y+1));
           y = y+1;
           Invalidate();
}

Thank you for your time.

I think you can use double buffering - you will need to Google it, its been a while since I did animation.

by adding:

 this.DoubleBuffered = true;
this.SetStyle(ControlStyles.UserPaint | 
              ControlStyles.AllPaintingInWmPaint |
              ControlStyles.ResizeRedraw |
              ControlStyles.ContainerControl |
              ControlStyles.OptimizedDoubleBuffer |
              ControlStyles.SupportsTransparentBackColor
              , true);

In the constructor it now animates without flickering when I create the image for the second time, it still flickers the first time I click.

Complete setup code:

 private void BindControlMouseClicks(Control con)
        {
            con.MouseClick += delegate(object sender, MouseEventArgs e)
            {
                TriggerMouseClicked(sender, e);


                if (MousePosition.X > 0 && MousePosition.X < cWidth){
                    x = cWidth/2;
                    label1.Text ="1ste kollom";
                }
                if(MousePosition.X > cWidth +1 && MousePosition.X < cWidth*2){
                    x = cWidth/2 + cWidth;
                   label1.Text ="1ste kollom";
                }
              y = MousePosition.Y;
                //Timer
              aTimer.Interval =speed;
              aTimer.Tick += new EventHandler(t_Tick);
              aTimer.Start();

                bTimer.Interval =1;
              bTimer.Tick += new EventHandler(t_Tick2);
              bTimer.Start();

              gfx.DrawImage(bmp,new Point(x,y));

            };
            // bind to controls already added
            foreach (Control i in con.Controls)
            {
                BindControlMouseClicks(i);
            }
            // bind to controls added in the future
            con.ControlAdded += delegate(object sender, ControlEventArgs e)
            {
                BindControlMouseClicks(e.Control);
            };            
        }

Edited 1 Year Ago by Tigr@: not enough info

Well it's not working 100% since when clicking for the first time it still flickers. Also i've noticed the my image accelerates each time I click.

Acceleration may be due to starting a new timer each time you click -
n timers = n times as many events per second (?)

Edited 1 Year Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.