|
|
How to Capture video from a video device such as a Webcam with VB .NET
Introduce
Understand VB.NET programming to capture video from a video device such as a WebCam that it is on your hand.
The Webcam in present is easy way to connect with PC that Windows XP will be auto detect as it has driver for webcam already.
We will be use the AVI Capture DLL(avicap32.dll) contains functions that incloude in Windows already for connecting to and listing available video capture device on your system .
Step by Step
- Open Visual Studio .NET Software.
- Select New Visual Basic Project on .NET software.
- Follow to click and place controls thus picture below .

- Go to the View Code windows by select menu View --> Code or Press F7
- Coding as thus detail below that it should be continue from line name is
"Windows form designer genarated code ".
First load a list of available video capture devices into a listbox.
To do this, use a loop that checks to see if there is a driver available. If there is, add it to a listbox.
' Create constant using attend in function of DLL file.
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
Dim iDevice As Integer = 0 ' Normal device ID
Dim hHwnd As Integer ' Handle value to preview window
' Declare function from AVI capture DLL.
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
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
' Connect to the device.
Private Sub LoadDeviceList()
Dim strName As String = Space(100)
Dim strVer As String = Space(100)
Dim bReturn As Boolean
Dim x As Integer = 0
' Load name of all avialable devices into the lstDevices .
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
' To display the output from a video capture device, you need to create a capture window.
Private Sub OpenPreviewWindow()
Dim iHeight As Integer = picCapture.Height
Dim iWidth As Integer = picCapture.Width
' Open Preview window in picturebox .
' Create a child window with capCreateCaptureWindowA so you can display it in a 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
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
btnSave.Enabled = True
btnStop.Enabled = True
btnStart.Enabled = False
Else
' Error connecting to device close window
DestroyWindow(hHwnd)
btnSave.Enabled = False
End If
End Sub
' Use SendMessage to copy the data to the clipboard Then transfer the image to the picture box.
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim data As IDataObject
Dim bmap As Image
' Copy image to clipboard
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
' Get image from clipboard and convert it to a bitmap
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
picCapture.Image = bmap
ClosePreviewWindow()
btnSave.Enabled = False
btnStop.Enabled = False
btnStart.Enabled = True
If sfdImage.ShowDialog = DialogResult.OK Then
bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
End If
End If
End Sub
' Finally, to close the preview window, disconnect from the device and destroy the preview window.
Private Sub ClosePreviewWindow()
' Disconnect from device
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
' close window
DestroyWindow(hHwnd)
End Sub
Run program by Press F5 as show thus picture below after that click Start Preview buttom .

Click to Get Webcam VB.NET Source Code
บทนำ
  บทความนี้เราจะมาทำความเข้าใจในการเขียนโปรแกรมติดต่อกล้องวีดิโอ หรือ ที่เราเรียกว่า webcam ที่คุณอาจจะมีอยู่ในมือแล้วขณะนี้
ซึ่งมันง่าย มากๆ ในการต่อ webcam โดยที่ใช้ Windows XP เพราะมันมี Driver ที่สนับสนุนกล้องนั้นอยู่แล้ว
ในการเขียนโปรแกรมเราจะใช้ AVI Capture DLL(avicap32.dll) ซึ่งจะมี functions(ฟังก์ชั่น) ในการติดต่อกับกล้อง ส่วนเจ้า DLL ไฟล์นี้ เป็นของ Windows ครับ มันอยู่แล้วในฮาร์ดดิสคุณ
ขั้นตอนการสร้างโปรแกรม
- เปิดโปรแกรม Visual Studio .NET.
- เลือกเมนู ชื่อ New Project แล้ว เลือกเป็น Visual Basic Project .
- จัดวางตัวคอนโทรลต่างๆ ตามรูปด้านล่าง ก็มี 1 Picture ,3 command button ,1 list box ครับ.

- ไปที่หน้า View Code windows โดยเลือก menu View --> Code หรือกด F7
- การเขียนโค๊ดโปแกรมด้านล่าง เราจะเขียนต่อจากบรรทัดที่ชื่อ
"Windows form designer genarated code ".
การทำงานของโปรแกรมอันดับแรกจะตรวจสอบหา Device กล้องในเครื่องคอมเราแล้วแสดงที่ List Box . การทำงานนี้เราใช้การเขียนแบบ loop ในการตรวจสอบหา Driver กล้องที่มีอยู่ในเครื่อง
' กำหนดค่าคงที่ในการใช้ function ของ DLL file.
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
Dim iDevice As Integer = 0 ' device ID ปัจจุบันในเครื่อง
Dim hHwnd As Integer ' ค่า Handle สำหรับการแสดงภาพในแตกละวิโดส์ว
' เรียก API function จาก AVI capture DLL.
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
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
' ติดต่อ device ของกล้อง.
Private Sub LoadDeviceList()
Dim strName As String = Space(100)
Dim strVer As String = Space(100)
Dim bReturn As Boolean
Dim x As Integer = 0
' เรียกรายชื่อของ device กล้องทั้งหมดในเครื่องคอมของเรา และโชว์ใน List Box ที่ชื่อ lstDevices .
Do
' เรียกชื่อ Driver และ version
bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
' ถ้าใช่ device ให้เพิ่มชื่อเข้าไปใน list box
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
' เปิดแสดงผลที่ picturebox .
' สร้าง window ลูก ขึ้นมาด้วย ฟังชั่น capCreateCaptureWindowA ซึ่งคุณสามารถเห็นใน picturebox.
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, _
480, picCapture.Handle.ToInt32, 0)
' ติดต่อกับ device
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
' ตั้งค่า preview scale
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
' ตั้งค่า preview rate ในระดับ milliseconds
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
' เริ่มต้นการแสดงภาพ จากงกล้อง
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
' หรับขนาด window ให้เท่ากับใน picturebox
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
btnSave.Enabled = True
btnStop.Enabled = True
btnStart.Enabled = False
Else
' การติดต่อdevice Error ให้ ปิด window
DestroyWindow(hHwnd)
btnSave.Enabled = False
End If
End Sub
' ใช้ฟังก์ชั่นชื่อ SendMessage ไป copy ข้อมูลไว้ใน clipboard ซึ่งย้ายภาพไปที่ picture box.
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim data As IDataObject
Dim bmap As Image
' Copy ภาพ ไป clipboard
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
'นำ ภาพ จาก clipboard และ convert มันไปเป็น bitmap
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
picCapture.Image = bmap
ClosePreviewWindow()
btnSave.Enabled = False
btnStop.Enabled = False
btnStart.Enabled = True
If sfdImage.ShowDialog = DialogResult.OK Then
bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
End If
End If
End Sub
' ขั้นตอนสุดท้าย, การปิดแสดงภาพใน โปรแกรมของเรา,เลิกทำการติดต่อ device และ destroy (ทำลาย Object) ที่ เราสร้างขึ้นมาตอนต้นการเขียนโปรแกรม ที่ให้แสดงภาพ และปิดโปรแกรม
Private Sub ClosePreviewWindow()
' เลิกติดต่อกับ device
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
' ปิด window
DestroyWindow(hHwnd)
End Sub
ทดสอบ Run โปรแกรม โดย กดปุ่ม F5 หลังจากที่มันแสดงหน้สต่างดังรูปด้านล่างก็ กดปุ่ม Start Preview เพื่อให้เริ่มทำการแสดงภาพ.

Click ลิ้งนี้สำหรับนำซอร์สโค๊ด Webcam VB.NET ไปทดลองเขียนการครับ
|