2014-11-15 137 views
0

我試圖循環遍歷所有加載的圖像,我能夠處理多達40個圖像,然後我得到Out的內存錯誤,儘管我正在處理變量tempImage。代碼在「Bitmap tempImage = new Bitmap(fileName);」線,PLZ的幫助! 有沒有辦法處理大量的輸入文件?像批處理一樣,將進程分成塊?由於該操作將持續超過一分鐘,程序將在此之前崩潰。在System.Drawing.dll中發生未處理的異常類型'System.OutOfMemoryException'

foreach (string fileName in openFileDialog.FileNames) 
{ 
    circleDetection examDetect = new circleDetection(); 
    Bitmap tempImage = new Bitmap(fileName); 
    directory.Text = fileName; 

    PictureBox picBox = new PictureBox(); 
    picBox.Width = 200; 
    picBox.Height = 200; 
    picBox.Location = new System.Drawing.Point(picBoard.Controls.Count * (picBox.Width + 5) + 5, 5); 
    picBox.SizeMode = PictureBoxSizeMode.Zoom; 
    picBox.BorderStyle = BorderStyle.FixedSingle; 
    examDetect.ProcessImage(tempImage); 
    picBox.Image = examDetect.getImage(); 

    Console.WriteLine(i++); 

    student.Add(compare(examDetect)); 
    picBoard.Controls.Add(picBox); 
    tempImage.Dispose(); 
} 
+0

在'foreach'循環中使用'using'並查看會發生什麼 –

+0

您的位圖有多大?因爲你的考試系列已經有了tempimage的參考資料,所以處置tempimage也無濟於事。 –

+0

爲什麼你將'tempImage'添加到'Exam'(對位圖持有一個引用),然後處理它? –

回答

0

我更喜歡使用using()而不是.Dispose()。它也在週期結束後處理一個對象。所以也許你應該試試像下一個?

foreach (string fileName in openFileDialog.FileNames) 
{ 
    circleDetection examDetect = new circleDetection(); 
    using (Bitmap tempImage = new Bitmap(fileName)) 
    { 
     Exam.Add(tempImage); 
     directory.Text = fileName; 

     PictureBox picBox = new PictureBox(); 
     picBox.Width = 200; 
     picBox.Height = 200; 
     picBox.Location = new System.Drawing.Point(picBoard.Controls.Count * (picBox.Width + 5) + 5, 5); 
     picBox.SizeMode = PictureBoxSizeMode.Zoom; 
     picBox.BorderStyle = BorderStyle.FixedSingle; 
     examDetect.ProcessImage(tempImage); 
     picBox.Image = examDetect.getImage(); 

     Console.WriteLine(i++); 

     student.Add(compare(examDetect)); 
     picBoard.Controls.Add(picBox); 
    } 
} 

,瞭解更多有關using請看MSDN

更新: 但是,爲什麼你不使用流加載你的文件?建議在這種情況下使用流。

相關問題