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
Hello, sorry for the question but with AFORGE is possible to memorize after combobox the 3 variable as X Y Z for the VIDEO DEVICE / RESOLUTION and video input??
posted 535 days a go
dzosoft
Hi Anto, I apologize for the delay in replying
you could use this code:
Dim videoCapDevFrm As VideoCaptureDeviceForm = New VideoCaptureDeviceForm()
Dim videocomboitems As New ComboBox
Dim resolutioncomboitems As New ComboBox
Dim devicesCombo As New ComboBox
videoCapDevFrm.Show()
For i = 0 To videoCapDevFrm.Controls.Count - 1
If TypeOf (videoCapDevFrm.Controls.Item(i)) Is Windows.Forms.GroupBox Then
For k = 0 To videoCapDevFrm.Controls.Item(i).Controls.Count - 1
If TypeOf (videoCapDevFrm.Controls.Item(i).Controls.Item(k)) Is Windows.Forms.ComboBox Then
Dim s As String
s = videoCapDevFrm.Controls.Item(i).Controls.Item(k).Name
If (s = "videoInputsCombo") Then
Dim cmbbox As ComboBox = videoCapDevFrm.Controls.Item(i).Controls.Item(k)
For Each item In cmbbox.Items
videocomboitems.Items.Add(item.ToString)
Next
End If
If (s = "videoResolutionsCombo") Then
Dim cmbbox As ComboBox = videoCapDevFrm.Controls.Item(i).Controls.Item(k)
For Each item In cmbbox.Items
resolutioncomboitems.Items.Add(item.ToString)
Next
End If
If (s = "devicesCombo") Then
Dim cmbbox As ComboBox = videoCapDevFrm.Controls.Item(i).Controls.Item(k)
For Each item In cmbbox.Items
devicesCombo.Items.Add(item.ToString)
Next
End If
End If
Next
End If
Next