简体   繁体   English

用C#移动图像

[英]Move images in C#

I want to load an small image into a WinForms pictureBox control and then animate it moving to the other side of the form. 我想将一个小图像加载到WinForms pictureBox控件中,然后将其动画移动到窗体的另一侧。

I've loaded image and used a timer to move the image, but when I run it the application just shows the final position of the pictureBox and its image. 我已经加载了图像并使用了计时器来移动图像,但是当我运行它时,应用程序只显示了pictureBox及其图像的最终位置。

How I can show image smoothly transition to the final location? 如何将图像平滑过渡到最终位置?

Here is my code so far: 到目前为止,这是我的代码:

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();
        }
     }
}

Does this work? 这有用吗? Sorry, I can't test it where I am right now (on netbook without VS). 对不起,我现在无法测试它(在没有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();
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM