【问题标题】:PictureBox Image Painted two times issuePictureBox 图像绘制两次问题
【发布时间】:2015-05-22 20:50:32
【问题描述】:

我正在将多个图像加载到一个图片框中,并试图将其置于底部的中心。我正在使用以下功能:

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

在阅读了here等几个问答后,我将绘画事件更新如下:

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 更新都会触发一个绘制事件。这正常吗?这是正确的做法吗?

感谢您的热心帮助。

【问题讨论】:

  • 您链接到的 SO 还包括调用 Refresh() 的代码 - 您在这样做吗?
  • 您需要学习 PictureBox 的基础知识。有关如何使用它的一些提示,请参阅here
  • 每个 gif 更新那是什么?
  • 图片居中,如果小于pb设置它的SizeMode=CenterImage
  • 这只是不正确的代码。 PictureBox 已经实现了 OnPaint 并绘制了 Image 属性的内容,注意您选择的 SizeMode。然后它触发 Paint 事件并且您的事件处理程序再次 绘制图像,而不注意 SizeMode。是的,动画 GIF 的唯一方法是绘制每一帧。你做错了,完全不清楚为什么你这样做。

标签: c# picturebox


【解决方案1】:

好的,我终于做了一个简单的填充:

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);

【讨论】:

    猜你喜欢
    • 2018-03-17
    • 2020-06-17
    • 2022-09-05
    • 2012-01-28
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多