50

How can I clear draw image on picturebox? The following doesn't help me:

pictbox.Image = null;
pictbox.Invalidate();

Please help.

EDIT

private void pictbox_Paint(object sender, PaintEventArgs e) 
{ 
     Graphics g = e.Graphics; 
     vl.Draw(g, ref tran.realListForInsert); 
} 

public void Draw(Graphics g, ref List<double> arr) 
{ 
    g.DrawRectangle(new Pen(Brushes.Red, 3), nodeArr[Convert.ToInt32(templstName)].pict.Location.X, nodeArr[Convert.ToInt32(templstName)].pict.Location.Y, 25, 25); 
    g.DrawRectangle(new Pen(Brushes.Green, 3), nodeArr[Convert.ToInt32(templstArgName)].pict.Location.X, nodeArr[Convert.ToInt32(templstArgName)].pict.Location.Y, 25, 25); 
    nodeArr[Convert.ToInt32(templstName)].text.Text = arr[Convert.ToInt32(templstArgName)].ToString(); 
    arr[Convert.ToInt32(templstName)] = arr[Convert.ToInt32(templstArgName)]; 
} 
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
ktarik
  • 653
  • 1
  • 6
  • 9

14 Answers14

44

Setting the Image property to null will work just fine. It will clear whatever image is currently displayed in the picture box. Make sure that you've written the code exactly like this:

picBox.Image = null;
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
37

As others have said, setting the Image property to null should work.

If it doesn't, it might mean that you used the InitialImage property to display your image. If that's indeed the case, try setting that property to null instead:

pictBox.InitialImage = null;
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • From comments to another answer, it looks like this is probably the correct solution. Probably never would have come up with that, I can't imagine how this property is even remotely useful in WinForms apps. – Cody Gray - on strike May 02 '11 at 11:49
  • @Cody, the `InitialImage` property holds the image that is displayed while the actual image is loading (usually through the `ImageLocation` property). You can think of it as a temporary placeholder. I agree with your comment in @V4Vendetta's answer, it doesn't look like the questioner's `PictureBox` actually contains an image, but it *does* look like he wants to remove the placeholder, which might not be possible. – Frédéric Hamidi May 02 '11 at 12:16
  • Unfortunately, it did NOT work for me all the time.I've shown blow how I cleared the picture box. – dmihailescu Apr 02 '13 at 21:11
  • Setting the Image to null worked for me. We use an InitialImage of a bitmap that says "No Picture Loaded" so we can tell if there's a picture loaded, trying to load, or broken. – Josh P Aug 05 '13 at 19:42
17
if (pictureBox1.Image != null)
{
    pictureBox1.Image.Dispose();
    pictureBox1.Image = null;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Grant Li
  • 259
  • 2
  • 5
8

You need the following:

pictbox.Image = null;
pictbox.update();
quinz
  • 1,282
  • 4
  • 21
  • 33
stacktay
  • 81
  • 2
  • 3
5
private void ClearBtn_Click(object sender, EventArgs e)
{
    Studentpicture.Image = null;
}
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
user1608207
  • 71
  • 1
  • 4
5

I assume you want to clear the Images drawn via PictureBox.

This you would be achieved via a Bitmap object and using Graphics object. you might be doing something like

Graphics graphic = Graphics.FromImage(pictbox.Image);
graphic.Clear(Color.Red) //Color to fill the background and reset the box

Is this what you were looking out?

EDIT

Since you are using the paint method this would cause it to be redrawn every time, I would suggest you to set a flag at the formlevel indicating whether it should or not paint the Picturebox

private bool _shouldDraw = true;
public bool ShouldDraw
{
    get { return _shouldDraw; }
    set { _shouldDraw = value; }
}

In your paint just use

if(ShouldDraw)
  //do your stuff

When you click the button set this property to false and you should be fine.

Adnan Mohib
  • 331
  • 5
  • 16
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • Uhh, you can't draw images via a PictureBox in the first place, so this doesn't make a lot of sense. You're just filling the image displayed by the picture box with the color red. If you want to *remove* the image altogether, you need to set the `Image` property to `null`, as explained in my answer. – Cody Gray - on strike May 02 '11 at 11:17
  • @cody well he mentioned clearing **draw image** so i assumed he is using graphic objects to manipulate the image and performing some actions on the same. – V4Vendetta May 02 '11 at 11:27
  • I think the problem is in this. But display exception "ArgumentNullException was unhandled" – ktarik May 02 '11 at 11:35
  • when i click on button which has this code you described(sorry for my english;)) – ktarik May 02 '11 at 11:42
  • The code above will produce an `ArgumentNullException` if `pictBox.Image` is not set to an image. That is, if you've set it to `null`. You can't create a `Graphics` object from `null`. – Cody Gray - on strike May 02 '11 at 11:43
  • Well first are you drawing via picturebox as i assumed or you are trying the code. what is the null value being passed (i think pictbox.Image is null) Is it so ? – V4Vendetta May 02 '11 at 11:46
  • I also saw that pictBox.Image is always null, but pictBox.InitialImage is "{System.Drawing.Bitmap}" – ktarik May 02 '11 at 11:47
  • @ktarik: So you need to clear the `InitialImage` property by setting it to `null`. See Frederic's answer for details. – Cody Gray - on strike May 02 '11 at 11:48
  • InitialImage and ErrorImage is always seen in Designer properties as BitMap, i think this is what the OP is referring to – V4Vendetta May 02 '11 at 11:53
  • 1
    @ktarik: What, exactly, convinces you that there *is* an image displayed in your picture box? – Cody Gray - on strike May 02 '11 at 11:54
  • @ktarik Can you post code where by you are setting the image to be displayed – V4Vendetta May 02 '11 at 12:12
  • private void pictbox_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; vl.Draw(g, ref tran.realListForInsert); } – ktarik May 02 '11 at 12:34
  • public void Draw(Graphics g,ref Listarr) { g.DrawRectangle(new Pen(Brushes.Red, 3), nodeArr[Convert.ToInt32(templstName)].pict.Location.X, nodeArr[Convert.ToInt32(templstName)].pict.Location.Y, 25, 25); g.DrawRectangle(new Pen(Brushes.Green, 3), nodeArr[Convert.ToInt32(templstArgName)].pict.Location.X, nodeArr[Convert.ToInt32(templstArgName)].pict.Location.Y, 25, 25); nodeArr[Convert.ToInt32(templstName)].text.Text = arr[Convert.ToInt32(templstArgName)].ToString(); arr[Convert.ToInt32(templstName)] = arr[Convert.ToInt32(templstArgName)];} – ktarik May 02 '11 at 12:36
  • @cody so he is actually drawing as i suspected – V4Vendetta May 03 '11 at 02:05
  • What's `vl` that you're calling the `Draw` method on? This is not how you're supposed to draw things. The picture box in .NET is not a replacement for the Picture Box control in VB 6. It's not a container for your own artwork. – Cody Gray - on strike May 03 '11 at 07:20
1

For the Sake of Understanding:

Depending on how you're approaching your objective(s), keep in mind that the developer is responsible to Dispose everything that is no longer being used or necessary.

This means: Everything you've created along with your pictureBox (i.e: Graphics, List; etc) shall be disposed whenever it is no longer necessary.

For Instance: Let's say you have a Image File Loaded into your PictureBox, and you wish to somehow Delete that file. If you don't unload the Image File from PictureBox correctly; you won't be able to delete the file, as this will likely throw an Exception saying that the file is being used.

Therefore you'd be required to do something like:

pic_PhotoDisplay.Image.Dispose();
pic_PhotoDisplay.Image = null;
pic_PhotoDisplay.ImageLocation = null;
// Required if you've drawn something in the PictureBox. Just Don't forget to Dispose Graphic.
pic_PhotoDisplay.Update();

// Depending on your approach; Dispose the Graphics with Something Like:
gfx = null;
gfx.Clear();
gfx.Dispose();

Hope this helps you out.

0

I've had a stubborn image too, that wouldn't go away by setting the Image and InitialImage to null. To remove the image from the pictureBox for good, I had to use the code below, by calling Application.DoEvents() repeatedly:

            Application.DoEvents();
            if (_pictureBox.Image != null)
                _pictureBox.Image.Dispose();
            _pictureBox.Image = null;
            Application.DoEvents();
            if (_pictureBox.InitialImage != null)
                _pictureBox.InitialImage.Dispose();
            _pictureBox.InitialImage = null;
            _pictureBox.Update();
            Application.DoEvents();
            _pictureBox.Refresh();
dmihailescu
  • 1,625
  • 17
  • 15
  • `Application.DoEvents()` is completely unnecessary here. - Plus you are running the same commands twice, whats the point? – Mark Dec 07 '17 at 11:20
0

I had to add a Refresh() statement after the Image = null to make things work.

0
pictureBox1.Image= new Bitmap(pictureBox1.Width, pictureBox1.Height);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-1

Its so simple! You can go with your button click event, I used it with a button property Name: "btnClearImage"

// Note 1a:
// after clearing the picture box 
// you can also disable clear button 
// by inserting follwoing one line of code:

btnClearImage.Enabled = false   

// Note 1b:
// you should set your button Enabled property
// to "False"
// after that you will need to Insert 
// the following line to concerned event or button
// that load your image into picturebox1 
// code line is as follows:

btnClearImage.Enabled = true;
Pm Duda
  • 741
  • 5
  • 16
-1

I used this method to clear the image from picturebox. It may help some one

private void btnClear1_Click(object sender, EventArgs e)
        {

            img1.ImageLocation = null;
        }
Amal Ps
  • 703
  • 7
  • 19
-2

You should try. When you clear your Graphics you must choose color. SystemColors.Control is native color of form

Graphics g = pB.CreateGraphics();
g.Clear(SystemColors.Control);
-3

clear pictureBox in c# winform Application Simple way to clear pictureBox in c# winform Application