相机捕获时内存泄漏,高分辨率

时间:2015-03-12 06:42:29

标签: wpf emgucv webcam-capture

内存达到一个极端,应用程序停止工作。我在计时器中打电话给Runcamera。对于分辨率640 * 480但有1920 * 1080的问题。我错过了什么?

  public void RunCamera() 
    {
       imgWeb.Visibility = Visibility.Visible;

      capture1.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 1920);
      capture1.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 1080);
       currentFrame = capture1.QueryFrame();
       imgWeb.Source = ToBitmapSource(currentFrame);
    }

下面给出的ToBitmapSource defenition

public static BitmapSource ToBitmapSource(IImage image)
    {
        BitmapSource bs = null;
        using (System.Drawing.Bitmap source = image.Bitmap)
        {
            try
            {

                IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap

                bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                   ptr,
                   IntPtr.Zero,
                   Int32Rect.Empty,
                   System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                DeleteObject(ptr); //release the HBitmap
            }
            catch (Exception ex)
            {
                GC.Collect();
                GC.WaitForFullGCComplete();
            }
            return bs;

        }
    }

1 个答案:

答案 0 :(得分:0)

捕获的最佳方式......我的旧想法太复杂了......找到了stockoverflow中的代码示例

using (Image<Bgr, byte> frame = capture1.QueryFrame())
       {
           if (frame != null)
           {
               using (var stream = new MemoryStream())
               {
                   // My way to display frame 
                   frame.Bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

                   BitmapImage bitmap = new BitmapImage();
                   bitmap.BeginInit();
                   bitmap.StreamSource = new MemoryStream(stream.ToArray());
                   bitmap.EndInit();
                   imgWeb.Source = bitmap;
               };
           }
       }