简体   繁体   中英

How do I remove the bitmap from a custom control in WinForms?

I have a form that is loaded with a generated bitmap. I want the user to be able to press a button and change the graphic displayed. My question is, how can I delete the bitmap that is currently displayed?

Edit: The bitmap is loading onto an ImageBox (not directly onto the form) Which was kindly suggested by Hans Passant c# panel for drawing graphics and scrolling

Thanks

Same principle as leppie's answer. Except that you need to set the ImageBox.Image property instead:

myImageBox.Image = null;


This works because of the following code (excerpted from Hans's answer to your previous question ):

protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
    if (mImage != null) e.Graphics.DrawImage(mImage, 0, 0);
    base.OnPaint(e);
}

When you set the control's Image property to null , the property setter forces the control to repaint itself ( this.Invalidate(); ). When it repaints itself, no image is drawn because the OnPaint method that is responsible for painting the control verifies that mImage != null before drawing it.

以下应该工作:

Form.BackgroundImage = null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM