2014-11-15 69 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

更新: 但是,为什么你不使用流加载你的文件?建议在这种情况下使用流。

相关问题