0

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

Community
  • 1
  • 1
Darren Young
  • 10,972
  • 36
  • 91
  • 150

2 Answers2

2

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.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
1

The following should work:

Form.BackgroundImage = null;
leppie
  • 115,091
  • 17
  • 196
  • 297
  • Hi, unfortunately this doesn't work as the bitmap is not loaded directly to the form. Please see my edit above. Thanks. – Darren Young Jan 12 '11 at 10:32