2011-01-28 15 views
3

小さな画像をWinFormsのpictureBoxコントロールに読み込み、それをフォームの反対側に移動してアニメーションしたいとします。C#で画像を移動する

私は画像を読み込み、画像を移動するためにタイマーを使いましたが、私がそれを実行すると、アプリケーションは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(); 
     } 
    } 
} 
+2

while(i <= 100){'ここで' i 'は定義されていますか?あなたのループは、提示されたコードに基づいて決して終了しません。 – Amy

答えて

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

はいそれは動作します...ありがとうalot yodaj007 !!!もう一つのこと:私は動画の速度を下げることができますか? –

+1

確かに、ピクセル数を減らす(+25の代わりに+15を使用する)か、間隔を増やす(10の代わりに20を使用する)。またはこれらの両方。 – quip

+0

あまりにもキープしてくれてありがとう... –