Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
Available now!
Buy at Amazon US or
Buy at Amazon UK


» Windows API reference
» Webcam streaming in VB.NET
» Remoting with firewalls
» RSA from first principles
» Key & MouseLogger in .NET
» Networking Resource Kit for .NET
» Migrating VB6 Winsock to VB.NET
» Migrating C++ sockets to C#
» RFC Reference guide
» COM Reference guide
» WMI Reference guide
» SQL stored procedures
» TCP & UDP port reference
» NET Framework reference
» Ethernet Type codes
» MAC address assignments
» DLL entry point reference
» Boost SQL performance
» Free SMS UK
» Free SMS Ireland
» Free SMS South Africa
» Internet Explorer

Contact us

Most properties for the webcam are set and retrieved using windows messaging, that is, using the SendMessage API to issue windows messages (WM_) to the capture window. This is quite an arcane means of setting properties, so it is best that we encapsulate these into separate functions as follows:

Function capDriverConnect(ByVal lwnd As Integer, ByVal i As Short) _
As Boolean
capDriverConnect = SendMessage(lwnd, WM_CAP_DRIVER_CONNECT, i, 0)
End Function

The capDriverConnect API call connects a VFW driver to a previously created capture window. The i parameter is the index of the driver, which, in this application is always set to 0, but when used with multiple drivers, could be any number between 0 and 10.

Function capDriverDisconnect(ByVal lwnd As Integer) As Boolean
capDriverDisconnect = SendMessage(lwnd, WM_CAP_DRIVER_DISCONNECT, _
0, 0)
End Function

The capDriverDisconnect gracefully disconnects a VFW driver from a capture window. Failure to call this method before exiting the application could stop other webcam applications from accessing the camera.

Function capSetVideoFormat(ByVal hCapWnd As Integer,ByRef BmpFormat _
As BITMAPINFO, ByVal CapFormatSize As Integer) As Boolean
capSetVideoFormat = SendMessageAsBitMap(hCapWnd, _
WM_CAP_SET_VIDEOFORMAT, CapFormatSize, BmpFormat)
End Function

The capSetVideoFormat API is used to indicate to the webcam the format of image to be returned. Many cameras do not support all ranges of bitmap formats, however, 24 bit colour 320 x 240 and 640 x 480 are quite common.

Function capPreview(ByVal lwnd As Integer, ByVal f As Boolean) As _
capPreview = SendMessage(lwnd, WM_CAP_SET_PREVIEW, f, 0)
End Function

The capPreview function is used to initiate the streaming of images between the VFW driver and the capture window.

Function capPreviewRate(ByVal lwnd As Integer,ByVal wMS As Short)As _
capPreviewRate = SendMessage(lwnd, WM_CAP_SET_PREVIEWRATE, wMS, 0)

End Function

The capPreviewRate function determines the refresh rate by specifying the refresh interval in milliseconds. In our case, it is set to 66 ms (15 Frames Per second).

Now, we must implement the two functions referenced by the main form as follows
Sub MapWebcamToWindow(ByRef lWidth As Integer, ByRef lHeight As Integer, ByRef hWnd As Integer)
Dim lpszName As New VB6.FixedLengthString(100)
With bmp.bmiHeader
.biSize = Len(bmp.bmiHeader)
.biWidth = 320
.biHeight = 240
.biPlanes = 1
.biBitCount = 24
End With
lwndC = capCreateCaptureWindowA(lpszName.Value, _
WS_VISIBLE Or WS_CHILD, 0, 0, lWidth, lHeight, hWnd, 0)
If capDriverConnect(lwndC, 0) Then
capPreviewRate(lwndC, 66)
capPreview(lwndC, True)
capSetVideoFormat(lwndC, bmp, Len(bmp))
SetWindowPos(lwndC, 0, 0, 0, bmp.bmiHeader.biWidth, _
bmp.bmiHeader.biHeight, SWP_NOMOVE Or SWP_NOZORDER)
End If

End Sub Page 2  Page 4 


Copyright 2019 Infinite Loop Ltd.