1,首先要安装OpenCvSharp,使用cv2的函数获取设备,并将图像转换到Bitmap

  using OpenCvSharp;
  using OpenCvSharp.Extensions;    
 
 
 
     void run_cap()
        {
            Mat src = new Mat();
            FrameSource frame = Cv2.CreateFrameSource_Camera(0);
            while (f1)
            {
 
                frame.NextFrame(src);
                
                Bitmap bitmap = BitmapConverter.ToBitmap(src);
                
 
                Invalidate();
                pictureBox1.Invalidate();
 
 
                imgshow = bitmap;
                
            }
 
        }

2,在调用时开启线程,可以在主函数内调用,也可在按钮内调用

      private void button1_Click(object sender, EventArgs e)
        {
 
           // Image imgshow0 = Image.FromFile("src.jpg");
          //  pictureBox1.Image = imgshow0;
            Thread threadA = new Thread(run_cap);
            threadA.Start();
 
        }

3,在form框架调用重绘函数将每一帧重绘到pictureBox1

this.pictureBox1.Paint += PictureBox1_Paint;

4,函数如下:

       private void PictureBox1_Paint(object sender, PaintEventArgs e)
        {
           if (f)
            {
 
                           
                Invalidate();pictureBox1.Invalidate();    
                e.Graphics.DrawImage(imgshow,0,0);
 
            }
 
        }

5,运行如下:

C#借助OpenCvSharp读取摄像头并显示的实现示例

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章