Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple camera capture in Winforms

I just want to preview then capture a snapshot from a device camera (webcam) in a WinForms C# application.

I've used this: WebEye WebCameraControl, but it seems to fail on some machines/cameras. The description implies there's loads more out there, but I can't find any on NuGet that are for WinForms.

Any recommendations? I feel like I'm missing something obvious, like a built-in windows control that will just do it!


Edit:
In trying to add OpenCVSharp this is what I get: enter image description here

like image 940
noelicus Avatar asked Jun 12 '18 08:06

noelicus


1 Answers

Try OpenCVSharp. A bit of a code snippet with a PictureBox and a 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;
    }
}

Full Code

 public partial class Form1 : Form
    {
        // Create class-level accesible variables
        VideoCapture capture;
        Mat frame;
        Bitmap image;
        private Thread camera;
        bool isCameraRunning = false;

        // Declare required methods
        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;
                }
            }
        }

        public Form1()
        {
            InitializeComponent();
            CaptureCamera();
            button1.Text = "Stop";
            isCameraRunning = true;
        }

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

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            try
            {
                capture.Release();
                camera.Abort();
            }
            catch (Exception ex)
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (isCameraRunning)
            {
                Bitmap snapshot = new Bitmap(pictureBox1.Image);


                snapshot.Save(string.Format(@"D:\image.jpg", Guid.NewGuid()), ImageFormat.Jpeg);
                capture.Release();
                camera.Abort();
                this.Close();
            }
            else
            {
                Console.WriteLine("Cannot take picture if the camera isn't capturing image!");
            }
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
        }
    }

enter image description here

Here SaveButton save image to D:\location if you feel any error try run as admin

you can find solution here:- working example

like image 78
PepitoSh Avatar answered Oct 12 '22 01:10

PepitoSh