CODE Capture Image From Web Cam
CODE Capture Image From Web Cam
InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents picCapture As System.Windows.Forms.PictureBox
Friend WithEvents lstDevices As System.Windows.Forms.ListBox
Friend WithEvents lblDevice As System.Windows.Forms.Label
Friend WithEvents btnStart As System.Windows.Forms.Button
Friend WithEvents btnSave As System.Windows.Forms.Button
Friend WithEvents btnStop As System.Windows.Forms.Button
Friend WithEvents sfdImage As System.Windows.Forms.SaveFileDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.picCapture = New System.Windows.Forms.PictureBox
Me.lstDevices = New System.Windows.Forms.ListBox
Me.lblDevice = New System.Windows.Forms.Label
Me.btnStart = New System.Windows.Forms.Button
Me.btnSave = New System.Windows.Forms.Button
Me.btnStop = New System.Windows.Forms.Button
'
Me.btnStop.Location = New System.Drawing.Point(184, 320)
Me.btnStop.Name = "btnStop"
Me.btnStop.Size = New System.Drawing.Size(112, 32)
Me.btnStop.TabIndex = 5
Me.btnStop.Text = "Stop Preview"
'
'sfdImage
'
Me.sfdImage.Filter = "Jpeg|*.jpg"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(480, 382)
Me.Controls.Add(Me.btnStop)
Me.Controls.Add(Me.btnSave)
Me.Controls.Add(Me.btnStart)
Me.Controls.Add(Me.lblDevice)
Me.Controls.Add(Me.lstDevices)
Me.Controls.Add(Me.picCapture)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Video Capture"
CType(Me.picCapture, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
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
Const
Const
Const
Const
Const
Const
Const
Const
Do
'
' Get Driver name and version
'
bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
'
' If there was a device add device name to the list
'
If bReturn Then lstDevices.Items.Add(strName.Trim)
x += 1
Loop Until bReturn = False
End Sub
Private Sub OpenPreviewWindow()
Dim iHeight As Integer = picCapture.Height
Dim iWidth As Integer = picCapture.Width
'
' Open Preview window in picturebox
'
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0,
0, 640, _
480, picCapture.Handle.ToInt32, 0)
'
' Connect to device
'
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
'
'Set the preview scale
'
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
'
'Set the preview rate in milliseconds
'
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
'
'Start previewing the image from the camera
'
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
'
' Resize window to fit in picturebox
'
sfdImage.InitialDirectory = "\jpegs"
'name of file
sfdImage.FileName = "Sample-"
If sfdImage.ShowDialog = DialogResult.OK Then
'save as jpeg also sfdImange filter changes
bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Jpeg)
End If
End If
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If btnStop.Enabled Then
ClosePreviewWindow()
End If
End Sub
End Class