Clear image on picturebox

后端 未结 13 1769
不思量自难忘°
不思量自难忘° 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: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.

提交回复
热议问题