开发者

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

I have a form tha开发者_StackOverflow中文版t 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.


The following should work:

Form.BackgroundImage = null;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜