C# Camera Capture Example

Ecco Suprastyo
5 min readJul 15, 2019

--

You can read see FULL SECTION this article in this http://camellabs.com/csharp-camera-capture-example/ or Visit my site at camellabs.com.

This article learn about C# Camera Capture Example,this tutorial explains how to integrate webcam to C# application with the framework AForge.Net. Here we will discuss step by step to create a windows form-based application using the C# programming language. In addition to showing webcam images we will also discuss how to take a picture from the webcam to be stored in the folder of your PC.

For that let us immediately practice this tutorial with detail instructions below:

  1. Create a new application project. In Visual Studio, on the menu click File> New > Project. For more details, see the following menu on the display.

2. Then will appear the window New Project like the look below

3. Write down the name of the project that will be created on a field Name. Specify the directory storage project by accessing the field Location. Next, give the name of the solution in the Solution Name. Then click OK.

4. Create a new windows form like the below

5. Next step, Back to windows form and view code to write the following program listing:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Text.RegularExpressions;
using AForge.Video;
using System.Diagnostics;
using AForge.Video.DirectShow;
using System.Collections;
using System.IO;
using System.Drawing.Imaging;
using System.IO.Ports;
using System.Globalization;
using System.Net;
namespace Camera_Record
{
public partial class Form1 : Form
{
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoDevice;
private VideoCapabilities[] snapshotCapabilities;
private ArrayList listCamera = new ArrayList();
public string pathFolder = Application.StartupPath + @"\ImageCapture\";
private Stopwatch stopWatch = null;
private static bool needSnapshot = false;
public Form1()
{
InitializeComponent();
getListCameraUSB();
}
private static string _usbcamera;
public string usbcamera
{
get { return _usbcamera; }
set { _usbcamera = value; }
}
private void button1_Click(object sender, EventArgs e)
{
OpenCamera();
}
#region Open Scan Camera
private void OpenCamera()
{
try
{
usbcamera = comboBox1.SelectedIndex.ToString();
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count != 0)
{
// add all devices to combo
foreach (FilterInfo device in videoDevices)
{
listCamera.Add(device.Name);
}
}
else
{
MessageBox.Show("Camera devices found");
}
videoDevice = new VideoCaptureDevice(videoDevices[Convert.ToInt32(usbcamera)].MonikerString);
snapshotCapabilities = videoDevice.SnapshotCapabilities;
if (snapshotCapabilities.Length == 0)
{
//MessageBox.Show("Camera Capture Not supported");
}
OpenVideoSource(videoDevice);
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
#endregion
//Delegate Untuk Capture, insert database, update ke grid
public delegate void CaptureSnapshotManifast(Bitmap image);
public void UpdateCaptureSnapshotManifast(Bitmap image)
{
try
{
needSnapshot = false;
pictureBox2.Image = image;
pictureBox2.Update();

string namaImage = "sampleImage";
string nameCapture = namaImage + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp";
if (Directory.Exists(pathFolder))
{
pictureBox2.Image.Save(pathFolder + nameCapture, ImageFormat.Bmp);
}
else
{
Directory.CreateDirectory(pathFolder);
pictureBox2.Image.Save(pathFolder + nameCapture, ImageFormat.Bmp);
}
} catch { } } public void OpenVideoSource(IVideoSource source)
{
try
{
// set busy cursor
this.Cursor = Cursors.WaitCursor;
// stop current video source
CloseCurrentVideoSource();
// start new video source
videoSourcePlayer1.VideoSource = source;
videoSourcePlayer1.Start();
// reset stop watch
stopWatch = null;
this.Cursor = Cursors.Default;
}
catch { }
}
private void getListCameraUSB()
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count != 0)
{
// add all devices to combo
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
}
else
{
comboBox1.Items.Add("No DirectShow devices found");
}
comboBox1.SelectedIndex = 0; } public void CloseCurrentVideoSource()
{
try
{
if (videoSourcePlayer1.VideoSource != null)
{
videoSourcePlayer1.SignalToStop();
// wait ~ 3 seconds
for (int i = 0; i < 30; i++)
{
if (!videoSourcePlayer1.IsRunning)
break;
System.Threading.Thread.Sleep(100);
}
if (videoSourcePlayer1.IsRunning)
{
videoSourcePlayer1.Stop();
}
videoSourcePlayer1.VideoSource = null;
}
}
catch { }
}
private void button2_Click(object sender, EventArgs e)
{
needSnapshot = true;
}
private void videoSourcePlayer1_NewFrame_1(object sender, ref Bitmap image)
{
try
{
DateTime now = DateTime.Now;
Graphics g = Graphics.FromImage(image);
// paint current time
SolidBrush brush = new SolidBrush(Color.Red);
g.DrawString(now.ToString(), this.Font, brush, new PointF(5, 5));
brush.Dispose();
if (needSnapshot)
{
this.Invoke(new CaptureSnapshotManifast(UpdateCaptureSnapshotManifast), image);
}
g.Dispose();
}
catch
{ }
}
}
}

6. After you write down the program listings, press the F5 key to run the program and if you successfull the result is:

7. Click button “Start” for begin webcam display, and button “snapshoot” for capture image, result of capture image will display in picture box with label “Image Capture Result”. For Detail look display the below.

8. And result of capture image will save in application directory path, look the below:

We have explained how to make a program C# camera capture image, for those of you who want to download the source code of the program also can. Hopefully this discussion is helpful to you.

You can see C# Camera Capture Example from Github project in Here.

Thank you for reading this article about C# Camera Capture Example, I hope this article is useful for you. Visit My Github about .Net Csharp in Here

--

--