First of all, you cannot dispose
PictureBox
, just because this class does not implement the interface
System.IDisposable
.
Much worse, you don't understand the concept of "deletion" and the whole idea of a managed system. In such systems, you don't release memory or dispose any managed memory (dispose does exist but is used for some very different purposes). Instead, you just stop using the object, and the
Garbage Collector will reclaim its memory (after calling its destructor, by the way) some time later, when the object will be detected as
unreachable. This is well explained here:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[
^].
Now, stop using some object and its desctruction are related but independent things. You need to exclude this object from UI, that's all you need. Even if you could destroy the object, it would be utterly dangerous is IU (or anything else) is using it; it's so good that you cannot to it in CLR. In this case, to remove object, you need to use
System.Windows.Forms.Controls.Remove
on the parent control:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx[
^].
That's not all. I cannot be sure that you really need to use this class at all, because CodeProject practice shows that his class is one of those abused by the beginners the most. I just thought you would need to be warned, even if your use is reasonable. Please see my past answers:
Append a picture within picturebox[
^],
draw a rectangle in C#[
^],
How do I clear a panel from old drawing[
^],
capture the drawing on a panel[
^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[
^],
Drawing Lines between mdi child forms[
^].
—SA