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.
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.
Procedure
Step 1. Create your own C# application for Webcam
Create a simple Forms Application in C#:
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.)
Drag & drop DynamicDotNetTwain to your form to create a
control:
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:
Step
3. Add code for the buttons
Initiation
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()
{
InitializeComponent();
dynamicDotNetTwain1.IfShowUI = true;
dynamicDotNetTwain1.SupportedDeviceType = EnumSupportedDeviceType.SDT_WEBCAM;
dynamicDotNetTwain1.IfThrowException = true;
}
Select
a camera and start the video stream
private void btnSelect_Click(object sender, EventArgs e)
{
try
{
dynamicDotNetTwain1.SelectSource();
dynamicDotNetTwain1.SetVideoContainer(pictureBox1);
dynamicDotNetTwain1.OpenSource();
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)
{
MessageBox.Show(exp.Message);
}
}
Change
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
private void btnAcquire_Click(object sender, EventArgs e)
{
try
{
dynamicDotNetTwain1.EnableSource();
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
Use
the internal Image editor of Dynamic .NET TWAIN to edit the image:
private void btnEdit_Click(object sender, EventArgs e)
{
dynamicDotNetTwain1.ShowImageEditor();
}
You can add more editing
features if you like.
Save
the acquired images locally
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)
{
dynamicDotNetTwain1.SaveAllAsPDF(saveFileDialog.FileName);
}
}
Dynamic .NET
TWAIN provides internal encoder for BMP, JPEG, PNG, (multi-page) TIF and (multi-page)
PDF.
Remove Images
private void btnRemove_Click(object sender, EventArgs e)
{
dynamicDotNetTwain1.RemoveAllImages();
}
Upload Images
private void btnUpload_Click(object sender, EventArgs e)
{
string serverName = "localhost"; 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.
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 = "";
try
{
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);
sw1.Write(strExc);
sw1.Close();
}
Response.Write(strExc);
}
}
}
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 support@dynamsoft.com.