Your problem is likely that, at some point, you are calling the Dispose
method on one of your Image
objects. When you call Image.Dispose
, it deletes the underlying image data from memory, so the Image
object still exists, but is invalid because it no longer contains an actual image. When you set the PictureBox.Image
property to a loaded Image
object, the PictureBox
control assumes that the Image
object will remain valid so that it can reuse it later any time the control needs to repaint itself to the screen. For instance:
Dim myImage As Image = Image.FromFile("file path")
PictureBox1.Image = myImage
PictureBox1.Refresh() ' This works
myImage.Dispose()
PictureBox1.Refresh() ' This throws an exception because it tries to access the disposed Image object
The PictureBox
control will automatically dispose of the image for you when it is disposed, so you don't need to worry about disposing it yourself. The only time you should be disposing your images is when you are not giving them to any other objects for later use.