如何使图像平滑移动?

时间:2012-11-27 18:54:49

标签: c# forms picturebox doublebuffered

我有一个PictureBox图像,其形式随着面板上的鼠标移动而移动。

它正在我想要它移动,但它一直闪烁(如刷新)并且我了解到它是表格的问题。

我在表单的构造函数中尝试了以下代码行,但没有成功:

SetStyle( ControlStyles.ResizeRedraw, true );

SetStyle( ControlStyles.UserPaint, true );

SetStyle( ControlStyles.AllPaintingInWmPaint, true );   

SetStyle( ControlStyles.OptimizedDoubleBuffer, true );    

这是鼠标移动的事件处理程序,如果它有助于查看所有图片。 chipHolder是一个面板,图像是分别从文件导入的图像。

private void grid_MouseMove(object sender, MouseEventArgs e)
{ 
      columnPosition = e.X;

      if (columnPosition != -1)
      {
          if (!(columnPosition < 35 || columnPosition > 610))
          {
                chipHolder.Controls.Clear();  
                PictureBox picBox = new PictureBox();
                chipHolder.Controls.Add(picBox);
                picBox.Image = image;
                picBox.Width = image.Width;
                picBox.Height = image.Height;
                picBox.Location = new Point(columnPosition - 33, 0);
                picBox.Show();
          }
      }
      chipHolder.Update();
}

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

不要重新创建PictureBox,只需移动它。

试过这个,图像移动没有任何闪烁:

private void button1_Click(object sender, EventArgs e)
{
    for (int iter = 0; iter < 500; iter++)
    {
        pictureBox1.Location = new Point(pictureBox1.Left + 1, pictureBox1.Top + 1);
        Application.DoEvents();
        System.Threading.Thread.Sleep(30);
    }
}

对于鼠标移动:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    pictureBox1.Location = new Point(e.X, e.Y);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    pictureBox1.Location = new Point(e.X + pictureBox1.Left, e.Y + pictureBox1.Top);
}

答案 1 :(得分:0)

伊戈尔所说的话:

private void grid_MouseMove(object sender, MouseEventArgs e)
{ 
      columnPosition = e.X;

      if (columnPosition != -1)
      {
          if (!(columnPosition < 35 || columnPosition > 610))
          {
                PictureBox picBox = chipHolder.Controls[0] // whatever your picbox id is;
                picBox.Location = new Point(columnPosition - 33, 0);
          }
      }
}