我試圖在列表中加載大約60張圖片。每張圖片大約1MB。對於20張照片來說沒有問題,但是在上面的代碼行中,我得到了內存不足的異常。我搜索了很多相關的問題,一些說明「使用」關鍵詞和流,但由於我是初學者,有人可以幫助我。Imagefrom.File()。內存不足異常
Image image = Bitmap.FromFile(Filename);
這裏是我的代碼
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];
}
我使用C#,Windows窗體。感謝
看看http://stackoverflow.com/questions/2610416/is-there-a-reason-image-fromfile-throws-an-outofmemoryexception-for-an-invalid-i – 2013-03-23 07:06:54
感謝您的回覆,但沒有答案在那裏符合我的問題。我的文件沒有損壞,問題必須真正處理所有文件的大小。正在考慮是否有關於這種記憶問題的方式,因爲我已經看到了完美的圖片瀏覽器,而這正是我想要的。例如,這個項目「https://sourceforge.net/projects/picturefilter/」確實是我想要的,但我無法訪問代碼。只有.exe文件可用 – Basco 2013-03-23 07:30:01