C# winform控件+EmguCv 打开指定视频文件并在ImageBox中进行播放展示

本文参考:浅入浅出EmguCv(三)EmguCv打开指定视频
打开视频的思路跟打开图片的思路是一样的,只不过视频是由一帧帧图片组成,因此,打开视频的处理程序有一个连续的获取图片并逐帧显示的处理过程
一.控件
在面板中拖入一个button和一个ImageBox(注意ImageBox控件为EmguCv控件)
在这里插入图片描述
二.代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;
using Emgu.CV.ML;


namespace EMGUCVtest
{
    public partial class CameraCapture : Form
    {
        //declaring global variables
        private VideoCapture capture;
        private bool isProcess = false;
     
      
        public CameraCapture()
        {
            InitializeComponent();
        }

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

            if (dilog.ShowDialog() == DialogResult.OK || dilog.ShowDialog() == DialogResult.Yes)
            {
                ImageViewer viewer = new ImageViewer();
                VideoCapture capture = new VideoCapture(dilog.FileName);

                Application.Idle += new EventHandler(delegate (object sendero, EventArgs eo)
                {
                    Mat frame = capture.QueryFrame();
                    System.Threading.Thread.Sleep(1);
                    imageBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                    imageBox1.Image = frame;
                });
            }
        }
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C#使用EmguCV打开摄像头,并在Windows窗体应用程序展示摄像头的视频流,可以使用以下代码: 1. 引用EmguCV库,将其添加到项目引用。 2. 在代码创建一个`Capture`对象,并指定摄像头的索引号。例如,打开默认的摄像头: ``` Capture capture = new Capture(); ``` 或者,指定摄像头的索引号: ``` Capture capture = new Capture(1); ``` 3. 创建一个`ImageBox`控件,用于显示摄像头的视频流。 ``` ImageBox imageBox = new ImageBox(); this.Controls.Add(imageBox); ``` 4. 在每个循环迭代,使用`Capture`对象的方法获取视频帧,并将其显示在`ImageBox`控件。 ``` Mat frame = capture.QueryFrame(); imageBox.Image = frame; ``` 5. 在处理完所有帧后,释放`Capture`对象: ``` capture.Dispose(); ``` 完整的示例代码如下: ``` Capture capture = new Capture(); ImageBox imageBox = new ImageBox(); this.Controls.Add(imageBox); while (true) { Mat frame = capture.QueryFrame(); imageBox.Image = frame; } capture.Dispose(); ``` 在上面的代码,我们使用`Capture`类打开默认的摄像头,并创建一个`ImageBox`控件,用于显示摄像头的视频流。在每个循环迭代,我们使用`QueryFrame`方法获取视频帧,并将其赋值给`ImageBox`控件的`Image`属性。由于`ImageBox`控件是专门为显示OpenCV的图像而设计的控件,因此在将`Mat`对象赋值给`ImageBox`控件的`Image`属性时,不需要进行格式转换和数据复制。 需要注意的是,在Windows窗体应用程序,更新UI控件的操作必须在主线程进行,否则会抛出异常。如果需要在其他线程更新UI控件,可以使用`Invoke`方法或`BeginInvoke`方法来实现。 希望我的回答能够帮到您。如果您还有其他问题,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值