Click here to Skip to main content
14,738,144 members
Articles » Third Party Products » Product Showcase » General
Posted 6 Nov 2012


30 bookmarked

Webcam in C#: Easiest Way to Capture Images from Your Webcams

6 Nov 2012CPOL
Get the solution to integrate Image Grab from webcams in a .NET application. Achieve the functionality in just a few lines of code.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Webcams, as an easy & cost-effective way to acquire images from video stream, is widely used in the business world. Below are some common uses of the device:

  • In hospitals, take a photo of a patient and upload it together with his/her med file onto the server.
  • In banks, representatives scan customer’s ID using a webcam.

Although Webcams can be very useful in scenarios like the above, it’s not a simple task to develop an application from scratch for image acquiring, editing, saving & uploading.

Image 1

To save your time & energy, Dynamsoft developed the SDK Dynamic .NET TWAIN which can handle the above tasks all by itself. Based on the .NET framework, you can develop your own application in C# with a few lines of code.

If you’d like to try out the combination, you can download the sample code and the 30-day free trial from this article.

Key Features

  • Simple. Designed especially for C# & VB.NET, Dynamic .NET TWAIN is very easy to use. You can get your job done in a few lines of code.
  • Powerful. Dynamic .NET TWAIN takes care of image acquiring, editing, saving & uploading in a number of ways. You can always find the best fit for your application.
  • Flexible. Both WinForm and WPF are supported. Besides the webcams, the .NET control is also compatible with scanners and other imaging devices.


Step 1. Create your own C# application for Webcam

Create a simple Forms Application in C#:

Image 2

Add Dynamic .NET TWAIN to the Toolbox: Assume you have it installed, you can browse and add DynamicDotNetTWAIN.dll at C:\Program Files (x86)\Dynamsoft\Dynamic .NET TWAIN 4.1 Trial. (You download the trial version from Dynamic .NET TWAIN 30-Day Free Trial if you haven’t installed it on your development machine.)

Image 3

Image 4

Drag & drop DynamicDotNetTwain to your form to create a control: Image 5

Step 2. Add the video container and the necessary buttons

Add a picturebox to the form as the video container. Add buttons for selecting cameras, acquiring images, saving/uploading images and removing images. Create a dropdown box to hold the resolutions. More functions can be added according to your requirements. See below:

Image 6

Step 3. Add code for the buttons


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dynamsoft.DotNet.TWAIN.Enums;
using Dynamsoft.DotNet.TWAIN.WebCamera;

namespace UseWebcamInCSharp
    public partial class Form1 : Form
        public Form1()
            dynamicDotNetTwain1.IfShowUI = true;
            dynamicDotNetTwain1.SupportedDeviceType = EnumSupportedDeviceType.SDT_WEBCAM;
            dynamicDotNetTwain1.IfThrowException = true;

Select a camera and start the video stream

//Select a source
private void btnSelect_Click(object sender, EventArgs e)
        //List the source name and resolutions
        txtSourceName.Text = dynamicDotNetTwain1.CurrentSourceName;
        int count = dynamicDotNetTwain1.ResolutionForCamList.Count;
        for (int j = 0; j < count; j++)
            string tempHeight = dynamicDotNetTwain1.ResolutionForCamList[j].Height.ToString();
            string tempWidth = dynamicDotNetTwain1.ResolutionForCamList[j].Width.ToString();
            string tempResolution = tempWidth + "X" + tempHeight;
            comboResolution.Items.Insert(j, tempResolution);
            comboResolution.SelectedIndex = 0;
    catch (Exception exp)

Change resolution

//Change the resolution
private void comboResolution_SelectedIndexChanged(object sender, EventArgs e)
    dynamicDotNetTwain1.ResolutionForCam = dynamicDotNetTwain1.ResolutionForCamList[comboResolution.SelectedIndex];

The above is the simplest one. According to your requirements, besides resolution, you can also adjust the brightness, contrast, sharpness, etc. before actually acquiring the images.

Acquire an image from the video stream

//Acquire an image
private void btnAcquire_Click(object sender, EventArgs e)
    catch (Exception exp)

Use the internal Image editor of Dynamic .NET TWAIN to edit the image:

//Edit an image
private void btnEdit_Click(object sender, EventArgs e)

You can add more editing features if you like.

Save the acquired images locally

//Save images
private void btnSave_Click(object sender, EventArgs e)
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.FileName = "test.pdf";
    saveFileDialog.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
    if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)

Dynamic .NET TWAIN provides internal encoder for BMP, JPEG, PNG, (multi-page) TIF and (multi-page) PDF.

Remove Images 

//Remove images
private void btnRemove_Click(object sender, EventArgs e)

Upload Images

//Upload all images
private void btnUpload_Click(object sender, EventArgs e)
    string serverName = "localhost"; //please update the server name accordingly
    string actionPagePath = "/UseWebcamInCSharp/SaveToFile.aspx";
    dynamicDotNetTwain1.HTTPUploadAllThroughPostAsPDF(serverName, actionPagePath, "test.pdf");

Sometimes, you might want to upload the scanned images to your system. That includes web server, SQL Server, Oracle, SharePoint, etc. And some requirements would be uploading images and extra info to different places, for example, images to the web server, and the corresponding image IDs and comments to the SQL Server database. These can be easily achieved by Dynamic .NET TWAIN. In this sample, the simplest one is provided. The scanned images will be uploaded to the local web server as one multi-page PDF:

On the server side, you need to add an action page to receive and process (save) the uploaded file. If you are trying the sample code downloaded from this article, you can copy the UseWebcamInCSharp_IIS folder to your web server.

// SaveToFile.aspx.cs
using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
public partial class SaveToFile : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        String strExc = "";
            HttpFileCollection files = HttpContext.Current.Request.Files;
            HttpPostedFile uploadfile = files["RemoteFile"];
            uploadfile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(".") + "/ImageScanned/" + uploadfile.FileName);
        catch (Exception exc)
            strExc = exc.ToString();
            String strField1Path = HttpContext.Current.Request.MapPath(".") + "/" + "log.txt";
            if (strField1Path != null)
                StreamWriter sw1 = File.CreateText(strField1Path);

Get Samples

The detailed sample code can be downloaded from this article. In the meantime, you can get 30-day free trial of Dynamic .NET TWAIN too:

Dynamic .NET TWAIN Free trial for 30 days

If you have any questions, you can contact our support team at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rachel Jia
United States United States
No Biography provided

Comments and Discussions

QuestionFYI - this code uses a 3rd party object PinPopular
Member 767847218-Jan-13 11:49
MemberMember 767847218-Jan-13 11:49 
QuestionAnother alternative way to capture Images from your webcams Pin
K.Raja Moorthy12-Nov-12 16:48
MemberK.Raja Moorthy12-Nov-12 16:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.