在Winforms中捕获简单的相机

时间:2018-06-12 08:48:39

标签: c# winforms camera opencvsharp

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

我用过这个:WebEye WebCameraControl,但似乎在某些机器/相机上失败了。描述意味着有更多的负载,但我在NuGet上找不到任何WinForms。

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

编辑:
在尝试添加OpenCVSharp时,这是我得到的: enter image description here

1 个答案:

答案 0 :(得分:1)

试试OpenCVSharp。一些带有PictureBox和Button的代码片段:

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;
    }
}