Introduction Today many of the application automation requires the process of capturing the image. Here I am going to explain the steps that you need to follow to capture an image from Camera.
Design Guidelines Create a windows application project in C#, Add 2 picture box, 5 buttons and name the button as follows 1) Start 2) Stop 3) Continue ) Click 5) Save and name the PictureBox1 as imgCapture and PictureBox2 as imgSave. For capturing image from Webcam you need to have an API, I am attaching the API which can be used for capturing image from camera. You can find API named "EasyWebcamApi" in attachment, you need to include Webcam_Capture dll in your application for capturing image.
Code Segment
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using WinFormCharpWebCam; namespace Camera_Sample { public partial class Form1 : Form { WebCam webcam; //customized class created for handling the task of capturing image from Webcamera public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { webcam = new WebCam(); webcam.InitializeWebCam(ref imgCam); }
private void btnStart_Click(object sender, EventArgs e) { webcam.Start();
}
private void btnStop_Click(object sender, EventArgs e) { webcam.Stop(); }
private void imgPreview_Click(object sender, EventArgs e) {
}
private void btnContinue_Click(object sender, EventArgs e) { webcam.Continue(); }
private void btnClick_Click(object sender, EventArgs e) { imgPreview.Image = imgCam.Image; }
private void btnSave_Click(object sender, EventArgs e) { SaveFileDialog sdialog = new SaveFileDialog(); sdialog.FileName = "Image";// Default file name sdialog.DefaultExt = ".Jpg";// Default file extension sdialog.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension
// Show save file dialog box // Process save file dialog box results if (sdialog.ShowDialog() == DialogResult.OK) { // Save Image string filename = sdialog.FileName; FileStream fstream = new FileStream(filename, FileMode.Create); imgPreview.Image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg); fstream.Close(); } } } }
Helper Class WebCam.cs is the class which can be used for capturing the image
using System; using System.IO; using System.Text; using WebCam_Capture; using System.Collections.Generic;
namespace CSharpWebCam { //Design by Dharma Iyer 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(); }
internal void InitializeWebCam(System.Windows.Forms.PictureBox imgCam) { throw new NotImplementedException(); } } }
Attachments DLL used for add to reference (44164-5241-WebCam_Capture.dll)
|
| Guest Author: patel archihs 09 Sep 2012 |
file not found error occur when execute the program " webcam.InitializeWebCam(ref imgCam); " at this stage plz help me
|
| Guest Author: patel archish 09 Sep 2012 |
Could not load file or assembly 'WebCam_Capture, Version=1.0.2395.24959, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
error is occur when i run app. at this point " webcam.InitializeWebCam(ref imgCam); " plz help
|
| Guest Author: Tejachowdary 22 Nov 2012 |
"WinFormCharpWebCam" showing error sir... Please reply me immediately..
|
| Guest Author: Girish 30 Nov 2012 |
for this error download and use EasyWebCam Library this dll from below link
http://www.creativecodedesign.com/node/66
|
| Guest Author: Hridya 10 Dec 2012 |
Hi Dharma Iyer,
Would be able to help me. I am getting "Object reference not set to an instance of an object." error.
public void Start() { webcam.TimeToCapture_milliseconds = FrameNumber; webcam.Start(0); }
|
| Guest Author: rajila 30 Dec 2012 |
This error occur when running the form ..Could not load file or assembly 'WebCam_Capture, Version=1.0.2395.24959, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
|
| Guest Author: vidhi 09 Jan 2013 |
just rename both picture box as imgPreview and imgCam. so "webcam.InitializeWebCam(ref imgCam); " this error will be removed and rename your halper class as WinFormCharpWebCam so "WinFormCharpWebCam" this error will be removed..
but still when i run the prog my webcam doesn't start... plz give some solution
|
| Guest Author: guru 14 Mar 2013 |
how to open webcam(laptop) in c# windows form.plz guide me!!!!!
|