Simple camera capture in Winforms

13,630

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

Share:
13,630

Related videos on Youtube

noelicus
Author by

noelicus

Deep down, I still like C the best but have had a lot of fun with C#, React, Java & Javascript over the last few years. I also like picking up hobbies. I continuously like chocolate, drums, guitar, lego. Hobbies I'd like to resume are RC helicopters, gliding, go-carting and climbing.

Updated on September 14, 2022

Comments

  • noelicus
    noelicus over 1 year

    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