-1

I'm programing in C# on Visual Studio 2016, and I'm making a game. I need to repeat a pictureBox. It should begin on the right side of the screen, and move to the left side of the screen.

How can I do this? I currently have this code:

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

x & y are random variables.

WJS
  • 714
  • 6
  • 13

1 Answers1

0

If I properly understood your question (which if I didn't please tell me) you should try this:

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;
    }
}
liam
  • 1,918
  • 3
  • 22
  • 28
  • 3
    You leak `Bitmap` and `Graphics` objects here. The creation of the `Graphics` object should be wrapped in a `using` statement to prevent that, and the old `pic.Image` should be explicitly disposed of. – Cody Gray - on strike Aug 02 '17 at 19:28
  • Sorry dude It don't function in my program. I need to do that the picturebox started again in the right side of the screen when it arrive in the left side of the screen. Sorry for my english again – Danny J. Parr Aug 02 '17 at 19:58
  • Check this out and see if it helps: https://stackoverflow.com/questions/6606819/creating-numerous-pictureboxes-by-code-only-one-is-visible – liam Aug 02 '17 at 20:04