Question Webcam Capture | Select Video Source Dialog Box Appearing

crystaluz

Well-known member
Joined
Feb 17, 2009
Messages
55
Programming Experience
Beginner
Hello there.. I'm using this code to capture an image from built-in webcam. It worked. But when I run the code for second time, the Select Video Source dialog box started appearing. But when the laptop is rebooted, the code will work fine. This is the code when button is clicked in order to start the webcam preview.

VB.NET:
 Private Sub OpenPreviewWindow()
        Dim iHeight As Integer = picCapture.Height
        Dim iWidth As Integer = picCapture.Width



        '
        ' Open Preview window in picturebox
        '
        hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, _
            480, picCapture.Handle.ToInt32, 0)

        
            ' Connect to device
            '
            If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
                '
                'Set the preview scale
                '
                SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)

                '
                'Set the preview rate in milliseconds
                '
                SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)

                '
                'Start previewing the image from the camera
                '
                SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)

                '
                ' Resize window to fit in picturebox
                '
                SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
                        SWP_NOMOVE Or SWP_NOZORDER)

                btnSave.Enabled = True
                btnStop.Enabled = True
                btnStart.Enabled = False
            Else
                '
                ' Error connecting to device close window
                ' 
                DestroyWindow(hHwnd)

                btnSave.Enabled = False
            End If

    End Sub


I googled some solution and found one, but I don't understand. :

Finally I Found a solution for this
The problem happen in Windows 7 / 8

First you need this API function
Private Declare Function GetTickCount Lib "kernel32" () As Long

Then... after you call capCreateCaptureWindowA() you have to wait 1 second processing events, (note: sleep don't work the same)

IniTime = GetTickCount()
While GetTickCount() < (IniTime + 1000)
DoEvents
Wend

then you call WM_CAP_DRIVER_CONNECT.. and THAT's IT

Any help would be appreciated. Thank you :)
 
Equivalent VB.Net code is:
        Dim time = Date.Now         
        Do Until (Date.Now - time).TotalMilliseconds > 1000
            Application.DoEvents()
        Loop

It's not a good practice to loop like that to chew time, even if you allow events to process, but for just once rarely it could pass I guess. Better would be to use for example a Timer or threading.
 
Thank you for your reply. I've noticed something here. Whenever I spam the button which triggered the OpenPreviewWindow() at least 3 times fast click, it will activate the webcam. I just don't understand why it's like that.
 
Back
Top