8 Replies - 98811 Views - Last Post: 24 May 2013 - 10:03 AM Rate Topic: -----

#1 hpeeze  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 03-April 09

webcam image capture using avicap.dll

Posted 20 May 2009 - 09:06 AM

I'm trying to capture an image from my webcam and save it as a .bmp file.
the code i have works perfectly except for 1 issue i cant figure out no matter what I try

the issue is that when i save the bitmap it only saves in 320x240 resolution even though I have set the resolution to 640x480 everywhere I could think of.

here is the (edited) code I currently have. some places I didn't know what to change so I put in 640,480 anyways to see what happened but that didnt make any difference and also didnt cause any errors so i left the changes there.

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 ' Current device ID
	Dim hHwnd As Integer ' Handle to preview window

	Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
		(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
		<MarshalAs(UnmanagedType.AsAny)> 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

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		LoadDeviceList()
		If lstDevices.Items.Count > 0 Then
			btnStart.Enabled = True
			lstDevices.SelectedIndex = 0
			btnStart.Enabled = True
		Else
			lstDevices.Items.Add("No Capture Device")
			btnStart.Enabled = False
		End If
		btnStop.Enabled = False
		btnSave.Enabled = False
		'picCapture.SizeMode = PictureBoxSizeMode.StretchImage
		picCapture.SizeMode = PictureBoxSizeMode.AutoSize


	End Sub

	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

	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
			'
			SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, 640, 480, _
					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

	Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
		iDevice = lstDevices.SelectedIndex
		OpenPreviewWindow()
	End Sub

	Private Sub ClosePreviewWindow()
		'
		' Disconnect from device
		'
		SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)

		'
		' close window
		'

		DestroyWindow(hHwnd)
	End Sub

	Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
		ClosePreviewWindow()
		btnSave.Enabled = False
		btnStart.Enabled = True
		btnStop.Enabled = False
	End Sub

	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, 640, 480)

		'
		' 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




I do not take credit for this code as I have not written it myself. the original code can be downloaded from the following link:
http://www.vb-helper...eo_capture.html

does anyone know how to make the program save the image as 640x480 instead of 320x240?
or how about a different code example that can save it as a 640x480 image and works in vb.net 2008?


also, im attaching the entire program here for anyone that wants to play around with what i have so far.

Attached File(s)



Is This A Good Question/Topic? 1

Replies To: webcam image capture using avicap.dll

#2 kyrotomia  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 05-May 07

Re: webcam image capture using avicap.dll

Posted 20 May 2009 - 03:16 PM

Hi there.
Just to let you know I haven't look at the code, maybe I will later on. However, I think I would let you know that, by using your provided example, the software saved a 640x480 .bmp file. So my first thought is that maybe your camera doesn't have a big enough resolution? You might have to resize (increase) the image by code to be able to output a 640x480 image with your camera.

Just my .02$
Was This Post Helpful? 0
  • +
  • -

#3 hpeeze  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 03-April 09

Re: webcam image capture using avicap.dll

Posted 21 May 2009 - 05:26 AM

thats what i thought at first when i was using an older camera. but i have switched to a Logitech Quickcam Pro 9000 webcam which is capable of taking at least 2mp pictures and up to 8mp with its included software.

i know its more than capable of producing an image at least 640x480
i was thinking maybe it could be some setting in the code i could change to make it save the image in a different resolution.

for now i've made the code resize the picture placed in the clipboard to 640x480 and it works just like it should. only problem with that is the end result comes out slightly blurry which is fine for now.
if possible i would like the program to take the picture at 640x480 so there is no scaling that needs to be done later on.

the difference between the scaled image and the exact same picture taken with its included software is very easy to see. the included software's image is very crisp and clear but the scaled image is well... scaled
Was This Post Helpful? 1

#4 Guest_Ruso*


Reputation:

Re: webcam image capture using avicap.dll

Posted 31 December 2010 - 01:39 PM

Gotta send WM_CAP_DLG_VIDEOFORMAT message to capture window. This will open webcam's video format dialog.

By the way, I found a serious flaw using avicap.... it was designed to capture video so you are limited to 640x480 EVEN though my webcam can handle 1920x1080 picture capture :(
Was This Post Helpful? 0

#5 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: webcam image capture using avicap.dll

Posted 31 December 2010 - 05:19 PM

Yes, but unless you want to record a movie in HD with your computer, there's not much point in doing that. avicap does it so that the images/videos captured are of a standard that can be streamed across the internet, and so that you can see the webcam's image on another computer with 500kbps internet.]

btw, dead zombie topic revival...
Was This Post Helpful? 1
  • +
  • -

#6 Guest_Elda*


Reputation:

Re: webcam image capture using avicap.dll

Posted 31 January 2011 - 11:16 PM

View Posthpeeze, on 20 May 2009 - 09:06 AM, said:

I'm trying to capture an image from my webcam and save it as a .bmp file.
the code i have works perfectly except for 1 issue i cant figure out no matter what I try

the issue is that when i save the bitmap it only saves in 320x240 resolution even though I have set the resolution to 640x480 everywhere I could think of.

here is the (edited) code I currently have. some places I didn't know what to change so I put in 640,480 anyways to see what happened but that didnt make any difference and also didnt cause any errors so i left the changes there.

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 ' Current device ID
	Dim hHwnd As Integer ' Handle to preview window

	Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
		(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
		<MarshalAs(UnmanagedType.AsAny)> 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

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		LoadDeviceList()
		If lstDevices.Items.Count > 0 Then
			btnStart.Enabled = True
			lstDevices.SelectedIndex = 0
			btnStart.Enabled = True
		Else
			lstDevices.Items.Add("No Capture Device")
			btnStart.Enabled = False
		End If
		btnStop.Enabled = False
		btnSave.Enabled = False
		'picCapture.SizeMode = PictureBoxSizeMode.StretchImage
		picCapture.SizeMode = PictureBoxSizeMode.AutoSize


	End Sub

	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

	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
			'
			SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, 640, 480, _
					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

	Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
		iDevice = lstDevices.SelectedIndex
		OpenPreviewWindow()
	End Sub

	Private Sub ClosePreviewWindow()
		'
		' Disconnect from device
		'
		SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)

		'
		' close window
		'

		DestroyWindow(hHwnd)
	End Sub

	Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
		ClosePreviewWindow()
		btnSave.Enabled = False
		btnStart.Enabled = True
		btnStop.Enabled = False
	End Sub

	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, 640, 480)

		'
		' 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




I do not take credit for this code as I have not written it myself. the original code can be downloaded from the following link:
http://www.vb-helper...eo_capture.html

does anyone know how to make the program save the image as 640x480 instead of 320x240?
or how about a different code example that can save it as a 640x480 image and works in vb.net 2008?


also, im attaching the entire program here for anyone that wants to play around with what i have so far.


Hi... Thanks a lot for this post and link. It helps alot. I just want to ask why there is a little difference whenever I use this in WinXP and Win7. It works perfectly in WinXP but why is it in Win7, it needs to pop up a form for video source.? Any idea on this? Please help. Thank you...
Was This Post Helpful? 0

#7 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: webcam image capture using avicap.dll

Posted 01 February 2011 - 02:06 AM

It's probably somehow to do with how the computer is configured - does windows 7 have the video device set as default?
Was This Post Helpful? 0
  • +
  • -

#8 sviking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 24-May 13

Re: webcam image capture using avicap.dll

Posted 24 May 2013 - 09:39 AM

thanks for help me!
I conncet 3 webcam with project application code Vb.net 2010 ok but i try conncet 8 webcam error. i don't know .
please help me code connect 8 webcam in one project
thanks all!
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: webcam image capture using avicap.dll

Posted 24 May 2013 - 10:03 AM

@sviking, this is an old thread, please start your own. Provide your relevant code and any error messages so we can help, otherwise we will not be able to assist you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1