我正在使用 BackgroundWorker 从摄像机中提取视频并将其写入 WinForms 窗体上的 PictureBox。在 BW 线程中,我只是从相机中拉出一帧,将其放入 PictureBox,休眠,然后继续:
while (CaptureThreadRunning)
{
Thread.Sleep(5);
Image tmp = Camera.GetFrame(500);
pbCameraFeed.Image = tmp;
//this.BeginInvoke(new MethodInvoker(delegate { pbCameraFeed.Image = Camera.GetFrame(500); }));
}
问题是最终在我的屏幕上调整或移动表单将抛出异常 System.InvalidOperationException
并显示消息 Additional information: Object is currently in use elshere.
在线 pbCameraFeed.Image = tmp;
我假设库试图在我的 while 循环的同时绘制与 PictureBox 相关的东西,所以我切换到上面注释掉的 this.BeginInvoke
实现。不幸的是,这大大降低了我的帧率。我在一台非常慢的 Mini PC 上运行这段代码,这可能是导致该问题的原因。
我真正想要的是一种使用图像可靠地更新我的 GUI 的方法,它不会将我的帧速率降低近一半。还有其他标准方法可以做到这一点吗? BW 线程似乎非常适合此应用程序,但我是否遗漏了什么?
谢谢
最佳答案
如果我是你,我肯定会检查 AForge.NET 框架。无需重新发明轮子;)
http://www.aforgenet.com/framework/samples/video.html
AForge.NET is an open source C# framework designed for developers and researchers in the fields of Computer Vision and Artificial Intelligence - image processing, neural networks, genetic algorithms, fuzzy logic, machine learning, robotics, etc.
The framework is comprised by the set of libraries and sample applications, which demonstrate their features:
AForge.Imaging - library with image processing routines and filters;
AForge.Vision - computer vision library;
AForge.Video - set of libraries for video processing;
...
关于c# - 使用后台 worker 高效写入 GUI 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31687235/