How to animate an image in C#
I'm trying to animate an image in C#. Basically, I want to take an image and connect it to another function and based on a scale of 1 to 10, make the image move up or down. By default, the image will start out at 1. I have searched and I've found ways to make shapes move up and down on the screen, but not an actual image. The image is small, say 60x60 pixels. I feel like this should be simple, but I have yet to figure it out. I was thinking of just having an image placed on the Windows form and then basically have it move up or down the y-axis of the form, but I would like it to move smoothly.
Ok, I was able to hook a button up to a timer function and get the button to move smoothly up and down the screen. The button has to keep moving for the duration of the program running. However, I'm having a hard time writing a function that stops the timer and the image(button) from moving once the image reaches a certain location. Without that, the timer continues and the image(button) moves off the screen. I've tried messing with button.Location.Y functions, but I have yet to get it work right. Can anyone please advise? Thanks. Oh yea, once the image(button) reaches Y location of 192 or 447, it should stop moving.
An example of what开发者_C百科 I have:
private void timer2_Tick(object sender, EventArgs e)
{
button2.Top = button2.Top + 1;
if (button2.Location.Y == button2.Location.Y - 192)
{
timer2.Stop();
timer3.Stop();
}
//if (timer_limit < 100)
//{
// button2.Top = button2.Top + 1;
// timer_limit++;
//}
//else
//{
// timer2.Stop();
//}
}
Several ways to do this. You could just use a PictureBox and change its Location property. Or you could draw the image in the form's OnPaint() override and change the argument to e.Graphics.DrawImage(). You'll then have to call Invalidate() to force the OnPaint method to run. It is the cheapest way.
精彩评论