如何在WPF中清除内存 [英] How to clear memory in WPF

查看:90
本文介绍了如何在WPF中清除内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,我正在从kinect捕获帧并对它们进行一些实时处理,我需要显示位图,所以我将它们转换为bmapsource并传递给image.source:Bitmap bmap =新的位图(640,480,System.Drawing.Imaging.PixelFormat.Format24bppRgb); BitmapSource bmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmap.GetHbitmap(),IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions()); image.source = bmapSource;



但是当我在2分钟后处理15FPS时,我得到了这部分的Out of memory错误。无论如何在每个过程之后清除内存?或者是否有任何其他方式在wpf中显示bmap?



提前致谢:)

解决方案

试试这个:



[ ^ ]



希望它有所帮助:)

嗨Niloufar

多么巧合。很高兴再次在codeproject上看到同样的问题。我在StackOverflow上回答了你的问题:)。所以我再次在这里发布。但是这次,我将分开我的代码块:



 //在你的类中声明
[System.Runtime.InteropServices .DllImport(gdi32.dll)]
public static extern bool DeleteObject(IntPtr hObject);
IntPtr bmp;

//你的帧循环
{
if(bmp!= null)
{
DeleteObject(bmp);
}
位图bmap =新位图(640,480,System.Drawing.Imaging.PixelFormat
.Format24bppRgb);
bmp = bmap.GetHbitmap();
BitmapSource bmapSource = System.Windows.Interop.Imaging。
CreateBitmapSourceFromHBitmap(bmp,IntPtr.Zero,Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
bmap.Dispose();
bmap = null;

image.Source = bmapSource;
}


I'm doing a project and I'm capturing frames from kinect and do some real-time process on them, I need to display bitmaps so I'm converting them to bmapsource and pass to image.source: Bitmap bmap = new Bitmap(640, 480, System.Drawing.Imaging.PixelFormat.Format24bppRgb); BitmapSource bmapSource= System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmap.GetHbitmap(),IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); image.source = bmapSource;

But as I'm processing 15FPS after 2 minute I get the error "Out of memory" for this part. Is there anyway to clear the memory after each process? or isthere any other way to display the bmap in wpf?

Thanks in advance :)

解决方案

Try this:

Local image caching with custom Image control in WPF[^]

hope it helps :)


Hi Niloufar
What a coincidence. Nice to see your same question again on codeproject. I've answered your question on StackOverflow :). so I just post it here again. But this time, I'll separate my code block:

// declare in your class
            [System.Runtime.InteropServices.DllImport("gdi32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);
            IntPtr bmp;

// your frame loop
{
                if (bmp != null)
                {
                    DeleteObject(bmp);
                }
                Bitmap bmap = new Bitmap(640, 480, System.Drawing.Imaging.PixelFormat
                                                         .Format24bppRgb);
                bmp = bmap.GetHbitmap();
                BitmapSource bmapSource = System.Windows.Interop.Imaging.
                  CreateBitmapSourceFromHBitmap(bmp, IntPtr.Zero, Int32Rect.Empty,
                                                BitmapSizeOptions.FromEmptyOptions());
                bmap.Dispose();
                bmap = null;

                image.Source = bmapSource;
}


这篇关于如何在WPF中清除内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆