高分辨率相机捕获内存泄漏
Memory leak on camera capture for high resolution
内存达到极限,应用程序停止运行。我在计时器中调用了 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;
}
}
捕获的最佳方式...我的旧想法太复杂...在 Whosebug 中找到代码示例
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;
};
}
}
内存达到极限,应用程序停止运行。我在计时器中调用了 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;
}
}
捕获的最佳方式...我的旧想法太复杂...在 Whosebug 中找到代码示例
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;
};
}
}