System.Drawing.dll中发生了未处理的“System.OutOfMemoryException”类型异常

时间:2014-11-15 16:06:04

标签: c# bitmap aforge

我试图遍历我加载的所有图像,我能够处理多达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();
}

1 个答案:

答案 0 :(得分:0)

我更喜欢使用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);
    }
}

有关using声明的详情,请参阅MSDN

<强>更新 但是,为什么不使用流来加载文件?建议在这种情况下使用流。