1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| Imports System.Runtime.InteropServices
Public Class Form1
Const WM_CAP_START = &H400S
Const WS_CHILD = &H40000000
Const WS_VISIBLE = &H10000000
Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
Const WM_CAP_EDIT_COPY = WM_CAP_START + 30
Const WM_CAP_SEQUENCE = WM_CAP_START + 62
Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23
Const WM_CAP_SET_SCALE = WM_CAP_START + 53
Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
Const WM_CAP_SET_SEQUENCE_SETUP As Long = WM_CAP_START + 64
Const SWP_NOMOVE = &H2S
Const SWP_NOSIZE = 1
Const SWP_NOZORDER = &H4S
Const HWND_BOTTOM = 1
Private Const WM_CAP_STOP As Long = (WM_CAP_START + 68)
Private Const WM_CAP_FILE_SET_CAPTURE_FILEA As Long = (WM_CAP_START + 20)
Structure CAPTUREPARMS
Public dwRequestMicroSecPerFrame As Long '// Requested capture rate
Public fMakeUserHitOKToCapture As Long '// Show "Hit OK to cap" dlg?
Public wPercentDropForError As Long '// Give error msg if > (10%)
Public fYield As Long '// Capture via background task?
Public dwIndexSize As Long '// Max index size in frames (32K)
Public wChunkGranularity As Long '// Junk chunk granularity (2K)
Public fUsingDOSMemory As Long '// Use DOS buffers?
Public wNumVideoRequested As Long '// # video buffers, If 0, autocalc
Public fCaptureAudio As Long '// Capture audio?
Public wNumAudioRequested As Long '// # audio buffers, If 0, autocalc
Public vKeyAbort As Long '// Virtual key causing abort
Public fAbortLeftMouse As Long '// Abort on left mouse?
Public fAbortRightMouse As Long '// Abort on right mouse?
Public fLimitEnabled As Long '// Use wTimeLimit?
Public wTimeLimit As Long '// Seconds to capture
Public fMCIControl As Long '// Use MCI video source?
Public fStepMCIDevice As Long '// Step MCI device?
Public dwMCIStartTime As Long '// Time to start in MS
Public dwMCIStopTime As Long '// Time to stop in MS
Public fStepCaptureAt2x As Long '// Perform spatial averaging 2x
Public wStepCaptureAverageFrames As Long '// Temporal average n Frames
Public dwAudioBufferSize As Long '// Size of audio bufs (0 = default)
Public fDisableWriteCache As Long '// Attempt to disable write cache
End Structure
'--The capGetDriverDescription function retrieves the version description of the capture driver--
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" _
(ByVal wDriverIndex As Short, _
ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
ByVal cbVer As Integer) As Boolean
'--The capCreateCaptureWindow function creates a capture window--
Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
(ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
ByVal nHeight As Short, ByVal hWnd As Integer, _
ByVal nID As Integer) As Integer
'--This function sends the specified message to a window or windows--
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, _
<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
'--Sets the position of the window relative to the screen buffer--
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
(ByVal hwnd As Integer, _
ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
'--This function destroys the specified window--
Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
Dim VideoSource As Integer
Dim hWnd As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'NorthwindDataSet.Employees' table. You can move, or remove it, as needed.
' Me.EmployeesTableAdapter.Fill(Me.NorthwindDataSet.Employees)
'TODO: This line of code loads data into the 'NorthwindDataSet.Employees' table. You can move, or remove it, as needed.
' Me.EmployeesTableAdapter.Fill(Me.NorthwindDataSet.Employees)
btnStartRecording.Enabled = True
btnStopRecording.Enabled = False
'---list all the video sources---
ListVideoSources()
End Sub
'--disconnect from video source---
Private Sub StopPreviewWindow()
SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
DestroyWindow(hWnd)
End Sub
'---list all the various video sources---
Private Sub ListVideoSources()
Dim DriverName As String = Space(80)
Dim DriverVersion As String = Space(80)
For i As Integer = 0 To 9
If capGetDriverDescriptionA(i, DriverName, 80, DriverVersion, 80) Then
lstVideoSources.Items.Add(DriverName.Trim)
End If
Next
End Sub
'---save the image---
Private Sub CaptureImage()
Dim data As IDataObject
Dim bmap As Image
'---copy the image to the clipboard---
SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0)
'---retrieve the image from clipboard and convert it
' to the bitmap format
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = _
CType(data.GetData(GetType(System.Drawing.Bitmap)), _
Image)
PhotoPictureBox.Image = bmap
StopPreviewWindow()
End If
End Sub
'---preview the selected video source---
Private Sub PreviewVideo(ByVal pbCtrl As PictureBox)
hWnd = capCreateCaptureWindowA(VideoSource, WS_VISIBLE Or WS_CHILD, 0, 0, 0, _
0, pbCtrl.Handle.ToInt32, 0)
If SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, VideoSource, 0) Then
'---set the preview scale---
SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
'---set the preview rate (ms)---
SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0)
'---start previewing the image---
SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0)
'---resize window to fit in PictureBox control---
SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, _
pbCtrl.Width, pbCtrl.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
Else
'--error connecting to video source---
DestroyWindow(hWnd)
End If
End Sub
Private Sub lstVideoSources_SelectedIndexChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles lstVideoSources.SelectedIndexChanged
'---stop video in case it is on---
StopPreviewWindow()
'---check which video source is selected---
VideoSource = lstVideoSources.SelectedIndex
'---preview the selected video source
PreviewVideo(PictureBox1)
End Sub
Private Sub btnStopCamera_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopCamera.Click
StopPreviewWindow()
End Sub
Private Sub btnStartRecording_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartRecording.Click
btnStartRecording.Enabled = False
btnStopRecording.Enabled = True
'---start recording---
SendMessage(hWnd, WM_CAP_SEQUENCE, 0, 0)
End Sub
Private Sub btnStopRecording_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopRecording.Click
btnStartRecording.Enabled = True
btnStopRecording.Enabled = False
'---save the recording to file---
SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, "C:\Users\Anis\Documents\RecordedVideo.avi")
End Sub
Private Sub EmployeesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.EmployeesBindingSource.EndEdit()
Me.EmployeesTableAdapter.Update(Me.NorthwindDataSet.Employees)
End Sub
Private Sub btnCapturePhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapturePhoto.Click
CaptureImage()
End Sub
End Class |
Partager