|
Constant Bitrate
Join Date: Sep 2006
Location: Cornwall, England
Vehicle: 2003 VW Transporter T5
Posts: 145
|
Thanks for the reply, this is the code that I've just got working. It's basic, but it works, and I'll probably build on it...
Imports System.Runtime.InteropServices
Public Class Form1
Const WM_CAP As Short = &H400S
Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
Const WS_CHILD As Integer = &H40000000
Const WS_VISIBLE As Integer = &H10000000
Const SWP_NOMOVE As Short = &H2S
Const SWP_NOSIZE As Short = 1
Const SWP_NOZORDER As Short = &H4S
Const HWND_BOTTOM As Short = 1
'--------------------------------------------------
Const WM_CAP_CONNECT As Integer = 1034
Const WM_CAP_DISCONNECT As Integer = 1035
Const WM_CAP_GET_FRAME As Integer = 1084
Const WM_CAP_COPY As Integer = 1054
Private tempObj As IDataObject
Private tempimg As System.Drawing.Bitmap
Private mCapHwnd As Integer ' Handle of webcam
Dim iDevice As Integer = 0 ' Current device ID
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
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
Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
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 hWndParent As Integer, _
ByVal nID As Integer) As Integer
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
ByVal cbVer As Integer) As Boolean
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Try
' setup a capture window
mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, 320, 240, Me.Handle.ToInt32(), 0)
' connect to the capture device
SendMessage(mCapHwnd, WM_CAP_CONNECT, iDevice, 0)
SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, iDevice, 0)
'Set the preview rate in milliseconds
SendMessage(mCapHwnd, WM_CAP_SET_PREVIEWRATE, 40, 0)
Catch ex As Exception
DestroyWindow(mCapHwnd)
End Try
Timer1.Enabled = True
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
' Disconnect device
SendMessage(mCapHwnd, WM_CAP_DISCONNECT, iDevice, 0)
Timer1.Enabled = False
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
DestroyWindow(mCapHwnd)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
' get the next frame;
SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0)
' copy the frame to the clipboard
SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0)
' get from the clipboard
tempObj = Clipboard.GetDataObject()
tempimg = tempObj.GetData(System.Windows.Forms.DataFormats.B itmap)
tempimg.RotateFlip(RotateFlipType.RotateNoneFlipX)
PictureBox.Image = tempimg
Catch ex As Exception
End Try
End Sub
End Class
All I do is copy a frame to the clipboard from the camera every time the timer ticks. I then copy and flip the image to a tempory image object and then put the image in the image box.
Seems to work OK, do you think this is a good method?
__________________
ViVE - Volkswagen In Van Entertainment:
VoomPC 2, VIA C7 2GHz
7" Lilliput
120Gb Sata drive
1GB RAM
[||||||||||] 100% - Planning
[||||||||||] 100% - Software coding
[||||||||||] 100% - Built
(Always tweaking!)
|