2017-08-02 58 views
-1

我正在C#上編程Visual Studio 2016,並且正在製作遊戲。我需要重複一個pictureBox。它應該從屏幕的右側開始,然後移動到屏幕的左側。如何在屏幕上重複圖片框來模擬動畫?

我該怎麼做?目前,我有這樣的代碼:

while (pictureBox4.Location.X == -10 && pictureBox4.Location.Y == -2) 
{ 
    pictureBox4.Location = new Point(pictureBox4.Location.X - x, pictureBox4.Location.Y - y); 
} 

X & y是隨機變量。

+0

我很驚訝,當看到你在Visual Studio 2016編程)您是指2015年或2017? –

+0

這是VS 2017,對不起 –

+0

'while(pictureBox4.Location.X == -10' should not that'> ='?? if'while'while while'。慷慨和add和'else'子句!當然,沒有告訴我們代碼從哪裏來的都是猜測......! – TaW

回答

0

如果我正確地理解你的問題(其中,如果我沒有請告訴我),你應該試試這個:

public static class PictureBoxExtension 
{ 
    public static void SetImage(this PictureBox pic, Image image, int repeatX) 
    { 
     Bitmap bm = new Bitmap(image.Width * repeatX, image.Height); 
     Graphics gp = Graphics.FromImage(bm); 
     for (int x = 0; x <= bm.Width - image.Width; x += image.Width) 
     { 
      gp.DrawImage(image, new Point(x, 0)); 
     } 
     pic.Image = bm; 
    } 
} 
+3

你在這裏泄漏了'Bitmap'和'Graphics'對象。'Graphics'對象的創建應該包含在'using'語句中以防止那麼,舊的'pic.Image'應該被明確地處置掉 –

+0

感謝您告訴我我會修復這個問題 –

+0

對不起,夥計它在我的程序中不起作用我需要做的是,picturebox重新開始屏幕右側到達屏幕左側時對不起我的英語 –