Graphics.DrawImage 并不总是绘制整个位图?

发布于 2024-08-13 01:05:15 字数 1165 浏览 4 评论 0原文

我在使用 Graphics.DrawImage 时遇到了一个奇怪的问题。

当在 OnPaint 中使用 e.Graphics.DrawImage(Image, Point) 在控件上绘制位图“缓冲区”时,图像的某些部分似乎被省略。缓冲区填充在一个辅助方法中,该方法使用从其构造的Graphics直接绘制到Bitmap上。当控件绘制时,缓存的位图将绘制在控件上。

位图本身似乎没有遗漏任何内容,因为当我将位图保存到光盘并检查它时,它都在那里。 (见下图)

位图缓冲区如下所示:
buffer
(来源:zachjohnson.net

控件上显示的内容如下:
显示值
(来源:zachjohnson.net

这就是我在 OnPaint 中所做的一切:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    if (this.bufferInvalid)
    {
        this.UpdateBuffer();
    }

    if (this.buffer != null)
    {
        e.Graphics.DrawImage(this.buffer, Point.Empty);
    }
}

I have run into a strange problem when using Graphics.DrawImage.

When using e.Graphics.DrawImage(Image, Point) in OnPaint to paint a bitmap 'buffer' on the control, it appears that parts of the image are omitted. The buffer is populated in a helper method which draws directly onto the Bitmap using a Graphics constructed from it. When the control paints, the cached bitmap is drawn on the control.

Nothing appears to be omitted on the bitmap itself, because when I saved the bitmap to disc and examined it, it was all there. (see images below)

This is what the bitmap buffer looks like:

buffer
(source: zachjohnson.net)

This what appears on the control:

displayed value
(source: zachjohnson.net)

This is all I am doing in OnPaint:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    if (this.bufferInvalid)
    {
        this.UpdateBuffer();
    }

    if (this.buffer != null)
    {
        e.Graphics.DrawImage(this.buffer, Point.Empty);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青瓷清茶倾城歌 2024-08-20 01:05:15

当调用 OnPaint 时,Windows 有一个允许绘画的无效区域,而其他所有内容都会被剪掉。当您想要更新矩形控件时,您应该在Windows API中调用InvalidateRect以确保整个矩形都是无效区域的一部分。当控件的任何部分无效时,OnPaint 将被自动调用。

When OnPaint is called, Windows has an invalid region that it allows painting into, and everything else is clipped. When you want a rectangular control to be updated, you should call InvalidateRect in the Windows API to make sure the entire rectangle is part of the invalid region. OnPaint will be called automatically whenever there is any part of the control that is invalid.

遥远的她 2024-08-20 01:05:15

你可以尝试一些不同的事情。

  • 将代码发布到“UpdateBuffer”,
  • 在 updatebuffer 调用后执行 bitmap.save (即 buffer.save)并检查位图的外观。
  • 确保所有控件都已先绘制,并且没有控件被绘制为透明等。
  • 将“onpaint”和“update buffer”的代码放入单独的项目中进行测试以查看其是否有效。
  • 取出“base.onpaint”并检查会发生什么
  • 尝试调用 bitmap.drawimageunscaled 而不是 bitmap.drawimage
  • 尝试使用 new point(0,0) 而不是 point.empty (一个很长的镜头,但你永远不知道......)

吨调试这个选项的选项,只是缩小范围的问题。

You could try a couple of different things.

  • post the code to "UpdateBuffer"
  • do a bitmap.save (i.e. buffer.save) after the updatebuffer call and check what the bitmap looks like.
  • ensure that all controls have been drawn first and that no controls are being drawn transparent etc..
  • take the code of "onpaint" and "update buffer" and put it in a separate project testing to see if it works.
  • take out "base.onpaint" and check what happens
  • try a call to bitmap.drawimageunscaled instead of bitmap.drawimage
  • try using new point(0,0) instead of point.empty (a loooong shot but ya never know...)

Tons of options for debugging this, just a matter of narrowing it down.

沦落红尘 2024-08-20 01:05:15

查看System.Windows.Forms.ControlStyles.ResizeRedraw

Check out the System.Windows.Forms.ControlStyles.ResizeRedraw

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文