VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
Begin VB.Form Form1
Caption = "Video Capture"
ClientHeight = 5025
ClientLeft = 60
ClientTop = 450
ClientWidth = 8670
LinkTopic = "Form1"
ScaleHeight = 5025
ScaleWidth = 8670
StartUpPosition = 3 'Windows Default
Begin MSComDlg.CommonDialog CommonDialog1
Left = 4080
Top = 2280
_ExtentX = 847
_ExtentY = 847
_Version = 393216
End
Begin VB.CommandButton cmdSave
Caption = "Save Image"
Height = 495
Left = 6128
TabIndex = 5
Top = 4320
Width = 1215
End
Begin VB.CommandButton cmdStop
Caption = "Stop Preview"
Height = 495
Left = 3728
TabIndex = 4
Top = 4320
Width = 1215
End
Begin VB.CommandButton cmdStart
Caption = "Start Preview"
Height = 495
Left = 1328
TabIndex = 3
Top = 4320
Width = 1215
End
Begin VB.PictureBox picCapture
Height = 3495
Left = 3840
ScaleHeight = 229
ScaleMode = 3 'Pixel
ScaleWidth = 285
TabIndex = 1
Top = 600
Width = 4335
End
Begin VB.ListBox lstDevices
Height = 3180
Left = 240
TabIndex = 0
Top = 840
Width = 3255
End
Begin VB.Label Label1
Alignment = 2 'Center
Caption = "Available Devices"
Height = 255
Left = 240
TabIndex = 2
Top = 600
Width = 3255
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Const WM_CAP As Integer = &H400
Const WM_CAP_DRIVER_CONNECT As Long = WM_CAP + 10
Const WM_CAP_DRIVER_DISCONNECT As Long = WM_CAP + 11
Const WM_CAP_EDIT_COPY As Long = WM_CAP + 30
Const WM_CAP_SET_PREVIEW As Long = WM_CAP + 50
Const WM_CAP_SET_PREVIEWRATE As Long = WM_CAP + 52
Const WM_CAP_SET_SCALE As Long = WM_CAP + 53
Const WS_CHILD As Long = &H40000000
Const WS_VISIBLE As Long = &H10000000
Const SWP_NOMOVE As Long = &H2
Const SWP_NOSIZE As Integer = 1
Const SWP_NOZORDER As Integer = &H4
Const HWND_BOTTOM As Integer = 1
Dim iDevice As Long ' Current device ID
Dim hHwnd As Long ' Handle to preview window
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 SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hndw As Long) As Boolean
Private Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Integer, ByVal hWndParent As Long, ByVal nID As Long) As Long
Private Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Long, ByVal lpszName As String, ByVal cbName As Long, ByVal lpszVer As String, ByVal cbVer As Long) As Boolean
Private Sub cmdSave_Click()
Dim bm As Image
' Copy image to clipboard
SendMessage hHwnd, WM_CAP_EDIT_COPY, 0, 0
ClosePreviewWindow
picCapture.Picture = Clipboard.GetData
CommonDialog1.CancelError = True
CommonDialog1.FileName = "Webcam1"
CommonDialog1.Filter = "Bitmap |*.bmp"
On Error GoTo NoSave
CommonDialog1.ShowSave
SavePicture picCapture.Image, CommonDialog1.FileName
NoSave:
cmdStop.Enabled = False
cmdSave.Enabled = False
cmdStart.Enabled = True
End Sub
Private Sub cmdStart_Click()
iDevice = lstDevices.ListIndex
OpenPreviewWindow
End Sub
Private Sub cmdStop_Click()
ClosePreviewWindow
cmdStop.Enabled = False
cmdSave.Enabled = False
cmdStart.Enabled = True
End Sub
Private Sub Form_Load()
LoadDeviceList
If lstDevices.ListCount > 0 Then
lstDevices.Selected(0) = True
Else
cmdStart.Enabled = False
lstDevices.AddItem ("No Device Available")
End If
cmdStop.Enabled = False
cmdSave.Enabled = False
End Sub
Private Sub LoadDeviceList()
Dim strName As String
Dim strVer As String
Dim iReturn As Boolean
Dim x As Long
x = 0
strName = Space(100)
strVer = Space(100)
' Load name of all available devices into lstDevices
Do
' Get Driver name and version
iReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
' If there was a device add device name to the list
If iReturn Then lstDevices.AddItem Trim$(strName)
x = x + 1
Loop Until iReturn = False
End Sub
Private Sub OpenPreviewWindow()
' Open Preview window in picturebox
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.hwnd, 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.ScaleWidth, picCapture.ScaleHeight, SWP_NOMOVE Or SWP_NOZORDER
cmdSave.Enabled = True
cmdStop.Enabled = True
cmdStart.Enabled = False
Else
' Error connecting to device close window
DestroyWindow hHwnd
cmdSave.Enabled = False
End If
End Sub
Private Sub ClosePreviewWindow()
' Disconnect from device
SendMessage hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0
' close window
DestroyWindow hHwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
If cmdStop.Enabled Then
ClosePreviewWindow
End If
End Sub