Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / Win32

Fabrika LAB: WebCam Video

Rate me:
Please Sign up or sign in to vote.
5.00/5 (20 votes)
22 May 2013CPOL3 min read 112.6K   12.4K   40  
Free and easy way to access a web camera by using the Aforge library.
Imports AForge.Video.DirectShow

Public Class frmCamera

    Private videoDevices As FilterInfoCollection
    Private videoCapabilities As VideoCapabilities()

    Private videoDevice As VideoCaptureDevice

    Private Sub EnumerateVideoDevices()
        ' enumerate video devices
        videoDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)

        If videoDevices.Count <> 0 Then
            ' add all devices to combo
            For Each device As FilterInfo In videoDevices
                ComboBoxSources.Items.Add(device.Name)
            Next
        Else
            ComboBoxSources.Items.Add("No DirectShow devices found")
        End If

        ComboBoxSources.SelectedIndex = 0
    End Sub

    Private Sub EnumerateVideoModes(device As VideoCaptureDevice)
        ' get resolutions for selected video source
        Me.Cursor = Cursors.WaitCursor

        ComboBoxModes.Items.Clear()

        Try
            videoCapabilities = videoDevice.VideoCapabilities

            For Each capabilty As VideoCapabilities In videoCapabilities
                If Not ComboBoxModes.Items.Contains(capabilty.FrameSize) Then
                    ComboBoxModes.Items.Add(capabilty.FrameSize)
                End If
            Next

            If videoCapabilities.Length = 0 Then
                ComboBoxModes.Items.Add("Not supported")
            End If

            ComboBoxModes.SelectedIndex = 0
        Finally
            Me.Cursor = Cursors.[Default]
        End Try

    End Sub

    Private Sub frmCamera_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        EnumerateVideoDevices()
    End Sub

    Private Sub ComboBoxSources_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxSources.SelectedIndexChanged
        If videoDevices.Count <> 0 Then
            videoDevice = New VideoCaptureDevice(videoDevices(ComboBoxSources.SelectedIndex).MonikerString)
            EnumerateVideoModes(videoDevice)
        End If
    End Sub

    Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
        CameraStart()
    End Sub

    Private Sub ButtonStop_Click(sender As Object, e As EventArgs) Handles ButtonStop.Click
        CameraStop()
    End Sub

    Private Sub CameraStart()
        If videoDevice IsNot Nothing Then
            If (videoCapabilities IsNot Nothing) AndAlso (videoCapabilities.Length <> 0) Then
                videoDevice.DesiredFrameSize = DirectCast(ComboBoxModes.SelectedItem, Size)
            End If
            VideoSourcePlayer1.VideoSource = videoDevice
            VideoSourcePlayer1.Start()
        End If
    End Sub

    Private Sub CameraStop()
        If VideoSourcePlayer1.VideoSource IsNot Nothing Then
            ' stop video device
            VideoSourcePlayer1.SignalToStop()
            VideoSourcePlayer1.WaitForStop()
            VideoSourcePlayer1.VideoSource = Nothing
        End If
    End Sub

    Private Sub VideoSourcePlayer1_NewFrame(sender As Object, ByRef image As Bitmap) Handles VideoSourcePlayer1.NewFrame
        ' add overlay
        Dim g As Graphics = Graphics.FromImage(image)
        g.DrawString("Augmented reality?", New Font("Arial", 16), Brushes.Black, New Rectangle(10, 10, 200, 50))
        g.Dispose()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) CENT SI d.o.o.
Slovenia Slovenia
I have strange hobby - programming.
While I am not programming I am tasting some good beers Wink | ;)

Comments and Discussions