I have code that looks like this:
Bitmap b = new Bitmap(...); //doesn't matter how this image is constructed
pictureBox1.Image = b;
//b.Dispose() placed here crashes the application and so does "using" if used on the declaration of 'b'
Since in C# you should dispose of everything that is IDisposable
, this image needs to be disposed of at some point. It seems that the property PictureBox.Image
only makes a shallow copy, so I need to keep the object alive (aka not Dispose()
of it) for the entire time the image stays in the box. If I later change the image in the box:
pictureBox1.Image = b2;
Does the property dispose of it for me? Or should I do it manually:
Image im = pictureBox1.Image;
pictureBox1.Image = b2;
im.Dispose();