'-------------------------------------------------
'--------Code By Ken Tucker -------------
'-------------------------------------------------
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents picCapture As System.Windows.Forms.PictureBox
Friend WithEvents lstDevices As System.Windows.Forms.ListBox
Friend WithEvents lblDevice As System.Windows.Forms.Label
Friend WithEvents btnStart As System.Windows.Forms.Button
Friend WithEvents btnSave As System.Windows.Forms.Button
Friend WithEvents btnStop As System.Windows.Forms.Button
Friend WithEvents sfdImage As System.Windows.Forms.SaveFileDialog
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2Me.picCapture = New System.Windows.Forms.PictureBox()
3Me.lstDevices = New System.Windows.Forms.ListBox()
4Me.lblDevice = New System.Windows.Forms.Label()
5Me.btnStart = New System.Windows.Forms.Button()
6Me.btnSave = New System.Windows.Forms.Button()
7Me.btnStop = New System.Windows.Forms.Button()
8Me.sfdImage = New System.Windows.Forms.SaveFileDialog()
9Me.SuspendLayout()
10'
11'picCapture
12'
13Me.picCapture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
14Me.picCapture.Location = New System.Drawing.Point(208, 24)
15Me.picCapture.Name = "picCapture"
16Me.picCapture.Size = New System.Drawing.Size(256, 272)
17Me.picCapture.TabIndex = 0
18Me.picCapture.TabStop = False
19'
20'lstDevices
21'
22Me.lstDevices.Location = New System.Drawing.Point(8, 55)
23Me.lstDevices.Name = "lstDevices"
24Me.lstDevices.Size = New System.Drawing.Size(184, 238)
25Me.lstDevices.TabIndex = 1
26'
27'lblDevice
28'
29Me.lblDevice.Location = New System.Drawing.Point(8, 32)
30Me.lblDevice.Name = "lblDevice"
31Me.lblDevice.Size = New System.Drawing.Size(184, 16)
32Me.lblDevice.TabIndex = 2
33Me.lblDevice.Text = "Available Devices"
34Me.lblDevice.TextAlign = System.Drawing.ContentAlignment.TopCenter
35'
36'btnStart
37'
38Me.btnStart.Location = New System.Drawing.Point(20, 320)
39Me.btnStart.Name = "btnStart"
40Me.btnStart.Size = New System.Drawing.Size(112, 32)
41Me.btnStart.TabIndex = 3
42Me.btnStart.Text = "Start Preview"
43'
44'btnSave
45'
46Me.btnSave.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
47Me.btnSave.Location = New System.Drawing.Point(348, 320)
48Me.btnSave.Name = "btnSave"
49Me.btnSave.Size = New System.Drawing.Size(112, 32)
50Me.btnSave.TabIndex = 4
51Me.btnSave.Text = "Save Image"
52'
53'btnStop
54'
55Me.btnStop.Location = New System.Drawing.Point(184, 320)
56Me.btnStop.Name = "btnStop"
57Me.btnStop.Size = New System.Drawing.Size(112, 32)
58Me.btnStop.TabIndex = 5
59Me.btnStop.Text = "Stop Preview"
60'
61'sfdImage
62'
63Me.sfdImage.FileName = "Webcam1"
64Me.sfdImage.Filter = "Bitmap|*.bmp"
65'
66'Form1
67'
68Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
69Me.ClientSize = New System.Drawing.Size(480, 382)
70Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnStop, Me.btnSave, Me.btnStart, Me.lblDevice, Me.lstDevices, Me.picCapture})
71Me.Name = "Form1"
72Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
73Me.Text = "Video Capture"
74Me.ResumeLayout(False)
75
76End Sub
77
78#End Region
79
80Const WM_CAP As Short = &H400S
81
82Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
83Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
84Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
85
86Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
87Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
88Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
89Const WS_CHILD As Integer = &H40000000
90Const WS_VISIBLE As Integer = &H10000000
91Const SWP_NOMOVE As Short = &H2S
92Const SWP_NOSIZE As Short = 1
93Const SWP_NOZORDER As Short = &H4S
94Const HWND_BOTTOM As Short = 1
95
96Dim iDevice As Integer = 0 ' Current device ID
97Dim hHwnd As Integer ' Handle to preview window
98
99Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
100(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
101<marshalas(unmanagedtype.asany)> ByVal lParam As Object) As Integer
102
103Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, _
104ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
105ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
106
107Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
108
109Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
110(ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
111ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
112ByVal nHeight As Short, ByVal hWndParent As Integer, _
113ByVal nID As Integer) As Integer
114
115Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
116ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
117ByVal cbVer As Integer) As Boolean
118
119Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
120LoadDeviceList()
121If lstDevices.Items.Count > 0 Then
122btnStart.Enabled = True
123lstDevices.SelectedIndex = 0
124btnStart.Enabled = True
125Else
126lstDevices.Items.Add("No Capture Device")
127btnStart.Enabled = False
128End If
129
130btnStop.Enabled = False
131btnSave.Enabled = False
132picCapture.SizeMode = PictureBoxSizeMode.StretchImage
133End Sub
134
135Private Sub LoadDeviceList()
136Dim strName As String = Space(100)
137Dim strVer As String = Space(100)
138Dim bReturn As Boolean
139Dim x As Integer = 0
140
141'
142' Load name of all avialable devices into the lstDevices
143'
144
145Do
146'
147' Get Driver name and version
148'
149bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
150
151'
152' If there was a device add device name to the list
153'
154If bReturn Then lstDevices.Items.Add(strName.Trim)
155x += 1
156Loop Until bReturn = False
157End Sub
158
159Private Sub OpenPreviewWindow()
160Dim iHeight As Integer = picCapture.Height
161Dim iWidth As Integer = picCapture.Width
162
163'
164' Open Preview window in picturebox
165'
166hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, _
167480, picCapture.Handle.ToInt32, 0)
168
169'
170' Connect to device
171'
172If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
173'
174'Set the preview scale
175'
176SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
177
178'
179'Set the preview rate in milliseconds
180'
181SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
182
183'
184'Start previewing the image from the camera
185'
186SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
187
188'
189' Resize window to fit in picturebox
190'
191SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
192SWP_NOMOVE Or SWP_NOZORDER)
193
194btnSave.Enabled = True
195btnStop.Enabled = True
196btnStart.Enabled = False
197Else
198'
199' Error connecting to device close window
200'
201DestroyWindow(hHwnd)
202
203btnSave.Enabled = False
204End If
205End Sub
206
207Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
208iDevice = lstDevices.SelectedIndex
209OpenPreviewWindow()
210End Sub
211
212Private Sub ClosePreviewWindow()
213'
214' Disconnect from device
215'
216SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
217
218'
219' close window
220'
221
222DestroyWindow(hHwnd)
223End Sub
224
225Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
226ClosePreviewWindow()
227btnSave.Enabled = False
228btnStart.Enabled = True
229btnStop.Enabled = False
230End Sub
231
232Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
233Dim data As IDataObject
234Dim bmap As Image
235
236'
237' Copy image to clipboard
238'
239SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
240
241'
242' Get image from clipboard and convert it to a bitmap
243'
244data = Clipboard.GetDataObject()
245If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
246bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
247picCapture.Image = bmap
248ClosePreviewWindow()
249btnSave.Enabled = False
250btnStop.Enabled = False
251btnStart.Enabled = True
252
253If sfdImage.ShowDialog = DialogResult.OK Then
254bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
255End If
256
257End If
258End Sub
259
260Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
261If btnStop.Enabled Then
262ClosePreviewWindow()
263End If
264End Sub
265End Class</marshalas(unmanagedtype.asany)></system.diagnostics.debuggerstepthrough()>