用C#移动图像

时间:2011-01-28 22:59:20

标签: c# winforms animation

我想将一个小图片加载到WinForms pictureBox控件中,然后将其动画移动到表单的另一侧。

我已经加载了图像并使用了计时器来移动图像,但是当我运行它时,应用程序只显示pictureBox及其图像的最终位置。

如何将图像平滑过渡到最终位置?

到目前为止,这是我的代码:

public partial class Form1 : Form
{
    private int counter = 0;

    void timer_Tick(object sender, EventArgs e)
    {
        counter++;
        if (counter == 1)
        {
            pictureBox1.Show();
            timer1.Stop();
            counter = 0;
        }
    }

    public Form1()
    {
        InitializeComponent();

        timer1.Interval = 10;
        timer1.Tick += new EventHandler(timer_Tick);
    }

    private void button1_Click(object sender, EventArgs e)
    {

        while(i<=100){

             int x = pictureBox1.Location.X;
             int y = pictureBox1.Location.Y;

             pictureBox1.Location = new Point(x+25, y);
             timer1.Start();
        }
     }
}

1 个答案:

答案 0 :(得分:3)

这有用吗?对不起,我现在无法测试它(在没有VS的上网本上)。

public partial class Form1 : Form
{
    void timer_Tick(object sender, EventArgs e)
    {
        int x = pictureBox1.Location.X;
        int y = pictureBox1.Location.Y;

        pictureBox1.Location = new Point(x+25, y);

        if (x > this.Width)
            timer1.Stop();
    }

    public Form1()
    {
        InitializeComponent();

        timer1.Interval = 10;
        timer1.Tick += new EventHandler(timer_Tick);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.Show();
        timer1.Start();
     }
}