Home General VB.NET Preview Webcam and capture image
20 | 05 | 2012
This site has been updated and renewed. You have followed an old link.

Click here to go to the new site

http://www.visiblevisual.com/jupgrade

Preview Webcam and capture image PDF Print E-mail
User Rating: / 6
PoorBest 
Tutorials

The code below will show the webcams image into a picturebox and can be saved with the use of a savebutton.

 

Webcam Preview

'Impression of the preview screen'

Show/Hidden vbnet code

View source
Imports System.IO
 
 
 
 
 
	Public Class FrmCap
 
	'Create constant using attend in function of DLL file.
 
 
 
	Const CAP As Short = &H400S
 
	Const CAP_DRIVER_CONNECT As Integer = CAP + 10
 
	Const CAP_DRIVER_DISCONNECT As Integer = CAP + 11
 
	Const CAP_EDIT_COPY As Integer = CAP + 30
 
	Const CAP_SET_PREVIEW As Integer = CAP + 50
 
	Const CAP_SET_PREVIEWRATE As Integer = CAP + 52
 
	Const CAP_SET_SCALE As Integer = 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
 
 
 
 
 
	Private Sub OpenForm()
 
	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, CAP_DRIVER_CONNECT, iDevice, 0) Then
 
 
 
	' Set the preview scale
 
	SendMessage(hHwnd, CAP_SET_SCALE, True, 0)
 
 
 
	' Set the preview rate in milliseconds
 
	SendMessage(hHwnd, CAP_SET_PREVIEWRATE, 66, 0)
 
 
 
	' Start previewing the image from the camera
 
	SendMessage(hHwnd, 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)
 
	Else
 
	' Error connecting to device close window
 
	DestroyWindow(hHwnd)
 
 
 
	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, 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
 
 
 
	Dim saveFileDialog1 As New SaveFileDialog()
 
	saveFileDialog1.Filter = "Jpeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
 
	saveFileDialog1.Title = "Save an Image File"
 
	saveFileDialog1.FileName = "Image001"
 
	saveFileDialog1.ShowDialog()
 
 
 
	' If the file name is not an empty string open it for saving.
 
	If saveFileDialog1.FileName <> "" Then
 
	' Saves the Image via a FileStream created by the OpenFile method.
 
	Dim fs As System.IO.FileStream = CType _
 
	(saveFileDialog1.OpenFile(), System.IO.FileStream)
 
	picCapture.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
 
	fs.Close()
 
	End If
 
 
 
	End If
 
	End Sub
 
 
 
	Private Sub frmcap_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
 
	' Disconnect from device
 
	SendMessage(hHwnd, CAP_DRIVER_DISCONNECT, iDevice, 0)
 
	' close window
 
	DestroyWindow(hHwnd)
 
	End Sub
 
 
 
 
 
	Private Sub FRMCAP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
	OpenForm()
 
	End Sub
 
	End Class

Attachments:
FileDescriptionFile sizeDownloads
Download this file (WindowsApplication1.zip)WebcampreviewsourceWebcam Image Save source80 Kb4867
 

Comments   

 
+1 #13 Guest 2012-02-07 11:25
Hey, it runs for first time then second time it asks for the video source and after selecting it it does not work no preview in the picturebox.

plz help
Quote
 
 
0 #12 Guest 2012-02-01 21:13
thanks dear thanks a lot
Quote
 
 
0 #11 Guest 2011-11-04 19:28
tnx u a lot.
Quote
 
 
0 #10 Guest 2011-11-02 17:24
Thanks for sharing this code. Really helps
Quote
 
 
0 #9 Guest 2011-11-01 09:41
its nice code,,
thanks i wait for face detection code :-)
Quote
 
 
0 #8 Guest 2011-10-17 18:47
Hi coder,

very nice coding, simpler but stronger...
If any one download it from this site use latest version vis studio, it wont open in 2005. So just copy the code and align it, it will work nice in 2005 too.
Quote
 
 
0 #7 Guest 2011-10-05 05:04
Its not working on Windows 7.
U can use it one time, close and if u Restart it will not work. Im looking arround quiet a time and there seems to be no solution. :sad:
Quote
 
 
0 #6 Guest 2011-08-31 15:32
Love the code, works great. The only issue I have is that the saved file is the scaled down image of the preview window not the full resolution of the webcam. Is there any way to capture the unscaled image other than making the preview picturebox the same size?
Any thoughts, ideas would be much appreciated.
Thanks
John
Quote
 
 
0 #5 Guest 2011-06-27 06:04
dude its working in windows 7
Quote
 
 
0 #4 Guest 2011-06-27 06:02
excellent code :-)
very much useful
Thanks.
Quote
 

Add comment


Security code
Refresh

This site has been updated and renewed. You have followed an old link.

Click here to go to the new site

http://www.visiblevisual.com/jupgrade