内存不足异常C#freezable.freeze

内存不足异常C#freezable.freeze,c#,visual-studio,multithreading,C#,Visual Studio,Multithreading,我正试图将一些图片复制到RAM中,但这会导致内存不足异常。。 我不知道为什么,但我认为这是“冻结()的原因”。但如何“解冻”,这真的是问题吗 public void preLoadThread(Object o) { Overlay ov = (Overlay)o; ImageSource tempNext = BitmapConverter(ov.tempPreLoadPathNext); ImageSource temp

我正试图将一些图片复制到RAM中,但这会导致内存不足异常。。 我不知道为什么,但我认为这是“冻结()的原因”。但如何“解冻”,这真的是问题吗

        public void preLoadThread(Object o)
    {
        Overlay ov = (Overlay)o;
        ImageSource tempNext = BitmapConverter(ov.tempPreLoadPathNext);
        ImageSource tempPrev = BitmapConverter(ov.tempPreLoadPathPrev);
        tempNext.Freeze();
        tempPrev.Freeze();
        ov.Dispatcher.Invoke(
            DispatcherPriority.Normal,
            (Action)delegate()
            {
                ov.preLoadedNext = tempNext;
                ov.preLoadedPrev = tempPrev;
                ov.preLoadPathNext = ov.tempPreLoadPathNext;
                ov.preLoadPathPrev = ov.tempPreLoadPathPrev;
            }
        );
    }

    public BitmapSource BitmapConverter(String path)
    {
        System.Drawing.Bitmap b = null;
        using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
        {
            try
            {
                b = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(fs);
            }
            catch (Exception)
            {
                GC.Collect();
                GC.WaitForFullGCComplete();
            }
            fs.Close();
        }

        if ( b == null)
        {
            // Error
            return null;
        }

        BitmapSizeOptions options = BitmapSizeOptions.FromEmptyOptions();
        BitmapSource bs = null;
        try
        {
            bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
               b.GetHbitmap(),
               IntPtr.Zero,
               Int32Rect.Empty,
               options);
        }
        catch (Exception)
        {
            GC.Collect();
            GC.WaitForFullGCComplete();
        }
        return bs;
    }

至于您的“解冻”问题,您不能在初始项上执行,但可以剥离一个解冻的克隆,

我真诚地怀疑内存异常是否来自于Freeze()调用,因为这实际上没有分配任何内存

我很确定你有GDI漏洞。。。在调用CreateBitmapSourceFromHBitmap()后,必须对创建的位图调用DeleteObject。。。但是,因为您将GetHbitmap()作为参数调用,所以您没有要删除的句柄

试试这个:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

...

IntPtr hObject = b.GetHbitmap();
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
           hObject,
           IntPtr.Zero,
           Int32Rect.Empty,
           options);

DeleteObject(hObject);

亨克是对的,你不应该强迫GC收集。。。它并没有真正帮助您,因为您并没有真正释放任何要收集的内容(您释放的唯一内容必须通过DeleteObject()清理)

我们谈论的是多少1378x2000图像?即使你修复了GDI漏洞,这些都是大图像,会很快耗尽内存

Curtisk是对的,你不能解冻,你必须克隆…但这样做的时候你会分配内存。只是为了警告你


我想在64位下运行不是一个选项…

GC.Collect in a catch block不能作为答案。我知道..这是try&error,因为我不知道如何修复此漏洞您尝试处理的图像有多大?1378x2000..由于某些缩放功能,我无法缩小它们。什么是
DeleteObject()
?我是否缺少引用?它是对
gdi32.dll
dll(Windows的一部分)中调用的声明。声明上方的属性告诉.NET在运行时导入该dll,并告诉它在何处查找
DeleteObject()
调用代码。请注意,您调用的是非托管代码,其中包含许多含义。链接到更多信息: