English 中文(简体)
3. 地图和薄膜
原标题:Bitmap and hBitmap memory Leak

I capture images from my web camera to Image control in WPF with WebCam_Capture.dll. For every captured frame i have event called webCam_imageCaptured. It calls every time when i receive new image from web camera. In this event I call method LoadBitmap to get hBitmap and receive BitmapSource to put this captured picture to the Image control. Here is code of this method:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr handle);
public BitmapSource bs;
public IntPtr ip;
public BitmapSource LoadBitmap(System.Drawing.Bitmap source)
{
   ip = source.GetHbitmap();
   bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty,
   System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
   DeleteObject(ip);
   return bs;
}

这里是活动守则:

private void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
    //_frameImage is Image WPF control.
     _frameImage.Source = LoadBitmap((System.Drawing.Bitmap)e.WebCamImage);   
}

我在此有漏!! RAM装满了1支大不列颠和有时更多新娘! <代码>DeleteObject(ip)并非每次使用。 当RAM为1个或更多英镑时,它释放或显示我的记忆错误。 如何解决这一问题?

最佳回答

如果没有宣传员的帮助,甚至看不到你的法典的其余部分,我几乎可以保证,“ak”在这里:

_frameImage.Source = LoadBitmap((System.Drawing.Bitmap)e.WebCamImage);

这套方法肯定是十分迅速的,你不是<条码>Dispose()ing of You Bitmaps。 在你之后,理事会胜过了清理的时间,并将定稿人召集到每一条/code>上,以便你摆脱记忆。

您在重新加入后,须在<代码>Bitmap上打电话。 <代码>BitmapSource提供了图像数据的任何链接,因此不必提供数据。 我将努力这样做:

private void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
    using( Bitmap b = e.WebCamImage )
    {
         _frameImage.Source = LoadBitmap((System.Drawing.Bitmap)e.WebCamImage);   
    }
}

当然,这假定,在这次活动中处理笔记本是一件有效的事(即任何其他人是否处理这一事件?) 页: 1 相反,使你能够确保没有其他客户依赖<条码>Bitmap

That is of course if you wrote the API to being with. If not you could write a wrapper to make sure your code never actually sees a Bitmap and deals only with WPF image types.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签