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.