我是 DotNet 编程的新手。我有一个严重的问题,但我不知道为什么。我已经使用了 Dispose()
方法,但“内存不足”问题仍然存在。大约前 30 次,一切正常。然后,Out of memory
发生了。此外,图像的大小为 13-16Mb。这是我的代码:
private void advanBtn_Click(object sender, EventArgs e)
{
InvertFunction();
}
private void InverFunction()
{
Bitmap bm = new Bitmap(imgBox.Image); // Out of memory
Image<Gray, byte> EmguImage = new Image<Gray, byte>(bm);
EmguImage = EmguImage.Not();
imgBox.Image.Dispose();
imgBox.Image = EmguImage.Bitmap;
bm.Dispose();
EmguImage.Dispose();
}
最佳答案
试试他们 documentation 中的建议.
The time of when garbage collector decides to dispose the image is not guaranteed. When working with large image, it is recommend to call the Dispose() method to explicitly release the object. Alternatively, use the using keyword in C# to limit the scope of the image
using (Bitmap bm = new Bitmap(imgBox.Image))
{
using (Image<Gray, Single> image = new Image<Gray, Single>(bm))
{
EmguImage = EmguImage.Not();
imgBox.Image.Dispose();
imgBox.Image = EmguImage.Bitmap;
}
}
作为最后的手段,您可以尝试强制垃圾回收。但不推荐这样做,只有在别无他法时才应使用。
GC.Collect();
我建议您在使用前阅读它 here .
关于C# New bitmap() - 内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45752677/