• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1607
  • Last Modified:

How to get webcam pictures every x seconds

Hello,

I encounter following problem:
I have 2 pictureboxes, one containing the webcam image itself.  So a continuous stream of images.
And another that has to take a picture every few seconds of the webcam and analyse it.

I am using this to support my webcam:

Public Const WM_USER = &H400
Public Const WM_CAP_START = WM_USER
Public Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
Public Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
Public Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
Public Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Public Const WM_CAP_GET_STATUS = WM_CAP_START + 54
Public Const WM_CAP_EDIT_COPY = WM_CAP_START + 30

Public Const WS_CHILD = &H40000000
Public Const WS_VISIBLE = &H10000000

Private mCapHwnd As Long
Private Const CONNECT As Long = 1034
Private Const DISCONNECT As Long = 1035
Private Const GET_FRAME As Long = 1084
Private Const COPY As Long = 1054

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias "capCreateCaptureWindowA" (ByVal lpszWindowName As String, ByVal dwStyle As Long, ByVal X As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hwndParent As Long, ByVal nID As Long) As Long
Function capDriverConnect(ByVal lwnd As Long, ByVal i As Integer) As Boolean
   capDriverConnect = SendMessage(lwnd, WM_CAP_DRIVER_CONNECT, i, 0)
End Function
Function capDriverDisconnect(ByVal lwnd As Long) As Boolean
   capDriverDisconnect = SendMessage(lwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0)
End Function
Function capPreview(ByVal lwnd As Long, ByVal f As Boolean) As Boolean
   capPreview = SendMessage(lwnd, WM_CAP_SET_PREVIEW, f, 0)
End Function
Function capPreviewRate(ByVal lwnd As Long, ByVal wMS As Integer) As Boolean
   capPreviewRate = SendMessage(lwnd, WM_CAP_SET_PREVIEWRATE, wMS, 0)
End Function
Function capGetStatus(ByVal lwnd As Long, ByVal s As Long, ByVal wSize As Integer) As Boolean
   capGetStatus = SendMessage(lwnd, WM_CAP_GET_STATUS, wSize, s)
End Function
Function capEditCopy(ByVal lwnd As Long) As Boolean
   capEditCopy = SendMessage(lwnd, WM_CAP_EDIT_COPY, 0, 0)
End Function

Public Function LoadWebcam(ByVal formnr As Integer)

    On Error Resume Next
         
    If (breedte < 1 Or hoogte < 1) Then
        breedte = 640
        hoogte = 480
    End If
   
    mCapHwnd = capCreateCaptureWindow("WebcamCapture", WS_VISIBLE Or WS_CHILD, 0, 0, breedte, hoogte, Main.webcam.hwnd, 0)
   SendMessage mCapHwnd, COPY, 0, 0

    capDriverConnect mCapHwnd, 0
    capPreviewRate mCapHwnd, 66
    capPreview mCapHwnd, True
    Clipboard.Clear
End Function
Public Function close_webcam()
    On Error Resume Next
    SendMessage mCapHwnd, DISCONNECT, 0, 0
End Function

To copy a webcam picture to my other picture box every x seconds I am using following function:

Public Function copieer_webcam(ByVal formnr As Integer)
    Clipboard.Clear
    SendMessage mCapHwnd, GET_FRAME, 0, 0
    SendMessage mCapHwnd, COPY, 0, 0
    If (formnr = 1) Then
        Main.picMeting.Picture = Clipboard.GetData
    End If
    'Call close_webcam
    'Call LoadWebcam(formnr)
End Function

Now the problem is, when a webcam image is copied to the other picture box, the first picturebox (showing the continuous webcam image stream) freezes.  Adding those 2 lines with a ' to copieer_webcam solves half the problem but is a sucky solution.
Can anyone help me to find a correct, fast working solution for my problem?

Thx
0
black_help
Asked:
black_help
  • 2
1 Solution
 
black_helpAuthor Commented:
Thx, but thats not exactly what I need
Id like to know whats wrong with my current code so I can solve it as fast as possible

+ I dont need to save the picture, I need it to be copied to another picturebox while the first webcam picturebox keeps showing the webcam image without any delays.
Of course you can say I can save the current image and afterwards load it, but I believe thats working around the problem instead of solving it.

0
 
iHadiCommented:
While you've got the picture you want to copy in the first picturebox, copy it straight from there to the other picturebox using the famous bitblt API:

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Public Function copieer_webcam(ByVal formnr As Integer)
Dim mCapDC As Long
mCapDC = GetDC(mCapHwnd)
    If (formnr = 1) Then
        BitBlt picMeting.hDC, 0, 0, picMeting.Width, picMeting.Height, mCapDC, 0, 0, vbSrcCopy
        picMeting.Refresh
    End If
End Function
0
 
black_helpAuthor Commented:
Thx, it was all I needed and it seems to work now
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now