That the class I use for it.
Imports System.Drawing
Imports System.Windows.Forms
Public Class EDCaptureCam
#Region "API"
Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias
"capCreateCaptureWindowA" ( _
ByVal lpszWindowName As String, _
ByVal dwStyle As Integer, _
ByVal X As Integer, _
ByVal Y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hwndParent As Integer, _
ByVal nID As Integer) As Integer
Private Declare Function capGetVideoFormatSize Lib "avicap32.dll" Alias
"capGetVideoFormatSize" (ByVal hwnd As Integer) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias
"SendMessageA" ( _
ByVal hwnd As Integer, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByRef lParam As Integer) As Integer
Private Structure VIDEOHDR
Dim lpData As Integer '// address of video buffer
Dim dwBufferLength As Integer '// size, in bytes, of the Data buffer
Dim dwBytesUsed As Integer '// see below
Dim dwTimeCaptured As Integer '// see below
Dim dwUser As Integer '// user-specific data
Dim dwFlags As Integer '// see below
<VBFixedArray(3)> Dim dwReserved() As Integer '// reserved; do not
use}
End Structure
Private Delegate Function CallBackDelegate(ByVal hwnd As IntPtr, ByRef
lpVHdr As VIDEOHDR) As IntPtr
Private delCallBack As CallBackDelegate = New CallBackDelegate(AddressOf
FrameCallbackTarget)
Private Declare Function SendMessage2 Lib "user32.dll" Alias
"SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As
IntPtr, ByVal lParam As CallBackDelegate) As IntPtr
Private Const WM_USER As Long = &H400
Private Const WM_CAP_DRIVER_CONNECT As Long = WM_USER + 10
Private Const WM_CAP_DRIVER_DISCONNECT As Long = WM_USER + 11
Private Const WM_CAP_GRAB_FRAME As Long = WM_USER + 60
Private Const WM_CAP_EDIT_COPY As Long = WM_USER + 30
Private Const WS_CHILD = &H40000000
Private Const WS_VISIBLE = &H10000000
Private Const WM_CAP_START = WM_USER
Private Const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2
Private Const WM_CAP_SET_CALLBACK_STATUS = WM_CAP_START + 3
Private Const WM_CAP_SET_CALLBACK_YIELD = WM_CAP_START + 4
Private Const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5
Private Const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6
Private Const WM_CAP_SET_CALLBACK_WAVESTREAM = WM_CAP_START + 7
Private Const WM_CAP_DRIVER_GET_CAPS = WM_CAP_START + 14
Private Const WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41
Private Const WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42
Private Const WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43
Private Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
Private Const WM_CAP_SET_OVERLAY = WM_CAP_START + 51
Private Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Private Const WM_CAP_GET_STATUS = WM_CAP_START + 54
Private Const WM_CAP_SET_CALLBACK_CAPCONTROL = WM_CAP_START + 85
Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal
hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
Private Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC"
(ByVal hwnd As Integer) As Integer
Private Declare Function DestroyWindow Lib "user32" Alias
"DestroyWindow" (ByVal hwnd As Integer) As Integer
Private Structure POINTAPI
Dim x As Integer
Dim y As Integer
End Structure
Private Structure RECTAPI
Dim Left As Integer
Dim Top As Integer
Dim Right As Integer
Dim Bottom As Integer
End Structure
Private Declare Function FillRect Lib "user32" (ByVal hdc As IntPtr,
ByRef lpRect As RECTAPI, ByVal hBrush As IntPtr) As Integer
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As
Integer) As IntPtr
Private Structure CAPSTATUS
Dim uiImageWidth As Integer '// Width of the
image
Dim uiImageHeight As Integer '// Height of the
image
Dim fLiveWindow As Integer '// Now Previewing
video?
Dim fOverlayWindow As Integer '// Now Overlaying
video?
Dim fScale As Integer '// Scale image to
client?
Dim ptScroll As POINTAPI '// Scroll position
Dim fUsingDefaultPalette As Integer '// Using default
driver palette?
Dim fAudioHardware As Integer '// Audio hardware
present?
Dim fCapFileExists As Integer '// Does capture file
exist?
Dim dwCurrentVideoFrame As Integer '// # of video frames
cap'td
Dim dwCurrentVideoFramesDropped As Integer '// # of video frames
dropped
Dim dwCurrentWaveSamples As Integer '// # of wave samples
cap'td
Dim dwCurrentTimeElapsedMS As Integer '// Elapsed capture
duration
Dim hPalCurrent As Integer '// Current palette
in use
Dim fCapturingNow As Integer '// Capture in
progress?
Dim dwReturn As Integer '// Error value after
any operation
Dim wNumVideoAllocated As Integer '// Actual number of
video buffers
Dim wNumAudioAllocated As Integer '// Actual number of
audio buffers
End Structure
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As
Integer, _
ByRef lParam As CAPSTATUS) As Boolean
#End Region
Public Enum PurgeSurplus As Byte
Far = 0
Big = 1
End Enum
Public Event Refreshed(ByVal pBitMap As Bitmap)
'Public Event Filter(ByVal pRects As Collection)
Public HWnd As Integer
Public HDc As Integer
Private mWidth, mHeight As Integer
Private mRate As Integer
Private mbRunning As Boolean
Public Sub Dispose()
If HWnd <> 0 Then
StopCap()
SendMessage(HWnd, WM_CAP_DRIVER_DISCONNECT, 0, 0)
DestroyWindow(HWnd)
HWnd = 0
End If
End Sub
Protected Overrides Sub Finalize()
Dispose()
MyBase.Finalize()
End Sub
Public Sub New()
End Sub
Public Sub Initialize(ByVal pHwndParent As IntPtr, ByVal psDriver As
String)
Dispose()
mWidth = 640
mHeight = 480
HWnd = capCreateCaptureWindow("EDCaptureCam", _
WS_CHILD + WS_VISIBLE, 1 - mWidth,
1 - mHeight, _
mWidth, _
mHeight, _
pHwndParent.ToInt64, 0)
SendMessage(HWnd, WM_CAP_DRIVER_CONNECT, 0, 0)
'First image to get size
Dim s As CAPSTATUS
SendMessage(HWnd, WM_CAP_GET_STATUS,
System.Runtime.InteropServices.Marshal.SizeOf(s), s)
mWidth = s.uiImageWidth
mHeight = s.uiImageHeight
'If Not Clipboard.GetDataObject Is Nothing Then
' SendMessage(HWnd, WM_CAP_GRAB_FRAME, 0, 0)
' SendMessage(HWnd, WM_CAP_EDIT_COPY, 0, 0)
' With New
Bitmap(CType(Clipboard.GetDataObject.GetData(Windo ws.Forms.DataFormats.Bitma
p, True), Image))
' mWidth = .Width
' mHeight = .Height
' End With
'End If
End Sub
Public Sub ConfigFormat()
StopCap()
SendMessage(HWnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0)
'First image to get size
Dim s As CAPSTATUS
SendMessage(HWnd, WM_CAP_GET_STATUS,
System.Runtime.InteropServices.Marshal.SizeOf(s), s)
mWidth = s.uiImageWidth
mHeight = s.uiImageHeight
End Sub
Public Sub ConfigDevice()
StopCap()
SendMessage(HWnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0)
End Sub
Public Sub ConfigDisplay()
StopCap()
SendMessage(HWnd, WM_CAP_DLG_VIDEODISPLAY, 0, 0)
End Sub
Public Function StartCap(ByVal pRate As Integer) As Boolean
If HWnd = 0 Then
MsgBox("Not initialized")
Return False
End If
StopCap()
mRate = pRate
Try
HDc = GetWindowDC(HWnd)
SendMessage(HWnd, WM_CAP_SET_PREVIEWRATE, pRate, 0)
SendMessage(HWnd, WM_CAP_SET_PREVIEW, 1, 0)
SendMessage2(New IntPtr(HWnd), WM_CAP_SET_CALLBACK_FRAME, New
IntPtr(0), delCallBack)
mbRunning = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return mbRunning
End Function
Public Sub StopCap()
mbRunning = False
Try
If HWnd > 0 Then
SendMessage2(New IntPtr(HWnd), WM_CAP_SET_CALLBACK_FRAME,
New IntPtr(0), Nothing)
SendMessage(HWnd, WM_CAP_SET_PREVIEW, 0, 0)
End If
mBitMapPrev = Nothing
Catch ex As Exception
End Try
End Sub
Public ReadOnly Property Width() As Integer
Get
Return mWidth
End Get
End Property
Public ReadOnly Property Height() As Integer
Get
Return mHeight
End Get
End Property
Private Function FrameCallbackTarget(ByVal hwnd As IntPtr, ByRef lpVHdr
As VIDEOHDR) As IntPtr
Dim lBitMap24 As New Bitmap(mWidth, mHeight, mWidth * 3,
Imaging.PixelFormat.Format24bppRgb, New System.IntPtr(lpVHdr.lpData))
Dim lBitMap As New Bitmap(lBitMap24) 'Convertit en 32
lBitMap.RotateFlip(RotateFlipType.RotateNoneFlipXY )
RaiseEvent Refreshed(lBitMap)
End Function
End Class