EmguCV ImageGrabbed事件WPF应用 [英] EmguCV ImageGrabbed Event WPF App

查看:532
本文介绍了EmguCV ImageGrabbed事件WPF应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码有疑问

namespace MyApp
{    
    public partial class PhotoWindow : Window
    {
        private Capture _capture;

        public PhotoWindow ()
        {
            InitializeComponent();    
            _capture = new Capture();

            if (_capture != null)
            {
                //<Image> in XAML
                CaptureSource.Width = 150;
                CaptureSource.Height = 180;

                _capture.ImageGrabbed += ProcessFrame;
                _capture.Start();                
            }

            Activated += (s, e) => _capture.Start();
            Closing += (s, e) =>
            {
                if (_capture == null) return;
                _capture.Stop();                
                _capture.Dispose();
            };
        }

        private void ProcessFrame(object sender, EventArgs e)
        {
            try
            {
                Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();                   
                CaptureSource.Source = Helper.ToBitmapSource(frame);
            }
            catch (Exception exception)
            {

                System.Windows.MessageBox.Show(exception.ToString());
            }
        }

    }
}

当我运行应用程序时,在行CaptureSource.Source = Helper.ToBitmapSource(frame);

上收到异常System.InvalidOperationException: The thread that this call can not access this object because the owner is another thread

我能解决这个问题吗?

解决方案

似乎是从Capture的后台线程引发了ImageGrabbed事件,因为您的处理程序正在该线程中运行,而不是在窗口的UI线程中运行. >

您可以使用Dispatcher在控件的UI线程中调用代码.

CaptureSource.Dispatcher.Invoke(() =>
{
   Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();                   
   CaptureSource.Source = Helper.ToBitmapSource(frame);
});

I have problems with the following code

namespace MyApp
{    
    public partial class PhotoWindow : Window
    {
        private Capture _capture;

        public PhotoWindow ()
        {
            InitializeComponent();    
            _capture = new Capture();

            if (_capture != null)
            {
                //<Image> in XAML
                CaptureSource.Width = 150;
                CaptureSource.Height = 180;

                _capture.ImageGrabbed += ProcessFrame;
                _capture.Start();                
            }

            Activated += (s, e) => _capture.Start();
            Closing += (s, e) =>
            {
                if (_capture == null) return;
                _capture.Stop();                
                _capture.Dispose();
            };
        }

        private void ProcessFrame(object sender, EventArgs e)
        {
            try
            {
                Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();                   
                CaptureSource.Source = Helper.ToBitmapSource(frame);
            }
            catch (Exception exception)
            {

                System.Windows.MessageBox.Show(exception.ToString());
            }
        }

    }
}

when I run the application I get the exception System.InvalidOperationException: The thread that this call can not access this object because the owner is another thread on line CaptureSource.Source = Helper.ToBitmapSource(frame);

As I can solve this?

解决方案

It seems that ImageGrabbed event is raised from Capture's background thread and because of that your handler is running in that thread rather then the UI thread of your window.

You can use the Dispatcher to invoke code in the control's UI thread.

CaptureSource.Dispatcher.Invoke(() =>
{
   Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();                   
   CaptureSource.Source = Helper.ToBitmapSource(frame);
});

这篇关于EmguCV ImageGrabbed事件WPF应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆