C# 类型为';的未处理异常;System.OutOfMemoryException';发生在System.Drawing.dll中

C# 类型为';的未处理异常;System.OutOfMemoryException';发生在System.Drawing.dll中,c#,bitmap,aforge,C#,Bitmap,Aforge,我尝试循环我加载的所有图像,我可以处理多达40个图像,然后我得到内存不足错误,尽管我正在处理可变的tempImage。代码在“Bitmap tempImage=new Bitmap(fileName);”行中断,plz帮助! 有没有办法处理大量的输入文件?像批处理和将过程分割成块?由于操作将持续一分钟以上才能完成,因此程序届时将崩溃 foreach (string fileName in openFileDialog.FileNames) { circleDetection examDe

我尝试循环我加载的所有图像,我可以处理多达40个图像,然后我得到内存不足错误,尽管我正在处理可变的tempImage。代码在“Bitmap tempImage=new Bitmap(fileName);”行中断,plz帮助! 有没有办法处理大量的输入文件?像批处理和将过程分割成块?由于操作将持续一分钟以上才能完成,因此程序届时将崩溃

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();
}

我更喜欢使用
using()
而不是
.Dispose()
。它还在循环结束后处理对象。所以也许你应该试试smth,就像下一个

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);
    }
}
有关使用语句的更多信息,请参阅

更新:
但是,为什么不使用流来加载文件呢?对于这种情况,建议使用stream

foreach
循环中使用
using
,查看发生了什么情况位图有多大?另外,由于您的考试集合已经有tempimage的引用,因此处理tempimage也没有帮助。为什么要将
tempimage
添加到
Exam
(保留对位图的一个引用)中,如果有,则进行处理?