Live Feed from WebCam using C#

This blog explains how to get live feed from web cam using c# and Accord Dot net framework.The Accord.Net Framework is a machine learning framework combined with audio and image processing libraries which is completely written in c#. To download accord.net framework follow this link.


By Assuming you installed Accord.Net framework successfully we move on creating our form. Let’s open your visual studio and create a New Windows Form Project. Now your window will look like this.


Under Solution Explorer Right Click on References->Add References. A Reference Manager window will open in that Click on “Browser” button. A browser file dialog will open navigate to the folder where your accord.net framework is installed and select "Accord.dll", "Accord.Video.dll", "Accord.Video.DirectShow.dll" reference files and click “Add” and the Ok.

Add References


Then Add Picturebox, button and combo box to the form and design it like this.


Then paste the following code in the appropriate event handlers. 

Source Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Accord.Video.DirectShow;
using Accord.Video;

namespace CaptureCamera
{
    public partial class Form1 : Form
    {
        FilterInfoCollection cFilterInfoCollection;
        VideoCaptureDevice cVideoCaptureDevice;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                cFilterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                foreach (FilterInfo cFilterInfo in cFilterInfoCollection)
                {
                    comboBox1.Items.Add(cFilterInfo.Name);
                }
                comboBox1.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception:" + ex.ToString());
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                cVideoCaptureDevice = new VideoCaptureDevice(cFilterInfoCollection[comboBox1.SelectedIndex].MonikerString);
                cVideoCaptureDevice.NewFrame += new NewFrameEventHandler(getFrame);
                cVideoCaptureDevice.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception:" + ex.ToString());
            }
        }

        private void getFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap BsourceImage = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = BsourceImage;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            cVideoCaptureDevice.SignalToStop();
        }
    }
}


Output ScreenShot:



Download Source Code



No comments :

Post a Comment