0

I made several drawings using Windows forms application visual studio Bitmap, my goal is to clear all drawings when I press the push button (make the background clear) The Following code is not exactly what my original code but this is how I created each drawing

Rectangle area;
Bitmap creature;//this only one drawing but I have several
Graphics scG;
Bitmap background;
private void Form1_Load(object sender, EventArgs e)
{
  background = new Bitmap(Width, Height);
  area = new Rectangle(50, 50, 50, 50);
  creature = new Bitmap(@"C:\Users\Desktop\Image.png");
}

public Bitmap Draw()
{
  Graphics scG = Graphics.FromImage(background);
  scG.DrawImage(creature, area);

        return background;
    }
}

private void Button_Click(object sender, EventArgs e)
{
  // I want to clear all the drawings by push this button
}
  • 1
    Since you are drawing into a Bitmap you created from scratch you just fill that Bitmap with a Color, like white or transparent: graphics.Clear(yourColor) or re-create it from scratch. You can't take back a single drawing, though. You need to cache the previous step if you need that. – TaW Jul 09 '16 at 05:47

3 Answers3

1

There is no way to remove things drawn on a bitmap.

Why don't just dump the old background bitmap and create a new one?

Tommy
  • 3,044
  • 1
  • 14
  • 13
0

The real way is to use event Paint:

private void Form1_Paint(object sender, PaintEventArgs e)
{
  e.Graphics.DrawImage(creature, area); // draw your image here...
}

To delete, just do nothing in Form1_Paint, and call Refresh();

0

this is duplicate , see this link : How to delete a drawn circle in c# windows form?

or test these Code :

Graphics.Clear();

or

Control.Invalidate();

or

this.Invalidate();
Community
  • 1
  • 1
mohammad
  • 275
  • 3
  • 16
  • no one of these does work with my code I got an error: there is no argument given that corresponds to the required formal parameter 'color' of 'Graphic.Clear (Color)' – Mohanad Al Nuaimi Jul 09 '16 at 04:55
  • @MohanadAlNuaimi use two button , first for drow and secont for clean , remove anything in form load and paste in first and in the second button white this.Invalidate(); – mohammad Jul 09 '16 at 05:18
  • I found a way to solve the problem, Thank you for trying to help – Mohanad Al Nuaimi Jul 09 '16 at 08:23