EDN Admin
Well-known member
im trying to create a webcam software and found this code on the internet, the first time i run it, it works fine but when i stop it and run it again it doesnt work.
<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.IO
<span style="color:Blue; Public <span style="color:Blue; Class FrmCap
<span style="color:Green; Create constant using attend in function of DLL file.
<span style="color:Blue; Const CAP <span style="color:Blue; As <span style="color:Blue; Short = &H400S
<span style="color:Blue; Const CAP_DRIVER_CONNECT <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 10
<span style="color:Blue; Const CAP_DRIVER_DISCONNECT <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 11
<span style="color:Blue; Const CAP_EDIT_COPY <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 30
<span style="color:Blue; Const CAP_SET_PREVIEW <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 50
<span style="color:Blue; Const CAP_SET_PREVIEWRATE <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 52
<span style="color:Blue; Const CAP_SET_SCALE <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 53
<span style="color:Blue; Const WS_CHILD <span style="color:Blue; As <span style="color:Blue; Integer = &H40000000
<span style="color:Blue; Const WS_VISIBLE <span style="color:Blue; As <span style="color:Blue; Integer = &H10000000
<span style="color:Blue; Const SWP_NOMOVE <span style="color:Blue; As <span style="color:Blue; Short = &H2S
<span style="color:Blue; Const SWP_NOSIZE <span style="color:Blue; As <span style="color:Blue; Short = 1
<span style="color:Blue; Const SWP_NOZORDER <span style="color:Blue; As <span style="color:Blue; Short = &H4S
<span style="color:Blue; Const HWND_BOTTOM <span style="color:Blue; As <span style="color:Blue; Short = 1
<span style="color:Blue; Dim iDevice <span style="color:Blue; As <span style="color:Blue; Integer = 0 <span style="color:Green; Normal device ID
<span style="color:Blue; Dim hHwnd <span style="color:Blue; As <span style="color:Blue; Integer <span style="color:Green; Handle value to preview window
<span style="color:Green; Declare function from AVI capture DLL.
<span style="color:Blue; Declare <span style="color:Blue; Function SendMessage <span style="color:Blue; Lib <span style="color:#A31515; "user32" <span style="color:Blue; Alias <span style="color:#A31515; "SendMessageA" _
(<span style="color:Blue; ByVal hwnd <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal wMsg <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal wParam <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal lParam <span style="color:Blue; As <span style="color:Blue; Object) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Declare <span style="color:Blue; Function SetWindowPos <span style="color:Blue; Lib <span style="color:#A31515; "user32" <span style="color:Blue; Alias <span style="color:#A31515; "SetWindowPos" (<span style="color:Blue; ByVal hwnd <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal hWndInsertAfter <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal x <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal y <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal cx <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal cy <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal wFlags <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Declare <span style="color:Blue; Function DestroyWindow <span style="color:Blue; Lib <span style="color:#A31515; "user32" (<span style="color:Blue; ByVal hndw <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; Declare <span style="color:Blue; Function capCreateCaptureWindowA <span style="color:Blue; Lib <span style="color:#A31515; "avicap32.dll" _
(<span style="color:Blue; ByVal lpszWindowName <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal dwStyle <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal x <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal y <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal nWidth <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal nHeight <span style="color:Blue; As <span style="color:Blue; Short, <span style="color:Blue; ByVal hWndParent <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal nID <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Private <span style="color:Blue; Sub OpenForm()
<span style="color:Blue; Dim iHeight <span style="color:Blue; As <span style="color:Blue; Integer = picCapture.Height
<span style="color:Blue; Dim iWidth <span style="color:Blue; As <span style="color:Blue; Integer = picCapture.Width
<span style="color:Green; Open Preview window in picturebox .
<span style="color:Green; Create a child window with capCreateCaptureWindowA so you can display it in a picturebox.
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE <span style="color:Blue; Or WS_CHILD, 0, 0, 640, _
480, picCapture.Handle.ToInt32, 0)
<span style="color:Green; Connect to device
<span style="color:Blue; If SendMessage(hHwnd, CAP_DRIVER_CONNECT, iDevice, 0) <span style="color:Blue; Then
<span style="color:Green; Set the preview scale
SendMessage(hHwnd, CAP_SET_SCALE, <span style="color:Blue; True, 0)
<span style="color:Green; Set the preview rate in milliseconds
SendMessage(hHwnd, CAP_SET_PREVIEWRATE, 66, 0)
<span style="color:Green; Start previewing the image from the camera
SendMessage(hHwnd, CAP_SET_PREVIEW, <span style="color:Blue; True, 0)
<span style="color:Green; Resize window to fit in picturebox
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
SWP_NOMOVE <span style="color:Blue; Or SWP_NOZORDER)
<span style="color:Blue; Else
<span style="color:Green; Error connecting to device close window
DestroyWindow(hHwnd)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Green; Use SendMessage to copy the data to the clipboard Then transfer the image to the picture box.
<span style="color:Blue; Private <span style="color:Blue; Sub btnSave_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles btnSave.Click
<span style="color:Blue; Dim data <span style="color:Blue; As IDataObject
<span style="color:Blue; Dim bmap <span style="color:Blue; As Image
<span style="color:Green; Copy image to clipboard
SendMessage(hHwnd, CAP_EDIT_COPY, 0, 0)
<span style="color:Green; Get image from clipboard and convert it to a bitmap
data = Clipboard.GetDataObject()
<span style="color:Blue; If data.GetDataPresent(<span style="color:Blue; GetType(System.Drawing.Bitmap)) <span style="color:Blue; Then
bmap = <span style="color:Blue; CType(data.GetData(<span style="color:Blue; GetType(System.Drawing.Bitmap)), Image)
picCapture.Image = bmap
<span style="color:Blue; Dim saveFileDialog1 <span style="color:Blue; As <span style="color:Blue; New SaveFileDialog()
saveFileDialog1.Filter = <span style="color:#A31515; "Jpeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
saveFileDialog1.Title = <span style="color:#A31515; "Save an Image File"
saveFileDialog1.FileName = <span style="color:#A31515; "Image001"
saveFileDialog1.ShowDialog()
<span style="color:Green; If the file name is not an empty string open it for saving.
<span style="color:Blue; If saveFileDialog1.FileName <> <span style="color:#A31515; "" <span style="color:Blue; Then
<span style="color:Green; Saves the Image via a FileStream created by the OpenFile method.
<span style="color:Blue; Dim fs <span style="color:Blue; As System.IO.FileStream = <span style="color:Blue; CType _
(saveFileDialog1.OpenFile(), System.IO.FileStream)
picCapture.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
fs.Close()
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub frmcap_Leave(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; Me.Leave
<span style="color:Green; Disconnect from device
SendMessage(hHwnd, CAP_DRIVER_DISCONNECT, iDevice, 0)
<span style="color:Green; close window
DestroyWindow(hHwnd)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub FRMCAP_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
OpenForm()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
View the full article
<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.IO
<span style="color:Blue; Public <span style="color:Blue; Class FrmCap
<span style="color:Green; Create constant using attend in function of DLL file.
<span style="color:Blue; Const CAP <span style="color:Blue; As <span style="color:Blue; Short = &H400S
<span style="color:Blue; Const CAP_DRIVER_CONNECT <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 10
<span style="color:Blue; Const CAP_DRIVER_DISCONNECT <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 11
<span style="color:Blue; Const CAP_EDIT_COPY <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 30
<span style="color:Blue; Const CAP_SET_PREVIEW <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 50
<span style="color:Blue; Const CAP_SET_PREVIEWRATE <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 52
<span style="color:Blue; Const CAP_SET_SCALE <span style="color:Blue; As <span style="color:Blue; Integer = CAP + 53
<span style="color:Blue; Const WS_CHILD <span style="color:Blue; As <span style="color:Blue; Integer = &H40000000
<span style="color:Blue; Const WS_VISIBLE <span style="color:Blue; As <span style="color:Blue; Integer = &H10000000
<span style="color:Blue; Const SWP_NOMOVE <span style="color:Blue; As <span style="color:Blue; Short = &H2S
<span style="color:Blue; Const SWP_NOSIZE <span style="color:Blue; As <span style="color:Blue; Short = 1
<span style="color:Blue; Const SWP_NOZORDER <span style="color:Blue; As <span style="color:Blue; Short = &H4S
<span style="color:Blue; Const HWND_BOTTOM <span style="color:Blue; As <span style="color:Blue; Short = 1
<span style="color:Blue; Dim iDevice <span style="color:Blue; As <span style="color:Blue; Integer = 0 <span style="color:Green; Normal device ID
<span style="color:Blue; Dim hHwnd <span style="color:Blue; As <span style="color:Blue; Integer <span style="color:Green; Handle value to preview window
<span style="color:Green; Declare function from AVI capture DLL.
<span style="color:Blue; Declare <span style="color:Blue; Function SendMessage <span style="color:Blue; Lib <span style="color:#A31515; "user32" <span style="color:Blue; Alias <span style="color:#A31515; "SendMessageA" _
(<span style="color:Blue; ByVal hwnd <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal wMsg <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal wParam <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal lParam <span style="color:Blue; As <span style="color:Blue; Object) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Declare <span style="color:Blue; Function SetWindowPos <span style="color:Blue; Lib <span style="color:#A31515; "user32" <span style="color:Blue; Alias <span style="color:#A31515; "SetWindowPos" (<span style="color:Blue; ByVal hwnd <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal hWndInsertAfter <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal x <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal y <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal cx <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal cy <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal wFlags <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Declare <span style="color:Blue; Function DestroyWindow <span style="color:Blue; Lib <span style="color:#A31515; "user32" (<span style="color:Blue; ByVal hndw <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; Declare <span style="color:Blue; Function capCreateCaptureWindowA <span style="color:Blue; Lib <span style="color:#A31515; "avicap32.dll" _
(<span style="color:Blue; ByVal lpszWindowName <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal dwStyle <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal x <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal y <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal nWidth <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal nHeight <span style="color:Blue; As <span style="color:Blue; Short, <span style="color:Blue; ByVal hWndParent <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal nID <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Private <span style="color:Blue; Sub OpenForm()
<span style="color:Blue; Dim iHeight <span style="color:Blue; As <span style="color:Blue; Integer = picCapture.Height
<span style="color:Blue; Dim iWidth <span style="color:Blue; As <span style="color:Blue; Integer = picCapture.Width
<span style="color:Green; Open Preview window in picturebox .
<span style="color:Green; Create a child window with capCreateCaptureWindowA so you can display it in a picturebox.
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE <span style="color:Blue; Or WS_CHILD, 0, 0, 640, _
480, picCapture.Handle.ToInt32, 0)
<span style="color:Green; Connect to device
<span style="color:Blue; If SendMessage(hHwnd, CAP_DRIVER_CONNECT, iDevice, 0) <span style="color:Blue; Then
<span style="color:Green; Set the preview scale
SendMessage(hHwnd, CAP_SET_SCALE, <span style="color:Blue; True, 0)
<span style="color:Green; Set the preview rate in milliseconds
SendMessage(hHwnd, CAP_SET_PREVIEWRATE, 66, 0)
<span style="color:Green; Start previewing the image from the camera
SendMessage(hHwnd, CAP_SET_PREVIEW, <span style="color:Blue; True, 0)
<span style="color:Green; Resize window to fit in picturebox
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
SWP_NOMOVE <span style="color:Blue; Or SWP_NOZORDER)
<span style="color:Blue; Else
<span style="color:Green; Error connecting to device close window
DestroyWindow(hHwnd)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Green; Use SendMessage to copy the data to the clipboard Then transfer the image to the picture box.
<span style="color:Blue; Private <span style="color:Blue; Sub btnSave_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles btnSave.Click
<span style="color:Blue; Dim data <span style="color:Blue; As IDataObject
<span style="color:Blue; Dim bmap <span style="color:Blue; As Image
<span style="color:Green; Copy image to clipboard
SendMessage(hHwnd, CAP_EDIT_COPY, 0, 0)
<span style="color:Green; Get image from clipboard and convert it to a bitmap
data = Clipboard.GetDataObject()
<span style="color:Blue; If data.GetDataPresent(<span style="color:Blue; GetType(System.Drawing.Bitmap)) <span style="color:Blue; Then
bmap = <span style="color:Blue; CType(data.GetData(<span style="color:Blue; GetType(System.Drawing.Bitmap)), Image)
picCapture.Image = bmap
<span style="color:Blue; Dim saveFileDialog1 <span style="color:Blue; As <span style="color:Blue; New SaveFileDialog()
saveFileDialog1.Filter = <span style="color:#A31515; "Jpeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
saveFileDialog1.Title = <span style="color:#A31515; "Save an Image File"
saveFileDialog1.FileName = <span style="color:#A31515; "Image001"
saveFileDialog1.ShowDialog()
<span style="color:Green; If the file name is not an empty string open it for saving.
<span style="color:Blue; If saveFileDialog1.FileName <> <span style="color:#A31515; "" <span style="color:Blue; Then
<span style="color:Green; Saves the Image via a FileStream created by the OpenFile method.
<span style="color:Blue; Dim fs <span style="color:Blue; As System.IO.FileStream = <span style="color:Blue; CType _
(saveFileDialog1.OpenFile(), System.IO.FileStream)
picCapture.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
fs.Close()
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub frmcap_Leave(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; Me.Leave
<span style="color:Green; Disconnect from device
SendMessage(hHwnd, CAP_DRIVER_DISCONNECT, iDevice, 0)
<span style="color:Green; close window
DestroyWindow(hHwnd)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub FRMCAP_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
OpenForm()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
View the full article