English 中文(简体)
Graphics.DrawImage Doesn t Always Paint The Whole Bitmap?
原标题:

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);
    }
}
最佳回答

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.

  • 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.

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





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签