Webcam Capture in VB.NET Source code and Tutorial

Webcam Capture in VB.NET Source code and Tutorial

Problem

Create a Windows Form Application program in Visual Basic.Net that will allow users to capture and save an image from a webcam.

Description

This tutorial will allow the user to select a video device, a video resolution and video input. Once set upped, the user can click the capture button to take an image from the video device or video input then the user can either save the image or reject it.

Before the tutorial the following are required to start:

  1. Microsoft Visual Studio 2008 – Above
  2. AForge.dll
  3. AForge.Video.DirectShow.dll
  4. AForge.Video.dll

The tutorial starts here:

  1. Download AForge.dll, AForge.Video.DirectShow.dll, AForge.Video.dll from the internet and save it to your preferred directory.
  2. Open Microsoft Visual Studio 2012
  3. Select a New Project on the File menu.
  4. Select Visual Basic, Windows Form Application then click OK.
  5. On the solution Panel, right click the “name of your project” then click add reference
Webcam Capture in VB.NET Source code and Tutorial - Step 5
Webcam Capture in VB.NET Source code and Tutorial – Step 5
  1. On the reference manager, Click Browse on the left panel and check the MessagingToolkit.Barcode.dll, if it doesn’t appear, then click the Browse button on the bottom. Search and select for MessagingToolkit.Barcode.dll then click “Add”.
Webcam Capture in VB.NET Source code and Tutorial - Step 6a
Webcam Capture in VB.NET Source code and Tutorial – Step 6a
Webcam Capture in VB.NET Source code and Tutorial - Step 6b
Webcam Capture in VB.NET Source code and Tutorial – Step 6b
  1. Click “Ok” and we are ready now to design our form
  2. We need to design our form by the following controls:
  • 3 Command Button – 1 button that can capture an image from the webcam, another button rejects the captured image and the last button that can save the captured image.
  • 1 Picture Box – picture box where the captured image is displayed.
  1. We will also name our form controls in this way:
  • cmdno is the name of the button for save.
  • cmdok is the name of the button for save.
  • pbcapture is the name of the button to generate the QR Code.
  • PictureBox2 is the name of the picture box where the QR Code will be displayed.
  1. This is how we design the form. (Feel free to layout your own)
Webcam Capture in VB.NET Source code and Tutorial - Step 10
Webcam Capture in VB.NET Source code and Tutorial – Step 10

­­­

Figure 1. Design of the Form

  1. Import the .dll by pasting the code above the text editor

Code here

Imports AForge
Imports AForge.Video
Imports AForge.Video.DirectShow
Imports System.IO

End Code

Code Explanation:

The codes above are needed, to enable the camera.

  1. below Public Class Form1, paste the following code:

Code here

Dim CAMARA As VideoCaptureDevice
Dim BMP As Bitmap
Dim Cap As String = "Capture"

End Code

Code Explanation:

The code will declare the variables above that to be used in the next series of codes.

  1. In the Form1_FormClosing event handler, paste the following codes

Code here

Try
    CAMARA.Stop()
    ' PictureBox2.Image = Nothing
Catch ex As Exception
    Me.Dispose()
    'PictureBox2.Image = Nothing
End Try

End Code

Code Explanation:

The code will terminate the camera and return everything back to the initial state then closes the program.

  1. In the Form1_Load event handler, paste the following codes

Code here

cmdno.Visible = False
cmdok.Visible = False
Dim CAMARAS As VideoCaptureDeviceForm = New VideoCaptureDeviceForm()
        If CAMARAS.ShowDialog() = DialogResult.OK Then
            CAMARA = CAMARAS.VideoDevice
            AddHandler CAMARA.NewFrame, New NewFrameEventHandler(AddressOf CAPTURAR)
            CAMARA.Start()
        Else
            Me.Close()
        End If

End Code

Code Explanation:

The code will show a video capture device form dialogue, then once the user choses a video resolution, video device and video input and clicks “OK”, then it will proceed to the main form, else the form/program closes.

  1. Declare a Private Sub, naming it “CAPTURAR” then/or paste the following codes (it should be looked like below).

Code here

Private Sub CAPTURAR(sender As Object, eventArgs As NewFrameEventArgs)
        BMP = DirectCast(eventArgs.Frame.Clone(), Bitmap)
        pbcaptureimage.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap)
End Sub

End Code

Code Explanation:

This is the code for the capture button and also the code will capture a frame from the video device as a bitmap.

  1. Double click the pbcapture button then paste the following.

Code here

If Cap = "Capture" Then
            cmdno.Visible = True
            cmdok.Visible = True
            pbcapture.Visible = False
            CAMARA.Stop()
            Cap = "Start"
ElseIf Cap = "Start" Then
            Dim CAMARAS As VideoCaptureDeviceForm = New VideoCaptureDeviceForm()
            CAMARA.Start()
            cmdno.Visible = False
            cmdok.Visible = False
            Cap = "Capture"
End If
End Sub

End Code

Code Explanation:

The code will display the captured image to the Picture box then it will hide the capture button and shows the check and ex button.

  1. Double click the x/no button then paste the following.

Code here

Dim CAMARAS As VideoCaptureDeviceForm = New VideoCaptureDeviceForm()
CAMARA.Start()
cmdno.Visible = False
cmdok.Visible = False
pbcapture.Visible = True
Cap = "Capture"

End Code

Code Explanation:

The code will reject the captured image and re-initiate the camera. It will also hide the check/yes or ex/no button from the form and shows the capture button.

  1. Double click the check/yes button then paste the following.

Code here

Dim SD As New SaveFileDialog
        SD.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        SD.FileName = "Visual Capture " & Date.Now.ToString("MM/dd/yy")
        SD.SupportMultiDottedExtensions = True
        SD.AddExtension = True
        SD.Filter = "PNG File|*.png"
        If SD.ShowDialog() = DialogResult.OK Then
            Try
                pbcapture image.Image.Save(SD.FileName, Imaging.ImageFormat.Png)
                Dim CAMARAS As VideoCaptureDeviceForm = New VideoCaptureDeviceForm()
                CAMARA.Start()
                cmdno.Visible = False
                cmdok.Visible = False
                pbcapture.Visible = True
                Cap = "Capture"
            Catch ex As Exception
            End Try
        Else
            Dim CAMARAS As VideoCaptureDeviceForm = New VideoCaptureDeviceForm()
            CAMARA.Start()
            cmdno.Visible = False
            cmdok.Visible = False
            pbcapture.Visible = True
            Cap = "Capture"
        End If

End Code

Code Explanation:

The code will save the captured image from the picture box into a special directory (the desktop) then It will hide the check/yes or ex/no button from the form and shows the capture button. It also re-initiates the camera.

Webcam Capture in VB.NET Source code and Tutorial - Final Output
Webcam Capture in VB.NET Source code and Tutorial – Final Output

Author:

Name: Charlie Devera
Email Address: charliedevera000@gmail.com

Free Download Source code (WebCam Capture in VB.NET)

You may visit our facebook page for more information, inquiries and comments.

Hire our team to do the project.

, , ,

Post navigation