Abdul Jaja
Abdul Jaja

Reputation: 85

Why i'm getting exception out of memory when trying to display images in pictureBox?

int countFiles = 0;
private void timer1_Tick(object sender, EventArgs e)
{
    pictureBox1.Image = Image.FromFile(ff[countFiles]);
    countFiles++;
}

ff is a List contain 40 files. The timer interval is set to 1000ms.

The exception is on the line:

pictureBox1.Image = Image.FromFile(ff[countFiles]);

  System.OutOfMemoryException was unhandled
  HResult=-2147024882
  Message=Out of memory.
  Source=System.Drawing

Upvotes: 0

Views: 88

Answers (1)

Rafal
Rafal

Reputation: 12619

You need to release previous image from memory try this:

    private void timer1_Tick(object sender, EventArgs e)
    {
       if(pictureBox1.Image != null)
            pictureBox1.Image.Dispose();
        pictureBox1.Image = Image.FromFile(ff[countFiles]);
        countFiles++;
    }

Upvotes: 3

Related Questions