c# - 使用后台 worker 高效写入 GUI 线程

标签 c# multithreading winforms webcam

我正在使用 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/

相关文章:

c# - C# GUI 命名约定的最佳实践?

c# - ASP.NET 5 Web 项目中的 Entity Framework Core - 自动生成 LINQ 过滤器查询

c# - 学习 C#,数学方程未按预期结果

c# - 使用非 VB6 对象在 VS2008 中调试 VB6

c# - MVC 4 如何正确地将数据从 Controller 传递到 View

c++ - 奇怪的 pthread_mutex_t 行为

c# - 更改 Y 轴的比例

java - 我将我的线程设置为比其他线程更高的优先级,但 CPU 时间似乎是随机的?

java - 从线程返回字符串 Android Java

c# - 窗体。使用 BindingNavigator 时如何使 DataGridView 写入单元格值