Capture image from WebCam and display in PictureBox in Windows Application

Last Reply one month ago By priyajsr

Posted 2 months ago

Can  i have sample code reference to

capture image from webcam and display in picture box  and save in database in c# windows application.

I want to some downloaded file reference where, i can download dll and used in my project

Posted one month ago Modified on one month ago

Hi priyajsr,

Check the below sample.

First Download the dll from the below link and add its reference in your project.

http://www.mediafire.com/?dxrnc433pfrp7mu

Form Design

C#

using System;
using System.IO;
using System.Windows.Forms;
using WebCam_Capture;

namespace _173314_WebCam_PictureBox
{
    public partial class mainWinForm : Form
    {
        public mainWinForm()
        {
            InitializeComponent();
        }
        WebCam webcam;
        private void mainWinForm_Load(object sender, EventArgs e)
        {
            webcam = new WebCam();
            webcam.InitializeWebCam(ref imgVideo);
        }

        private void bntStart_Click(object sender, EventArgs e)
        {
            webcam.Start();
        }

        private void bntStop_Click(object sender, EventArgs e)
        {
            webcam.Stop();
        }

        private void bntContinue_Click(object sender, EventArgs e)
        {
            webcam.Continue();
        }

        private void bntCapture_Click(object sender, EventArgs e)
        {
            imgCapture.Image = imgVideo.Image;
        }

        private void bntSave_Click(object sender, EventArgs e)
        {
            SaveImageCapture(imgCapture.Image);
        }

        private void bntVideoFormat_Click(object sender, EventArgs e)
        {
            webcam.ResolutionSetting();
        }

        private void bntVideoSource_Click(object sender, EventArgs e)
        {
            webcam.AdvanceSetting();
        }

        class WebCam
        {
            private WebCamCapture webcam;
            private System.Windows.Forms.PictureBox _FrameImage;
            private int FrameNumber = 30;
            public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
            {
                webcam = new WebCamCapture();
                webcam.FrameNumber = ((ulong)(0ul));
                webcam.TimeToCapture_milliseconds = FrameNumber;
                webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
                _FrameImage = ImageControl;
            }

            void webcam_ImageCaptured(object source, WebcamEventArgs e)
            {
                _FrameImage.Image = e.WebCamImage;
            }

            public void Start()
            {
                webcam.TimeToCapture_milliseconds = FrameNumber;
                webcam.Start(0);
            }

            public void Stop()
            {
                webcam.Stop();
            }

            public void Continue()
            {
                // change the capture time frame.
                webcam.TimeToCapture_milliseconds = FrameNumber;

                // resume the video capture from the stop.
                webcam.Start(this.webcam.FrameNumber);
            }

            public void ResolutionSetting()
            {
                webcam.Config();
            }

            public void AdvanceSetting()
            {
                webcam.Config2();
            }
        }

        public static void SaveImageCapture(System.Drawing.Image image)
        {
            SaveFileDialog s = new SaveFileDialog();
            s.FileName = "Image";   // Default file name.
            s.DefaultExt = ".Jpg";  // Default file extension.
            s.Filter = "Image (.jpg)|*.jpg";    // Filter files by extension.

            // Show save file dialog box.
            if (s.ShowDialog() == DialogResult.OK)
            {
                // Save Image.
                string filename = s.FileName;
                FileStream fstream = new FileStream(filename, FileMode.Create);
                image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
                fstream.Close();
            }
        }
    }
}

Screenshot

While Capturing

Saved Image