How to Implement a Webcam Application in VB.NET
Dynamic .NET TWAIN is an .NET library supporting DirectShow Webcam and TWAIN compatible scanner programming. With the Webcam Capture .Net Component, you can easily and quickly build a WinForms or WPF application with webcam image capture in Visual Basic .NET (VB.NET) or C#.
This article will illustrate how to make a webcam application in VB.NET using Dynamic .NET TWAIN SDK.
Create a VB Project
Create a new WinForms project
- File -> New Project
- Templates -> Visual Basic -> Windows Forms Application
Add the Dynamic .NET TWAIN Component
Add reference to the project
- Right click on the project root -> Add Reference
- Click Browse to add the relevant library
Add the Dynamic .NET TWAIN component to the Toolbox
- Right click on the Toolbox panel -> Choose Items
- .NET Framework component -> Click Browse to add the relevant library (DynamicDotNetTWAIN.dll)
- Check DynamicDotNetTwain
Create the User Interface
Drag some Widgets from the Toolbox to the form, including some buttons, a picture box, some radio buttons and the Dynamic .NET TWAIN component, just like the picture below.
Select Webcams
Source selection is the first step. For the Webcam application, the source type is defined as EnumSupportedDeviceType.SDT_WEBCAM. Double click the Select Source button to add the following code:
Try
dynamicDotNetTwain1.SelectSource() ‘ select source
Dim en As EnumSupportedDeviceType
en = dynamicDotNetTwain1.SupportedDeviceType
dynamicDotNetTwain1.IfShowUI = True
dynamicDotNetTwain1.SetVideoContainer(Me.pictureBox1) ‘ display preview in PictureBox
dynamicDotNetTwain1.OpenSource() ‘ make source work
Catch exp As Exception
MessageBox.Show(exp.Message)
End Try
After selecting the source, the preview is displayed in the picture box.
Capture Image from Webcam
As source is ready, acquiring the image is a piece of cake. Double click Acquire button to add following code:
Try
dynamicDotNetTwain1.IfThrowException = True
dynamicDotNetTwain1.EnableSource() ‘ acquire image
Catch exp As Exception
MessageBox.Show(exp.Message)
End Try
Remove Image
Removing the image is as convenient as acquiring it. Double click the Remove All button to add the following code:
dynamicDotNetTwain1.RemoveAllImages() ‘ remove image
Save Image
Use a system API to create a dialog for saving image. Double click the Save button to add the following code:
If dlgFileSave.ShowDialog() = Windows.Forms.DialogResult.Cancel Then ‘ call system dialog
Exit Sub
End If
strFileName = dlgFileSave.FileName ‘ get file name from dialog
dynamicDotNetTwain1.SaveAsBMP(strFileName, dynamicDotNetTwain1.CurrentImageIndexInBuffer)
‘ save image to disk
With just a few steps, a simple Webcam application is made.
Get Sample Code Now
To explore more powerful functionalities, download the 30-day free trial of Dynamic .NET TWAIN.