3

Why am I receiving an OutOfMemoryException when I try to load files from my Pictures folder? There are no more than about 20 pictures in there (including sub-directories).

private void Form1_Shown(object sender, EventArgs e)
{
    dynamic files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
    if(files != null)
    {
        foreach(var file in files)
        {
            PictureBox box = new PictureBox();
            box.Image = Image.FromFile(file);
            box.Height = 250;
            box.SizeMode = PictureBoxSizeMode.Zoom;

            canvas.Controls.Add(box);
        }
    }
}

An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll

I'm guessing there's a better way to be writing this?

Additional information: Out of memory.

Max
  • 12,622
  • 16
  • 73
  • 101
spike.y
  • 389
  • 5
  • 17
  • 2
    Did you make sure that there are only pictures in that folder? – Mickey May 05 '14 at 09:36
  • 2
    Why do you use "dynamic" for the files variable? – Håkan Fahlstedt May 05 '14 at 09:37
  • 1
    How large are the pictures? Maybe it's possible you're actually out of memory? I think resizing the box won't shrink the actual image in memory. – sdf May 05 '14 at 09:37
  • @TheMotivation Yep. I did make sure there are only Pictures in that folder and all its sub-directories. I'm kinda OCD in that way. – spike.y May 05 '14 at 09:40
  • 1
    okay, still I would recommend to check each file before loading it into a picturebox ;) – Mickey May 05 '14 at 09:42
  • @HåkanFahlstedt Because I'm not allowed to use it at work. So in my personal projects, I tend to use whatever the hell I want, so long as it actually works. – spike.y May 05 '14 at 09:42
  • @sdf the pictures are no more than 1mb each. I have 8GB ram. How else would I load pictures onto the form then if this is what's going to happen? – spike.y May 05 '14 at 09:43
  • 3
    @spike.y your file size does not neccesarily correlate with how much memory a picture takes, as an example with png crush/black & white image you can get a 4000x2500 file to be about 0.5k on disk, when loaded into memory it will probably take 4000x2500x4 bytes (regular ARGB) – sdf May 05 '14 at 09:47
  • I just opened up a finished project I wrote last week, and the exact same code is in there to load pictures from the same folder. It still works. Not sure why, now. – spike.y May 05 '14 at 09:51
  • @TheMotivation you were right. There _is_ a file in the Pictures folder which is not an image file. The file is `Desktop.ini`, and the reason I said there were no files other than images is because I checked the `Show hidden files` option and there were none. Upon looking more closely VS says that Image.FromFile is trying to load a Desktop.ini file (which doesn't even appear when the `Show hidden files` option in explorer is checked). – spike.y May 05 '14 at 09:59
  • 1
    So this solves your problem? I'll post it as an answer if so. – Mickey May 05 '14 at 10:01
  • @TheMotivation it sure did. Thank you very much. I should've done a file-type check to begin with. Disappointed in myself right now. – spike.y May 05 '14 at 10:07

0 Answers0