Last Updated on 2020-08-02
Webcams, as an easy & cost-effective way to take images from video stream, is widely used in the business world. To save your time & energy to integrate webcam capture to your WinForms application, Dynamsoft developed the Dynamic .NET TWAIN SDK. With it, you can develop a web camera capture module in C# with a few lines of code. If you use VB.NET, please check out this blog post.
Step 1. Create your C# application for Webcam Capture
Create a simple Windows Forms Application in C#. Add Dynamic .NET TWAIN to the Toolbox: Assume you have it installed, you can browse and add DynamicDotNetTWAIN.dll in the installation folder of Dynamic .NET TWAIN (C:\Program Files (x86)\Dynamsoft\Dynamic .NET TWAIN Trial\Redistributable\).
Step 2. Add .NET TWAIN component, picture box and the necessary buttons
Drag & drop DynamicDotNetTwain to your form to create a control: Add a picturebox to the form as the video container. Add buttons for selecting cameras, acquiring images, and removing images. See below:
Step 3. Add code for the buttons
Select a web camera and start the video stream
[csharp]//Select a source private void btnSelect_Click(object sender, EventArgs e) { try { dynamicDotNetTwain1.SelectSource(); dynamicDotNetTwain1.SetVideoContainer(pictureBox1); dynamicDotNetTwain1.OpenSource(); //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) { MessageBox.Show(exp.Message); } }[/csharp]
Acquire an image from the video stream
[csharp]//Acquire an image private void btnAcquire_Click(object sender, EventArgs e) { try { dynamicDotNetTwain1.EnableSource(); } catch (Exception exp) { MessageBox.Show(exp.Message); } }[/csharp]
Save the acquired images locally
Dynamic .NET TWAIN provides internal encoder for BMP, JPEG, PNG, (multi-page) TIF and (multi-page) PDF.
[csharp]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); } }[/csharp]
To build your own webcam capture module using the SDK, download the 30-day free trial of Dynamic .NET TWAIN.
This artice was originally posted on CodeProject.