C# New bitmap() - 内存不足

标签 c# bitmap out-of-memory emgucv dispose

我是 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/

相关文章:

c# - NHibernate Evict By Type 而不是实例

delphi - 了解 Delphi 和 C++ Builder 中的 TBitmap.Scanline

c# - 将颜色过滤器应用于位图对象

java.lang.OutOfMemoryError : Direct buffer memory when invoking Files. 读取所有字节

c# - SignalR 2.2 回退传输耗尽 - 连接不可靠

c# - 为什么开放类型 Predicate<T> 比它的对应类型慢

java - 缓存谷歌地图v2标记位图

java - 如何避免 Java 中的 Java 堆空间异常

java - Android 内存不足错误位图

c# - 静态类优缺点