How to Implement a Webcam Application in VB.NET

Implement a Webcam Application in VB.NET using Dynamic .NET TWAIN

Dynamic .NET TWAIN is an .NET library supporting DirectShow Webcam and TWAIN compatible scanner programming. With the .NET webcam capture component, you can easily and quickly build a WinForms or WPF application with webcam image capture in Visual Basic .NET (VB.NET) or C#.

Try the SDK for Free

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

  1. File -> New Project
  2. Templates -> Visual Basic -> Windows Forms Application

winform webcam project

Add the Dynamic .NET TWAIN Component

Add reference to the project

  1. Right click on the project root -> Add Reference
  2. Click Browse to add the relevant library

twain library

Add the Dynamic .NET TWAIN component to the Toolbox

  1. Right click on the Toolbox panel -> Choose Items
  2. .NET Framework component -> Click Browse to add the relevant library (DynamicDotNetTWAIN.dll)
  3. Check DynamicDotNetTwain

twain component

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.

webcam UI

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

twain webcam source

After selecting the source, the preview is displayed in the picture box.

webcam preview display

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

acquire image

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.

If you are interested in embedding webcam capture in a browser-based application, you may take a look at Dynamsoft Camera SDK.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com