Clear image on picturebox

后端 未结 13 1770
不思量自难忘°
不思量自难忘° 2020-12-05 13:13

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

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

Please help.

相关标签:
13条回答
  • 2020-12-05 13:22

    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.

    0 讨论(0)
  • 2020-12-05 13:22

    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);
    
    0 讨论(0)
  • 2020-12-05 13:23

    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;
    
    0 讨论(0)
  • 2020-12-05 13:23

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

    0 讨论(0)
  • 2020-12-05 13:23

    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 讨论(0)
  • 2020-12-05 13:24

    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;
    
    0 讨论(0)
提交回复
热议问题