Move picture (PictureBox) in form

0

How to move an image into a form windows form.

I used the following code:

private void button1_Click(object sender, EventArgs e)
{
    int tamanhoFundo = picFundo.Width;
    int x = myPic.Location.X;
    int y = myPic.Location.Y;
    while (x < tamanhoFundo)
    {
        int pos = x += 5;
        myPic.Location = new Point(pos, myPic.Location.Y);
        Thread.Sleep(500);
    }
}

Even move the image, only a trace remains:

How to avoid this?

    
asked by anonymous 05.01.2015 / 19:24

2 answers

2

If the image is above picFundo , before calling Thred.Sleep give Refresh() in the background. Look at the code below:

private void button1_Click(object sender, EventArgs e)
{
    int tamanhoFundo = picFundo.Width;
    int x = myPic.Location.X;
    int y = myPic.Location.Y;
    while (x < tamanhoFundo)
    {
        int pos = x += 5;
        myPic.Location = new Point(pos, myPic.Location.Y);
        picFundo.Refresh();
        Thread.Sleep(500);
    }
}
    
05.01.2015 / 19:43
1

This is a deliberate occurrence of System.Windows.Forms itself, which is concerned with increasing window rendering performance. But you can disable this with the method Refresh() and Update() , the two are great render your window. see the example based on your code:

private void button1_Click(object sender, EventArgs e)
{
  int tamanhoFundo = picFundo.Width;
  int x = myPic.Location.X;
  int y = myPic.Location.Y;
  while (x < tamanhoFundo)
  {
     int pos = x += 5;
     myPic.Location = new Point(pos, myPic.Location.Y);
     myPic.Refresh(); myPic.Update();
     Thread.Sleep(250); // Também é bom reduzir o tempo de Sleep, pois os métodos Refresh() e Update() consomem um pouco de memória para re-criar o componente, então, de 500 vamos colocar 250.
  }
}
    
03.05.2015 / 05:19