I am trying to load about 60 pictures in a list. Each picture is about 1MB. For 20 pictures no problem but above that I get Out of memory exception on the code line below. I have searched vastly of related issues, some stating about "using" key word and stream but since I am a beginner can someone please help me.
Image image = Bitmap.FromFile(Filename);
Here is my code
private void LoadBtn_Click_1(object sender, EventArgs e)
{
OpenFileDialog newDialog = new OpenFileDialog();
if (newDialog.ShowDialog() == DialogResult.OK)
{
images.Clear();
string dirPath = System.IO.Path.GetDirectoryName(newDialog.FileName.ToLower());
DirectoryInfo di = new DirectoryInfo(dirPath);
FileInfo[] finfos = di.GetFiles("*.*");
foreach (FileInfo fi in finfos)
{
string ext = fi.Extension.ToLower();
if ((ext.Equals(".png")) || (ext.Equals(".jpg")) || (ext.Equals(".tif")) || (ext.Equals(".gif")))
{
string Filename = fi.FullName;
Image image = Bitmap.FromFile(Filename); //exception occurs HERE
images.Add(image);
//this.imageList1.Images.Add(image);
//image.Dispose();
}
}
}
pictureBox3.Image = images[0];
}
I am using C#, windows forms. thanks