【问题标题】:Can't refresh pictureBox via playing video using AForge无法通过使用 AForge 播放视频来刷新图片框
【发布时间】:2014-10-22 09:13:52
【问题描述】:

我使用 AForge 库打开视频文件

private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog opd = new OpenFileDialog();

        if (opd.ShowDialog() == DialogResult.OK)
        {
            FileVideoSource videoSource = new FileVideoSource(opd.FileName);
            videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
            videoSource.Start();
        }
    }

    private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap bitmap = eventArgs.Frame;

        pictureBox1.Image = bitmap;
        pictureBox1.Refresh();
    }

但在“pictureBox1.Refresh”行中,我有一个异常“跨线程操作无效:控制 'pictureBox1' 从创建它的线程以外的线程访问” 这是什么?

【问题讨论】:

    标签: c# multithreading visual-studio-2010 video aforge


    【解决方案1】:

    作为异常提示,控件只能在创建它们的线程内进行操作。

    这里有创建表单和控件的主 UI 线程,以及来自 AForge 的视频阅读器线程。 video_NewFrame中运行的代码在reader线程上,不能直接调用控件上的方法。

    See here 了解更多详情。您必须按照以下方式做一些事情:

    private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
    
        this.Invoke((MethodInvoker)delegate
        {
            pictureBox1.Image = bitmap;
            pictureBox1.Refresh();
        });      
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 2016-05-17
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      相关资源
      最近更新 更多