Your title is misleading. The line that throws is not the Delete
but the Dispose
.
You have already set it to null
, so you can't Dispose
of it any more. Use this instead:
..
if (PatientImage.Image != null)
{
Image dummy = PatientImage.Image;
PatientImage.Image = null;
dummy.Dispose();
}
..
First we store a new dummy reference to the image; then we clear the reference from the control and finally we use the dummy reference to free the GDI resources.
This is also recommended whenever you want to set a new Image
to a PictureBox
.
(This looks a little more complicated than one would expect when dealing with reference variables; but that is not what PictureBox.Image
is. It is a property with all sorts of extras going on behind the scenes..)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…