0

I'm trying to make an animation for my C# program. There's a space rocket that ascends 8 pixels vertically with a timer interval of 25ms. I've managed to make the animation but since the picturebox's background (I've used for the rocket) is set to transparent it flickers the form's background image everytime it moves. What can I do to prevent it?

Screenshot

The code I've used for timer tick :

pictureBox1.Top -= 8;

P.S: I've tried to change the picturebox with panel, slow downed the rocket and timer but nothing seemed to change.

Ümit Aparı
  • 540
  • 2
  • 6
  • 23

1 Answers1

1

Well i haven't tried it myself now. There needs to be a render event which you can hook to and make manipulations to your UI which would render smoothly.

Control.Paint

Try something like these :

private void Form1_Load(object sender, System.EventArgs e)
{  
     pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
}

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{ 
      pictureBox1.Top -= 8;
}

Again this isn't tested and i haven't developed any thing in winforms for ages. But that's the direction you should go for to render things smoothly.

That double buffered thing mentioned above is also a factor in some cases. but this is mainly the way to do it.

eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • I've tried to implement this in my code but as I mentioned in the post there's an interval of 25ms that slowers the rocket in my code. But since there's nothing to slow down the rocket in your code it flies so fast. What can I do to make it slower? – Ümit Aparı Jun 01 '17 at 21:14
  • Um, how would that help?? You should do a e.Graphics.DrawImage(image, x, y) in the Paint, not move that pbox again!! – TaW Jun 01 '17 at 21:38
  • 1
    im not a winforms developer, your probably correct. my point was manipulate the ui in the paint event – eran otzap Jun 01 '17 at 21:51
  • @Unmit: You don't show or hint at any code that makes the movement slower either! The Tick should contain code to change the data (y position..) and an Invalidate. ThePaint should draw the Rocket from some png image. – TaW Jun 01 '17 at 21:54
  • @UmitApari if it does happen too quick but indeed renders smothly. i would check the time passed between 2 intervals and move the rocket accordingaly . lets say you have a 1000 pixel to move and 25 ms passed since the last interval. then you move the rocket 40px – eran otzap Jun 01 '17 at 21:55
  • I've did it! It renders much smoother if not crystal clear. I appreciatied so much @eran otzap. – Ümit Aparı Jun 02 '17 at 01:48