显示方式1:OpenCvSharp窗体 var window = new Window(“cv”);
显示方式2:Windows 窗体 PictureBox控件
using OpenCvSharp; using OpenCvSharp.Extensions; 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; /// <summary> /// 打开摄像头 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpenVideo_Click(object sender, EventArgs e) { // 定义图像捕捉方式 从摄像头 , 注意 Windows下需要选择 VideoCaptureAPIs.DSHOW var capture = new VideoCapture(0, VideoCaptureAPIs.DSHOW); if (!capture.IsOpened()) return; capture.XI_OffsetX = 0; // 以左上角为起点 坐标X capture.XI_OffsetY = 0; // 以左上角为起点 坐标Y capture.FrameWidth = 640; // 宽 capture.FrameHeight = 480; // 高 capture.AutoFocus = true; const int sleepTime = 10; var window = new Window("cv"); // Mat作为图像的存储容器 var image = new Mat(); while (true) { capture.Read(image); if (image.Empty()) break; // 显示 window.ShowImage(image); // Windows窗体PictureBox加载 picboxDest.Image = image.ToBitmap(); int flag = Cv2.WaitKey(sleepTime); if (flag >= 0) { break; } // 可以防止界面停止响应 Application.DoEvents(); } }