0

内存达到一个极端,应用程序停止工作。我在计时器中调用了 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 定义如下

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;

        }
    }
4

1 回答 1

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;
               };
           }
       }
于 2015-03-17T09:42:04.850 回答