Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picture Box out of Memory

Tags:

c#

winforms

I am developing a windows form application. At one of my form i place a picture box control. Its working fine for majority of time. But sometime it displays red cross rather than displaying actual picture.

I further explore this and came to know that this control handle exceptions internally. So i go to Debug->Exceptions and Check the relevant thrown boxes. Then i found that the red box is display due to following error:

A first chance exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll

Additional information: Out of memory.

Could any body suggest me how to deal with this exception?

like image 761
Jame Avatar asked Aug 09 '11 03:08

Jame


1 Answers

As pair to you comment that you are using multiple images in the PictureBox, You should "like @Jason suggested" dispose the old image before applying the new one, like:

private void ChangePictureBoxImage(Image image)
{
    pictureBox.Image.Dispose();//dispose the old image.

    pictureBox.Image = image;
}
like image 141
Jalal Said Avatar answered Oct 20 '22 16:10

Jalal Said