Winforms 中的简单相机捕获

noe*_*cus 7 c# camera winforms opencvsharp

我只想预览然后从 WinForms C# 应用程序中的设备摄像头(网络摄像头)捕获快照。

我用过这个:WebEye WebCameraControl,但它似乎在某些机器/相机上失败。描述暗示有更多的负载,但我在 NuGet 上找不到任何适用于 WinForms 的内容。

有什么建议吗?我觉得我错过了一些明显的东西,比如一个可以做到这一点的内置 Windows 控件!


编辑:
在尝试添加 OpenCVSharp 这就是我得到的: 在此处输入图片说明

Pep*_*oSh 10

试试OpenCVSharp。一些带有图片框和按钮的代码片段:

VideoCapture capture;
Mat frame;
Bitmap image;
private Thread camera;
bool isCameraRunning = 0;

private void CaptureCamera() {
    camera = new Thread(new ThreadStart(CaptureCameraCallback));
    camera.Start();
}

private void CaptureCameraCallback() {

    frame = new Mat();
    capture = new VideoCapture(0);
    capture.Open(0);

    if (capture.IsOpened()) {
        while (isCameraRunning) {

            capture.Read(frame);
            image = BitmapConverter.ToBitmap(frame);
            if (pictureBox1.Image != null) {
                pictureBox1.Image.Dispose();
            }
            pictureBox1.Image = image;
        }
    }
}

private void button1_Click(object sender, EventArgs e) {
    if (button1.Text.Equals("Start")) {
        CaptureCamera();
        button1.Text = "Stop";
        isCameraRunning = true;
    }
    else {
        capture.Release();
        button1.Text = "Start";
        isCameraRunning = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • OpenCvSharp3-AnyCPU.3.4.1.20180319 (2认同)