Jame
Jame

Reputation: 22200

Picture Box out of Memory

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?

Upvotes: 5

Views: 11312

Answers (2)

Jalal Said
Jalal Said

Reputation: 16162

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;
}

Upvotes: 11

lettucemode
lettucemode

Reputation: 445

How much RAM does your target machine have?

You got this exception because the call to allocate more memory for the image failed. If you are unable to increase the amount of memory on your computer, try reducing the image's size or close any other programs you have running.

Upvotes: 0

Related Questions