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读取摄像头并显示的实现示例

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。