PictureBox图像绘制两次问题

本文关键字:两次 问题 图像 绘制 PictureBox | 更新日期: 2023-09-27 17:50:31

我正在加载多个图像到一个pictureBox,并试图在底部的中心。我正在使用下面的函数:

public void toogleImage(Boolean visible, Bitmap img)
{
  if (visible)
  {
    pict_statusCenter.Show();
    pict_statusCenter.Image = img;
  }
  else
  {
    pict_statusCenter.Hide();
  }
}

在阅读了几个问题之后,例如这里,我已经更新了油漆事件如下:

private void pict_statusCenter_Paint(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;
            g.DrawImage(pict_statusCenter.Image, 
                        (pict_statusCenter.Width - pict_statusCenter.Image.Width) / 2,
                         pict_statusCenter.Height - pict_statusCenter.Image.Height);
        }

图像现在绘制在图片框的正确位置。问题是在左上角绘制了图像的副本。我有点迷路了,有线索吗?

第二个问题:我的图像实际上是一个gif。在调试过程中,我注意到每个gif更新都会触发一个paint事件。这正常吗?这是正确的做法吗?

谢谢你的帮助。

PictureBox图像绘制两次问题

好了,我最后做了一个简单的填充:

pict_statusCenter.Padding = new Padding(
  (pict_statusCenter.Width - pict_statusCenter.Image.Width)/2, 
  (pict_statusCenter.Height- pict_statusCenter.Image.Height),
  (pict_statusCenter.Width - pict_statusCenter.Image.Width)/2,
  0);