An unhandled exception of type "System.OutOfMemoryException" occurred in System.Drawing.dll Additional information: Out of memory

I have a list of "images" that contains about 20 photos of 1 MB each. I want to scroll through the images in the list by clicking the "Next" button. But after about 8 shots, my memory goes out.

    private void button4_Click(object sender, EventArgs e) //next
    {
        index++;
        if (index >= images.Count) index = 0;
        CurrImage = images[index]; 
        Bitmap b = new Bitmap((Bitmap)CurrImage.Clone()); //breakpoint occurs her
        pictureBox1.Image = b; 

        NewThread = new Thread(new ThreadStart(ChooseColors2));
    }

The ChooseColors2 stream will use "CurrImage", so to avoid race conditions, I avoided this by creating a new bitmap as shown above.

Please note that if I use pictureBox1.Image = CurrImage; without creating a new bitmap, I do not get this error, but with the stream there will be an exception of the race condition.

+5
source share
2

, pictureBox1.Image, "" Bitmap :

pictureBox1.Image.Dispose();
+4

, using ; , . :

using (Bitmap b = new Bitmap((Bitmap)CurrImage.Clone()))
{
    pictureBox1.Image = b;
}'

, , , "" #.

0

All Articles