1

I'm ashamed, but I'll ask anyway: which is the most straightforward way to take a picture from a webcam with its default size and color-depth?

I started playing with DirectShowLib but I'm clueless... Can anyone guive me a guidance?

Imports DirectShowLib

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        PictureBox1.Image = Nothing

        Dim Cam As DsDevice = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice).FirstOrDefault

        If Cam IsNot Nothing Then

            Stop
            ' ... what now?

        End If

    End Sub

End Class
Roman R.
  • 68,205
  • 6
  • 94
  • 158
VBobCat
  • 2,527
  • 4
  • 29
  • 56

2 Answers2

2

DirectShowLib's samples DxSnap, DxWebCam (C#) show how to capture from a webcam. There is also VB.NET DxLogoVB there, it does a different thing but is still good if you also look for some DriectShow.NET + VB.NET sample code.

DxWebCam:

A poor man's web cam program. This application runs as a Win32 Service.
It takes the output of a capture graph, turns it into a stream of JPEG files, and sends it thru TCP/IP to a client application.

DxSnap:

Use DirectShow to take snapshots from the Still pin of a capture device. Note the MS encourages you to use WIA for this, but if you want to do in with DirectShow and C#, here's how.

Note that this sample will only work with devices that output uncompressed video as RBG24. This will include most webcams, but probably zero tv tuners.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • I tried to run DxSnap, each time I run it computer beeps as if I'd removed the webcam from usb. Can't figure out what is going wrong, sorry. – VBobCat Jul 27 '17 at 18:39
  • My point, overall, is that the library are trying to use comes with sample projects. It is worth to look into them even if they don't seem to run out of the box. The code gives hints how to achieve capture related tasks. – Roman R. Jul 27 '17 at 18:42
  • Yes, I think you are right, but... Is it only me or anyone else finds outrageous one need 200 lines of code to take a picture from a WebCam in .NET 4.5? – VBobCat Jul 27 '17 at 18:44
0

Ok, the best I was able to do depends on AForge.Controls and AForge.Video.DirectShow and is working with this code, which I intend to improve (it is a rough scratch - but takes the picture):

Public Class Form1
    Private Sub Test() Handles Me.Load
        Dim rf As New RolleiFlex
        PictureBox1.Image = rf.Click
    End Sub
End Class

Public Class RolleiFlex

    Public Sub New()
        Dim vDevices = New AForge.Video.DirectShow.FilterInfoCollection(FilterCategory.VideoInputDevice)
        Devices = vDevices.Cast(Of FilterInfo).Select(
            Function(fi) New Device With {
            .Name = fi.Name,
            .MonikerString = fi.MonikerString}).ToArray
        SelectedDevice = Devices.FirstOrDefault
        vDevices = Nothing
    End Sub

    Public Devices As Device()

    Public Property SelectedDevice As Device

    Public Class Device
        Public Property Name As String
        Public Property MonikerString As String
    End Class

    Public Function Click() As Bitmap
        Dim retBmp As Bitmap
        Dim camera As New AForge.Controls.VideoSourcePlayer
        camera.VideoSource = New VideoCaptureDevice(SelectedDevice.MonikerString)
        camera.Start()
        Do
            retBmp = camera.GetCurrentVideoFrame
            If retBmp Is Nothing Then Threading.Thread.Sleep(100)
        Loop While retBmp Is Nothing
        camera.Stop()
        camera.Dispose()
        camera = Nothing
        Return retBmp
    End Function

End Class
VBobCat
  • 2,527
  • 4
  • 29
  • 56