0

Memory reached to one extreme and application stopped working. I called the Runcamera in a timer. for the resolution 640*480 but having problem with 1920*1080. What am I missing?

  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 given below

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;

        }
    }
Jagan
  • 79
  • 1
  • 16
  • Use JetBrains dot Memory to profile your application. Or Visual Studio Ultimate's Memory Debugger, or the cumbersome WinDbg and http://thinkexception.blogspot.de/2010/06/tool-to-bompare-two-windbg-dumpheap.html and https://github.com/Seikilos/MemComparer – Samuel Mar 12 '15 at 06:47
  • @Samuel thanks... stil those links explains how its happening which i already know, i didnt find useful to solve the issue.... I entirely change the capture format which solved the issue – Jagan Mar 17 '15 at 09:39

1 Answers1

0

Best way for capture... old idea of mine was too complicated.... found the sample of code in 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;
               };
           }
       }
Jagan
  • 79
  • 1
  • 16