Webcam Record video

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Good day guys. I am writing a code for recording a video from my webcam. I cant record a video and theres always a combo box asking me on what video device i use. My question is this, how can i remove that process and instead automatically detect the default
webcam on my laptop. And also how can i record a video with this project?
Here are the code I use, I got it from other forum and I try it. This is for capturing picture only. I want to modify this code and add a button that can record video and save it on my drive. Can someone help me.. Any help, suggestion, advice and reaction
is well appreciated. Thanks!

Here is the code:


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.Runtime.InteropServices

<span style="color:Blue; Public <span style="color:Blue; Class Form1
<span style="color:Blue; Inherits System.Windows.Forms.Form

<span style="color:Blue; #Region <span style="color:#A31515; " Windows Form Designer generated code "

<span style="color:Blue; Public <span style="color:Blue; Sub <span style="color:Blue; New()
<span style="color:Blue; MyBase.<span style="color:Blue; New()

<span style="color:Green; This call is required by the Windows Form Designer.
InitializeComponent()

<span style="color:Green; Add any initialization after the InitializeComponent() call

<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Green; Form overrides dispose to clean up the component list.
<span style="color:Blue; Protected <span style="color:Blue; Overloads <span style="color:Blue; Overrides <span style="color:Blue; Sub Dispose(<span style="color:Blue; ByVal disposing <span style="color:Blue; As <span style="color:Blue; Boolean)
<span style="color:Blue; If disposing <span style="color:Blue; Then
<span style="color:Blue; If <span style="color:Blue; Not (components <span style="color:Blue; Is <span style="color:Blue; Nothing) <span style="color:Blue; Then
components.Dispose()
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; MyBase.Dispose(disposing)
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Green; Required by the Windows Form Designer
<span style="color:Green; Private components As System.ComponentModel.IContainer

<span style="color:Green; NOTE: The following procedure is required by the Windows Form Designer
<span style="color:Green; It can be modified using the Windows Form Designer.
<span style="color:Green; Do not modify it using the code editor.
<span style="color:Green; Friend WithEvents picCapture As System.Windows.Forms.PictureBox
<span style="color:Green; Friend WithEvents lstDevices As System.Windows.Forms.ListBox
<span style="color:Green; Friend WithEvents lblDevice As System.Windows.Forms.Label
<span style="color:Green; Friend WithEvents btnStart As System.Windows.Forms.Button
<span style="color:Green; Friend WithEvents btnSave As System.Windows.Forms.Button
<span style="color:Green; Friend WithEvents btnStop As System.Windows.Forms.Button
<span style="color:Green; Friend WithEvents sfdImage As System.Windows.Forms.SaveFileDialog
<span style="color:Green; <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
<span style="color:Green; Me.picCapture = New System.Windows.Forms.PictureBox()
<span style="color:Green; Me.lstDevices = New System.Windows.Forms.ListBox()
<span style="color:Green; Me.lblDevice = New System.Windows.Forms.Label()
<span style="color:Green; Me.btnStart = New System.Windows.Forms.Button()
<span style="color:Green; Me.btnSave = New System.Windows.Forms.Button()
<span style="color:Green; Me.btnStop = New System.Windows.Forms.Button()
<span style="color:Green; Me.sfdImage = New System.Windows.Forms.SaveFileDialog()
<span style="color:Green; Me.SuspendLayout()
<span style="color:Green;
<span style="color:Green; picCapture
<span style="color:Green;
<span style="color:Green; Me.picCapture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
<span style="color:Green; Me.picCapture.Location = New System.Drawing.Point(208, 24)
<span style="color:Green; Me.picCapture.Name = "picCapture"
<span style="color:Green; Me.picCapture.Size = New System.Drawing.Size(256, 272)
<span style="color:Green; Me.picCapture.TabIndex = 0
<span style="color:Green; Me.picCapture.TabStop = False
<span style="color:Green;
<span style="color:Green; lstDevices
<span style="color:Green;
<span style="color:Green; Me.lstDevices.Location = New System.Drawing.Point(8, 55)
<span style="color:Green; Me.lstDevices.Name = "lstDevices"
<span style="color:Green; Me.lstDevices.Size = New System.Drawing.Size(184, 238)
<span style="color:Green; Me.lstDevices.TabIndex = 1
<span style="color:Green;
<span style="color:Green; lblDevice
<span style="color:Green;
<span style="color:Green; Me.lblDevice.Location = New System.Drawing.Point(8, 32)
<span style="color:Green; Me.lblDevice.Name = "lblDevice"
<span style="color:Green; Me.lblDevice.Size = New System.Drawing.Size(184, 16)
<span style="color:Green; Me.lblDevice.TabIndex = 2
<span style="color:Green; Me.lblDevice.Text = "Available Devices"
<span style="color:Green; Me.lblDevice.TextAlign = System.Drawing.ContentAlignment.TopCenter
<span style="color:Green;
<span style="color:Green; btnStart
<span style="color:Green;
<span style="color:Green; Me.btnStart.Location = New System.Drawing.Point(20, 320)
<span style="color:Green; Me.btnStart.Name = "btnStart"
<span style="color:Green; Me.btnStart.Size = New System.Drawing.Size(112, 32)
<span style="color:Green; Me.btnStart.TabIndex = 3
<span style="color:Green; Me.btnStart.Text = "Start Preview"
<span style="color:Green;
<span style="color:Green; btnSave
<span style="color:Green;
<span style="color:Green; Me.btnSave.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
<span style="color:Green; Me.btnSave.Location = New System.Drawing.Point(348, 320)
<span style="color:Green; Me.btnSave.Name = "btnSave"
<span style="color:Green; Me.btnSave.Size = New System.Drawing.Size(112, 32)
<span style="color:Green; Me.btnSave.TabIndex = 4
<span style="color:Green; Me.btnSave.Text = "Save Image"
<span style="color:Green;
<span style="color:Green; btnStop
<span style="color:Green;
<span style="color:Green; Me.btnStop.Location = New System.Drawing.Point(184, 320)
<span style="color:Green; Me.btnStop.Name = "btnStop"
<span style="color:Green; Me.btnStop.Size = New System.Drawing.Size(112, 32)
<span style="color:Green; Me.btnStop.TabIndex = 5
<span style="color:Green; Me.btnStop.Text = "Stop Preview"
<span style="color:Green;
<span style="color:Green; sfdImage
<span style="color:Green;
<span style="color:Green; Me.sfdImage.FileName = "Webcam1"
<span style="color:Green; Me.sfdImage.Filter = "Bitmap|*.bmp"
<span style="color:Green;
<span style="color:Green; Form1
<span style="color:Green;
<span style="color:Green; Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
<span style="color:Green; Me.ClientSize = New System.Drawing.Size(480, 382)
<span style="color:Green; Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnStop, Me.btnSave, Me.btnStart, Me.lblDevice, Me.lstDevices, Me.picCapture})
<span style="color:Green; Me.Name = "Form1"
<span style="color:Green; Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
<span style="color:Green; Me.Text = "Video Capture"
<span style="color:Green; Me.ResumeLayout(False)

<span style="color:Green; End Sub

<span style="color:Blue; #End Region

<span style="color:Blue; Const WM_CAP <span style="color:Blue; As <span style="color:Blue; Short = &H400S

<span style="color:Blue; Const WM_CAP_DRIVER_CONNECT <span style="color:Blue; As <span style="color:Blue; Integer = WM_CAP + 10
<span style="color:Blue; Const WM_CAP_DRIVER_DISCONNECT <span style="color:Blue; As <span style="color:Blue; Integer = WM_CAP + 11
<span style="color:Blue; Const WM_CAP_EDIT_COPY <span style="color:Blue; As <span style="color:Blue; Integer = WM_CAP + 30

<span style="color:Blue; Const WM_CAP_SET_PREVIEW <span style="color:Blue; As <span style="color:Blue; Integer = WM_CAP + 50
<span style="color:Blue; Const WM_CAP_SET_PREVIEWRATE <span style="color:Blue; As <span style="color:Blue; Integer = WM_CAP + 52
<span style="color:Blue; Const WM_CAP_SET_SCALE <span style="color:Blue; As <span style="color:Blue; Integer = WM_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; Current device ID
<span style="color:Blue; Dim hHwnd <span style="color:Blue; As <span style="color:Blue; Integer <span style="color:Green; Handle to preview window

<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, _
<MarshalAs(UnmanagedType.AsAny)> <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; Declare <span style="color:Blue; Function capGetDriverDescriptionA <span style="color:Blue; Lib <span style="color:#A31515; "avicap32.dll" (<span style="color:Blue; ByVal wDriver <span style="color:Blue; As <span style="color:Blue; Short, _
<span style="color:Blue; ByVal lpszName <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal cbName <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal lpszVer <span style="color:Blue; As <span style="color:Blue; String, _
<span style="color:Blue; ByVal cbVer <span style="color:Blue; As <span style="color:Blue; Integer) <span style="color:Blue; As <span style="color:Blue; Boolean

<span style="color:Blue; Private <span style="color:Blue; Sub Form1_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
LoadDeviceList()
<span style="color:Blue; If lstDevices.Items.<span style="color:Blue; Count > 0 <span style="color:Blue; Then
btnStart.Enabled = <span style="color:Blue; True
lstDevices.SelectedIndex = 0
btnStart.Enabled = <span style="color:Blue; True
<span style="color:Green;
<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;
<span style="color:Green; Open Preview window in picturebox
<span style="color:Green;
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE <span style="color:Blue; Or WS_CHILD, 0, 0, 640, _
480, picCapture.Handle.ToInt32, 0)

<span style="color:Green;
<span style="color:Green; Connect to device
<span style="color:Green;
<span style="color:Blue; If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) <span style="color:Blue; Then
<span style="color:Green;
<span style="color:Green; Set the preview scale
<span style="color:Green;
SendMessage(hHwnd, WM_CAP_SET_SCALE, <span style="color:Blue; True, 0)

<span style="color:Green;
<span style="color:Green; Set the preview rate in milliseconds
<span style="color:Green;
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)

<span style="color:Green;
<span style="color:Green; Start previewing the image from the camera
<span style="color:Green;
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, <span style="color:Blue; True, 0)

<span style="color:Green;
<span style="color:Green; Resize window to fit in picturebox
<span style="color:Green;
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
SWP_NOMOVE <span style="color:Blue; Or SWP_NOZORDER)

btnSave.Enabled = <span style="color:Blue; True
btnStop.Enabled = <span style="color:Blue; True
btnStart.Enabled = <span style="color:Blue; False
<span style="color:Blue; Else
<span style="color:Green;
<span style="color:Green; Error connecting to device close window
<span style="color:Green;
DestroyWindow(hHwnd)

btnSave.Enabled = <span style="color:Blue; False
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Green;
<span style="color:Blue; Else
lstDevices.Items.Add(<span style="color:#A31515; "No Capture Device")
btnStart.Enabled = <span style="color:Blue; False
<span style="color:Blue; End <span style="color:Blue; If

btnStop.Enabled = <span style="color:Blue; False
btnSave.Enabled = <span style="color:Blue; False
picCapture.SizeMode = PictureBoxSizeMode.StretchImage
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub LoadDeviceList()
<span style="color:Blue; Dim strName <span style="color:Blue; As <span style="color:Blue; String = Space(100)
<span style="color:Blue; Dim strVer <span style="color:Blue; As <span style="color:Blue; String = Space(100)
<span style="color:Blue; Dim bReturn <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Blue; Dim x <span style="color:Blue; As <span style="color:Blue; Integer = 0

<span style="color:Green;
<span style="color:Green; Load name of all avialable devices into the lstDevices
<span style="color:Green;

<span style="color:Blue; Do
<span style="color:Green;
<span style="color:Green; Get Driver name and version
<span style="color:Green;
bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)

<span style="color:Green;
<span style="color:Green; If there was a device add device name to the list
<span style="color:Green;
<span style="color:Blue; If bReturn <span style="color:Blue; Then lstDevices.Items.Add(strName.Trim)
x += 1
<span style="color:Blue; Loop <span style="color:Blue; Until bReturn = <span style="color:Blue; False
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub OpenPreviewWindow()
<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;
<span style="color:Green; Open Preview window in picturebox
<span style="color:Green;
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE <span style="color:Blue; Or WS_CHILD, 0, 0, 640, _
480, picCapture.Handle.ToInt32, 0)

<span style="color:Green;
<span style="color:Green; Connect to device
<span style="color:Green;
<span style="color:Blue; If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) <span style="color:Blue; Then
<span style="color:Green;
<span style="color:Green; Set the preview scale
<span style="color:Green;
SendMessage(hHwnd, WM_CAP_SET_SCALE, <span style="color:Blue; True, 0)

<span style="color:Green;
<span style="color:Green; Set the preview rate in milliseconds
<span style="color:Green;
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)

<span style="color:Green;
<span style="color:Green; Start previewing the image from the camera
<span style="color:Green;
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, <span style="color:Blue; True, 0)

<span style="color:Green;
<span style="color:Green; Resize window to fit in picturebox
<span style="color:Green;
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
SWP_NOMOVE <span style="color:Blue; Or SWP_NOZORDER)

btnSave.Enabled = <span style="color:Blue; True
btnStop.Enabled = <span style="color:Blue; True
btnStart.Enabled = <span style="color:Blue; False
<span style="color:Blue; Else
<span style="color:Green;
<span style="color:Green; Error connecting to device close window
<span style="color:Green;
DestroyWindow(hHwnd)

btnSave.Enabled = <span style="color:Blue; False
<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 btnStart_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 btnStart.Click
iDevice = lstDevices.SelectedIndex
OpenPreviewWindow()
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub ClosePreviewWindow()
<span style="color:Green;
<span style="color:Green; Disconnect from device
<span style="color:Green;
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)

<span style="color:Green;
<span style="color:Green; close window
<span style="color:Green;

DestroyWindow(hHwnd)
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Sub btnStop_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 btnStop.Click
ClosePreviewWindow()
btnSave.Enabled = <span style="color:Blue; False
btnStart.Enabled = <span style="color:Blue; True
btnStop.Enabled = <span style="color:Blue; False
<span style="color:Blue; End <span style="color:Blue; Sub

<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;
<span style="color:Green; Copy image to clipboard
<span style="color:Green;
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)

<span style="color:Green;
<span style="color:Green; Get image from clipboard and convert it to a bitmap
<span style="color:Green;
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
ClosePreviewWindow()
btnSave.Enabled = <span style="color:Blue; False
btnStop.Enabled = <span style="color:Blue; False
btnStart.Enabled = <span style="color:Blue; True

<span style="color:Blue; If sfdImage.ShowDialog = DialogResult.OK <span style="color:Blue; Then
bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
<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 Form1_Closing(<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.ComponentModel.CancelEventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Closing
<span style="color:Blue; If btnStop.Enabled <span style="color:Blue; Then
ClosePreviewWindow()
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class

[/code]
<br/>
<br/>


View the full article
 
Back
Top