connect to webcam using Aforge[VB.NET] 04-20-2013, 08:35 PM
#1
i will show you how to access into webcam using Aforge..
first of all download this if you don't have it
Aforge.controls.dll,Aforge.video.dll,Aforge.videodirectshow.dll
and add it to reference
next add :
1 button -(i named it as "button")
1 label - (i named it "label")
1 combobox - (i named it "combobox")
1 VideoSourcePlayer = ( inamed it"VideoSourcePlayer")
note : if "VideoSourcePlayer" not showing in your toolbox,right click in toolbox>choose items>under ".NET framework components click browse>navigate to your "Aforge.controls.dll" directory>click ok
next add this code:
hope this things can help some one :ok:
Downlod Source code
first of all download this if you don't have it
Aforge.controls.dll,Aforge.video.dll,Aforge.videodirectshow.dll
and add it to reference
next add :
1 button -(i named it as "button")
1 label - (i named it "label")
1 combobox - (i named it "combobox")
1 VideoSourcePlayer = ( inamed it"VideoSourcePlayer")
note : if "VideoSourcePlayer" not showing in your toolbox,right click in toolbox>choose items>under ".NET framework components click browse>navigate to your "Aforge.controls.dll" directory>click ok
next add this code:
Code:
Imports AForge.Video
Imports AForge.Video.DirectShow
Public Class Form1
Dim VideoCaptureSource As VideoCaptureDevice
Dim VideoDevices As New FilterInfoCollection(FilterCategory.VideoInputDevice)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MySingleDevice As FilterInfo
If VideoDevices.Count = 0 Then
ComboBox.Items.Add("No Video Devices")
Else
For Each MySingleDevice In VideoDevices
ComboBox.Items.Add(MySingleDevice.Name)
Next
End If
ComboBox.SelectedIndex = 0
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
VideoSourcePlayer.SignalToStop()
VideoSourcePlayer.WaitForStop()
VideoDevices = Nothing
VideoCaptureSource = Nothing
End Sub
Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox.SelectedIndexChanged
If ComboBox.SelectedItem <> "No Video Devices" Then
If VideoSourcePlayer.IsRunning = True Then
VideoSourcePlayer.SignalToStop()
VideoSourcePlayer.WaitForStop()
End If
VideoCaptureSource = New VideoCaptureDevice(VideoDevices(ComboBox.SelectedIndex).MonikerString)
VideoSourcePlayer.VideoSource = VideoCaptureSource
VideoSourcePlayer.Start()
End If
End Sub
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
Try
Dim strFilename As String
strFilename = "Image-" & Format(Now, "yyyy-MMM-dd, H-mm-ss-fff") & ".jpg"
If VideoSourcePlayer.IsRunning = True Then
VideoSourcePlayer.GetCurrentVideoFrame.Save(System.Windows.Forms.Application.StartupPath & "\" & strFilename, System.Drawing.Imaging.ImageFormat.Jpeg)
Label.Text = "Image Saved : " & strFilename
End If
Catch ex As Exception
MessageBox.Show("Try taking snapshot again when video image is visible.", "Cannot Save Image", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Classhope this things can help some one :ok:
Downlod Source code

![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
