Open Menu dzosoft
Close Menu dzosoft
All About Computer Science and Technology

                twitter instagram facebook pinterest
search on the site
 
 
 

Webcam Capture in VB.NET

 
Webcam Capture in VB.NET
In this tutorial I will show you how to create a program in Visual Basic.Net that will allow users to capture
and save photo from the integrated or connected webcam.
First the user is invited o select a video device, a video resolution and video input.
Once done the user can click the capture button to take a photo from the video device (webcam)
 

Requirements

 
Microsoft Visual Studio community (Freeware) 2019 or with licenze
AForge.dll
AForge.Video.DirectShow.dll
AForge.Video.dll
 

Step 1:

Download AForge.dll, AForge.Video.DirectShow.dll, AForge.Video.dll from here and save it to your preferred directory.
Open Microsoft Visual Studio 2019 community or with licenze
Select a New Project on the File menu.
Select Visual Basic, Windows Form Application then click OK.
On the solution Panel, right click the “name of your project” then click add reference
 
Webcam Capture in VB.NET
 
On the reference manager, Click Browse on the left panel and click “Add” the downloaded dll.
 
Webcam Capture in VB.NET
 
Click “Ok” and we are ready now to design our form
 
Webcam Capture in VB.NET
 
 

Step 2:

We need to design our form by the following controls:
3 Command Button – 1 button that can take a photo from the webcam, another button rejects the taken photo
and the last button that can save the photo.
1 Picture Box – picture box where the captured image is displayed.
We will also name our form controls in this way:
cmdno is the name of the button for cancel.
cmdok is the name of the button for save.
btnCapture is the name of the button to take the photo
PictureBox2 is the name of the picture box where the photo will be displayed.
Imports AForge
Imports AForge.Video
Imports AForge.Video.DirectShow
Imports System.IO

The code above is needed, to enable the camera.
below Public Class frmWebCam, paste the following code:
Dim camera As VideoCaptureDevice
Dim bmp As Bitmap
Dim cap As String = "Capture"

The code will declare the variables that used in our program.
In the frmWebCam_FormClosing event handler, paste the following code
Private Sub frmWebCam_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
   
 Try
	  camera.Stop()
  Catch ex As Exception
	 Me.Dispose()
   End Try
End Sub   

The code will terminate the camera and return everything back to the initial state then closes the program.
In the frmWebCam_Load event handler, paste the following code
Private Sub frmWebCam_Load(sender As Object, e As EventArgs) Handles MyBase.Load
	cmdno.Visible = False
	cmdok.Visible = False
	Dim videoCapDevFrm As VideoCaptureDeviceForm = New VideoCaptureDeviceForm()

	If videoCapDevFrm.ShowDialog() = DialogResult.OK Then
		camera = videoCapDevFrm.VideoDevice
		AddHandler camera.NewFrame, New NewFrameEventHandler(AddressOf dzoCapture)
		camera.Start()
	Else
		Me.Close()
	End If
End Sub

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.
Declare a Private Sub, naming it “dzoCapture” then/or paste the following codes (it should be looked like below).
 Private Sub dzoCapture(sender As Object, eventArgs As NewFrameEventArgs)
        bmp = DirectCast(eventArgs.Frame.Clone(), Bitmap)
        pbcaptureimage.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap)
 End Sub

This is the code for the capture button and also the code will capture a frame from the video device as a bitmap.
Double click the btnCapture button then paste the following.
Private Sub btnCapture_Click(sender As Object, e As EventArgs) Handles btnCapture.Click
        If cap = "Capture" Then
            cmdno.Visible = True
            cmdok.Visible = True
            btnCapture.Visible = False
            camera.Stop()
            cap = "Start"
        ElseIf cap = "Start" Then
            camera.Start()
            cmdno.Visible = False
            cmdok.Visible = False
            cap = "Capture"
        End If
End Sub

The code will display the captured photo to the Picture box then it will hide the capture button and
shows the confirm and cancel button.
Double click cmdok button then paste the following.
 Private Sub cmdno_Click(sender As Object, e As EventArgs) Handles cmdno.Click
        camera.Start()
        cmdno.Visible = False
        cmdok.Visible = False
        btnCapture.Visible = True
        cap = "Capture"
    End Sub

The code will reject the captured image and re-initiate the camera. It will also hide the
confirm/yes or cancel/no button from and shows the capture button.
Double click the cmdok button then paste the following.
	Private Sub cmdok_Click(sender As Object, e As EventArgs) Handles cmdok.Click
        Dim saveDlg As New SaveFileDialog
        saveDlg.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        saveDlg.FileName = "VBNetCapture_" & Date.Now.ToString("MM/dd/yy")
        saveDlg.SupportMultiDottedExtensions = True
        saveDlg.AddExtension = True
        saveDlg.Filter = "PNG File|*.png"
        If saveDlg.ShowDialog() = DialogResult.OK Then
            Try
                pbcaptureimage.Image.Save(saveDlg.FileName, Imaging.ImageFormat.Png)
                camera.Start()
                cmdno.Visible = False
                cmdok.Visible = False
                btnCapture.Visible = True
                cap = "Capture"
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Else
            camera.Start()
            cmdno.Visible = False
            cmdok.Visible = False
            btnCapture.Visible = True
            cap = "Capture"
        End If
    End Sub

The code will save the taken photo from the picture box into a selected directory
it also re-initiates the camera.
 

Complete code

 
Imports AForge
Imports AForge.Video
Imports AForge.Video.DirectShow
Imports System.IO
Public Class frmWebCam
    Dim camera As VideoCaptureDevice
    Dim bmp As Bitmap
    Dim cap As String = "Capture"

    Private Sub frmWebCam_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Try
            camera.Stop()
        Catch ex As Exception
            Me.Dispose()
        End Try
    End Sub
    Private Sub frmWebCam_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cmdno.Visible = False
        cmdok.Visible = False
        Dim videoCapDevFrm As VideoCaptureDeviceForm = New VideoCaptureDeviceForm()

        If videoCapDevFrm.ShowDialog() = DialogResult.OK Then
            camera = videoCapDevFrm.VideoDevice
            AddHandler camera.NewFrame, New NewFrameEventHandler(AddressOf dzoCapture)
            camera.Start()
        Else
            Me.Close()
        End If
    End Sub
    Private Sub dzoCapture(sender As Object, eventArgs As NewFrameEventArgs)
        bmp = DirectCast(eventArgs.Frame.Clone(), Bitmap)
        pbcaptureimage.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap)
    End Sub
    Private Sub pbcapture_Click(sender As Object, e As EventArgs) Handles pbcapture.Click
        If cap = "Capture" Then
            cmdno.Visible = True
            cmdok.Visible = True
            pbcapture.Visible = False
            camera.Stop()
            cap = "Start"
        ElseIf cap = "Start" Then
            camera.Start()
            cmdno.Visible = False
            cmdok.Visible = False
            Cap = "Capture"
        End If
    End Sub

    Private Sub cmdno_Click(sender As Object, e As EventArgs) Handles cmdno.Click
        camera.Start()
        cmdno.Visible = False
        cmdok.Visible = False
        pbcapture.Visible = True
        cap = "Capture"
    End Sub

    Private Sub cmdok_Click(sender As Object, e As EventArgs) Handles cmdok.Click
        Dim saveDlg As New SaveFileDialog
        saveDlg.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        saveDlg.FileName = "VBNetCapture_" & Date.Now.ToString("MM/dd/yy")
        saveDlg.SupportMultiDottedExtensions = True
        saveDlg.AddExtension = True
        saveDlg.Filter = "PNG File|*.png"
        If saveDlg.ShowDialog() = DialogResult.OK Then
            Try
                pbcaptureimage.Image.Save(saveDlg.FileName, Imaging.ImageFormat.Png)
                camera.Start()
                cmdno.Visible = False
                cmdok.Visible = False
                pbcapture.Visible = True
                cap = "Capture"
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Else
            camera.Start()
            cmdno.Visible = False
            cmdok.Visible = False
            pbcapture.Visible = True
            Cap = "Capture"
        End If
    End Sub
End Class

 
Webcam Capture in VB.NET
 
you could download the whole project from here
Leave comment
          

Save nickname and email in this browser for the next time.



All about computer science and technology