Se necesita el código anterior para habilitar la cámara.
debajo de Public Class frmWebCam, pegue el siguiente código:
Atenuar la cámara como VideoCaptureDevice
Dim bmp como mapa de bits
Dim cap As String = "Capturar"
El código declarará las variables que se usaron en nuestro programa.
En el controlador de eventos frmWebCam_FormClosing, pegue el siguiente código
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
El código terminará la cámara y devolverá todo al estado inicial y luego cerrará el programa.
En el controlador de eventos frmWebCam_Load, pegue el siguiente código
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
El código mostrará un diálogo de formulario de dispositivo de captura de video, luego, una vez que el usuario elija una resolución de video, dispositivo de video y
entrada de video y hace clic en "Aceptar", luego se procederá al formulario principal, de lo contrario, se cerrará el formulario/programa.
Declare un Sub privado, llámelo "dzoCapture" y luego/o pegue los siguientes códigos (debería verse como a continuación).
Private Sub dzoCapture(sender As Object, eventArgs As NewFrameEventArgs)
bmp = DirectCast(eventArgs.Frame.Clone(), Bitmap)
pbcaptureimage.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap)
End Sub
Este es el código para el botón de captura y también el código capturará un cuadro del dispositivo de video como un mapa de bits.
Haga doble clic en el botón btnCapture y luego pegue lo siguiente.
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
El código mostrará la foto capturada en el cuadro Imagen, luego ocultará el botón de captura y
muestra el botón de confirmar y cancelar.
Haga doble clic en el botón cmdok y luego pegue lo siguiente.
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
El código rechazará la imagen capturada y reiniciará la cámara. También ocultará la
botón confirmar/sí o cancelar/no desde y muestra el botón de captura.
Haga doble clic en el botón cmdok y luego pegue lo siguiente.
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
El código guardará la foto tomada del cuadro de imagen en un directorio seleccionado
también reinicia la cámara.
Código completo
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