Graphics.DrawImage 并不总是绘制整个位图?
我在使用 Graphics.DrawImage 时遇到了一个奇怪的问题。
当在 OnPaint 中使用 e.Graphics.DrawImage(Image, Point) 在控件上绘制位图“缓冲区”时,图像的某些部分似乎被省略。缓冲区填充在一个辅助方法中,该方法使用从其构造的Graphics
直接绘制到Bitmap
上。当控件绘制时,缓存的位图将绘制在控件上。
位图本身似乎没有遗漏任何内容,因为当我将位图保存到光盘并检查它时,它都在那里。 (见下图)
位图缓冲区如下所示:
(来源: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:
(source: zachjohnson.net)
This what appears on the control:
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当调用 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.
你可以尝试一些不同的事情。
吨调试这个选项的选项,只是缩小范围的问题。
You could try a couple of different things.
Tons of options for debugging this, just a matter of narrowing it down.
查看
System.Windows.Forms.ControlStyles.ResizeRedraw
Check out the
System.Windows.Forms.ControlStyles.ResizeRedraw