Saving an AVI file with avicap32.dll

I can't figure out why this avi file won't save.

Page 1 of 1

10 Replies - 36893 Views - Last Post: 05 July 2010 - 05:22 AM Rate Topic: -----

#1 EEE  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 16-August 07

Saving an AVI file with avicap32.dll

Post icon  Posted 08 October 2007 - 11:36 PM

The following is code is for a windows form that I took straight from the code project website. Everything is working fine except when I stop recording, it's not saving where I want it to. I think the problem is the very last sub, but I cannot figure out what it is.

 
Imports System
Imports System.Runtime.InteropServices

Public Class Form1
	Const WM_CAP_START = &H400S
	Const WS_CHILD = &H40000000
	Const WS_VISIBLE = &H10000000

	Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
	Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
	Const WM_CAP_EDIT_COPY = WM_CAP_START + 30
	Const WM_CAP_SEQUENCE = WM_CAP_START + 62
	Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23

	Const WM_CAP_SET_SCALE = WM_CAP_START + 53
	Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
	Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50

	Const SWP_NOMOVE = &H2S
	Const SWP_NOSIZE = 1
	Const SWP_NOZORDER = &H4S
	Const HWND_BOTTOM = 1

	'--The capGetDriverDescription function retrieves the version 
	' description of the capture driver--
	Declare Function capGetDriverDescriptionA Lib "avicap32.dll" _
	   (ByVal wDriverIndex As Short, _
		ByVal lpszName As String, ByVal cbName As Integer, _
		ByVal lpszVer As String, _
		ByVal cbVer As Integer) As Boolean

	'--The capCreateCaptureWindow function creates a capture window--
	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 hWnd As Integer, _
		ByVal nID As Integer) As Integer

	'--This function sends the specified message to a window or windows--
	Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
	   (ByVal hwnd As Integer, ByVal Msg As Integer, _
		ByVal wParam As Integer, _
	   <MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer

	'--Sets the position of the window relative to the screen buffer--
	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

	'--This function destroys the specified window--
	Declare Function DestroyWindow Lib "user32" _
	   (ByVal hndw As Integer) As Boolean

	'---used to identify the video source---
	Dim VideoSource As Integer
	'---used as a window handle---
	Dim hWnd As Integer

	Private Sub Form1_Load( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) Handles MyBase.Load
		btnStartRecording.Enabled = True
		btnStopRecording.Enabled = False
		'---list all the video sources---
		ListVideoSources()
	End Sub
	Private Sub ListVideoSources()
		Dim DriverName As String = Space(80)
		Dim DriverVersion As String = Space(80)
		For i As Integer = 0 To 9
			If capGetDriverDescriptionA(i, DriverName, 80, _
			   DriverVersion, 80) Then
				lstVideoSources.Items.Add(DriverName.Trim)
			End If
		Next
	End Sub
	'---list all the video sources---
	Private Sub lstVideoSources_SelectedIndexChanged( _
	   ByVal sender As System.Object, ByVal e As System.EventArgs) _
	   Handles lstVideoSources.SelectedIndexChanged
		'---check which video source is selected---
		VideoSource = lstVideoSources.SelectedIndex
		'---preview the selected video source
		PreviewVideo(pbCtrl)
	End Sub
	'---preview the selected video source---
	Private Sub PreviewVideo(ByVal pbCtrl As PictureBox)
		hWnd = capCreateCaptureWindowA(VideoSource, _
			WS_VISIBLE Or WS_CHILD, 0, 0, 0, _
			0, pbCtrl.Handle.ToInt32, 0)
	   
		If SendMessage( _
		   hWnd, WM_CAP_DRIVER_CONNECT, _
		   VideoSource, 0) Then

			'---set the preview scale---
			SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
			'---set the preview rate (ms)---
			SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0)
			'---start previewing the image---
			SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0)
			'---resize window to fit in PictureBox control---
			SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, _
			   pbCtrl.Width, pbCtrl.Height, _
			   SWP_NOMOVE Or SWP_NOZORDER)
		Else
			'--error connecting to video source---
			DestroyWindow(hWnd)
		End If
	End Sub
	'---stop the preview window---
	Private Sub btnStopCamera_Click( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) _
	   Handles btnStopCamera.Click
		StopPreviewWindow()
	End Sub
	'--disconnect from video source---
	Private Sub StopPreviewWindow()
		SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
		DestroyWindow(hWnd)
	End Sub
	'---Start recording the video---
	Private Sub btnStartRecording_Click( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) _
	   Handles btnStartRecording.Click
		btnStartRecording.Enabled = False
		btnStopRecording.Enabled = True
		'---start recording---
		SendMessage(hWnd, WM_CAP_SEQUENCE, 0, 0)
	End Sub
	'---stop recording and save it on file---
	Private Sub btnStopRecording_Click( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) _
	   Handles btnStopRecording.Click
		btnStartRecording.Enabled = True
		btnStopRecording.Enabled = False
		'---save the recording to file---
		SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, "C:\Users\UserName\Videos\recordedvideo.avi")

	End Sub



End Class



*Fixed your code tags, alsmo moving to the VB.Net Forum as this is a VB.Net question :)

This post has been edited by PsychoCoder: 09 October 2007 - 12:04 AM


Is This A Good Question/Topic? 1

Replies To: Saving an AVI file with avicap32.dll

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Saving an AVI file with avicap32.dll

Posted 09 October 2007 - 12:05 AM

Where is it saving the file to and are you getting any errors?

This post has been edited by PsychoCoder: 09 October 2007 - 12:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 EEE  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 16-August 07

Re: Saving an AVI file with avicap32.dll

Posted 09 October 2007 - 08:27 AM

I'm saving into my video folder under my user name.
"C:\Users\Cal\Videos\recordedvideo.avi"

I tried putting a file in that folder with that name hoping that avicap32 needed an existing file to write to: that didn't work.

No errors.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Saving an AVI file with avicap32.dll

Posted 09 October 2007 - 09:36 AM

Ok, if theres no errors put you code inside a Try..>Catch block as something isnt happening or an exceptions happening and isnt being displayed
Was This Post Helpful? 0
  • +
  • -

#5 EEE  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 16-August 07

Re: Saving an AVI file with avicap32.dll

Posted 09 October 2007 - 11:38 PM

 '---stop recording and save it on file---
	Private Sub btnStopRecording_Click( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) _
	   Handles btnStopRecording.Click
		btnStartRecording.Enabled = True
		btnStopRecording.Enabled = False
		'---save the recording to file---
		Try
			SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, "C:\Users\Calvo\Videos\recordedvideo.avi")
			MsgBox("success")
		Catch ex As Exception
			MsgBox(ex.ToString)
		End Try


	End Sub


Above is the new try/catch that I put in. No errors though.

The whole avicap32.dll is a big mystery to me. I've been to a bunch of different sites and everyone seems to know how to set the constants (such as WM_CAP_FILE_SAVEAS), but I went to the microsoft MSDN area (http://msdn2.microsoft.com/en-us/library/ms713477.aspx) and it says nothing about what to set the constants to.

below are a couple of sites that I've been to in case you're interested in context:
http://www.devx.com/...t...le&trk=MSCP
http://msdn2.microso...y/ms713477.aspx
http://weblogs.asp.n...les/webcam.aspx
Was This Post Helpful? 0
  • +
  • -

#6 f0tis  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 10
  • Joined: 11-March 09

Re: Saving an AVI file with avicap32.dll

Posted 11 March 2009 - 10:55 AM

hey there nice tutorial but i have a problem....the error is IstVideoSources what is this?
istvideosources is a....??can you help me must i add prefence or rename something please answer
as soon as posible ...cheers
tnanks yours
f0tis
Was This Post Helpful? 0
  • +
  • -

#7 developerman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 28-March 08

Re: Saving an AVI file with avicap32.dll

Posted 28 March 2009 - 03:05 AM

View Postf0tis, on 11 Mar, 2009 - 09:55 AM, said:

hey there nice tutorial but i have a problem....the error is IstVideoSources what is this?
istvideosources is a....??can you help me must i add prefence or rename something please answer
as soon as posible ...cheers
tnanks yours
f0tis


Lstvideo sources, lst is meant be a listbox I think that is where all the confusion is

hope that helps

regards
Was This Post Helpful? 0
  • +
  • -

#8 gajendra_delhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-April 09

Re: Saving an AVI file with avicap32.dll

Posted 13 April 2009 - 09:22 PM

View Postdeveloperman, on 28 Mar, 2009 - 02:05 AM, said:

View Postf0tis, on 11 Mar, 2009 - 09:55 AM, said:

Hey, this is nicearticle but i have one question How can i retrieve camera name instead of camera driver name?


regards
Gajendra Kumar

Was This Post Helpful? 0
  • +
  • -

#9 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: Saving an AVI file with avicap32.dll

Posted 06 November 2009 - 09:48 PM

Sorry for bringing up this old thread - but this code isn't working for me... why? Everything looks fine, no compilation errors - but when I click "btnstoprecording" - I just get a "success" msgbox, and the file doesn't save.

This post has been edited by LeisureProgrammer: 06 November 2009 - 11:13 PM

Was This Post Helpful? 0
  • +
  • -

#10 mookie77  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 27-October 09

Re: Saving an AVI file with avicap32.dll

Posted 13 December 2009 - 08:04 PM

View PostLeisureProgrammer, on 6 Nov, 2009 - 08:48 PM, said:

Sorry for bringing up this old thread - but this code isn't working for me... why? Everything looks fine, no compilation errors - but when I click "btnstoprecording" - I just get a "success" msgbox, and the file doesn't save.


Click on the vidcap itself, go into the properties and check the CaptureFile location; should be able to set it there, i.e. C:\Capture.avi or whatever you like.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Ranjeet*


Reputation:

Re: Saving an AVI file with avicap32.dll

Posted 05 July 2010 - 05:22 AM

View PostEEE, on 08 October 2007 - 10:36 PM, said:

The following is code is for a windows form that I took straight from the code project website. Everything is working fine except when I stop recording, it's not saving where I want it to. I think the problem is the very last sub, but I cannot figure out what it is.

 
Imports System
Imports System.Runtime.InteropServices

Public Class Form1
	Const WM_CAP_START = &H400S
	Const WS_CHILD = &H40000000
	Const WS_VISIBLE = &H10000000

	Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
	Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
	Const WM_CAP_EDIT_COPY = WM_CAP_START + 30
	Const WM_CAP_SEQUENCE = WM_CAP_START + 62
	Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23

	Const WM_CAP_SET_SCALE = WM_CAP_START + 53
	Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
	Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50

	Const SWP_NOMOVE = &H2S
	Const SWP_NOSIZE = 1
	Const SWP_NOZORDER = &H4S
	Const HWND_BOTTOM = 1

	'--The capGetDriverDescription function retrieves the version 
	' description of the capture driver--
	Declare Function capGetDriverDescriptionA Lib "avicap32.dll" _
	   (ByVal wDriverIndex As Short, _
		ByVal lpszName As String, ByVal cbName As Integer, _
		ByVal lpszVer As String, _
		ByVal cbVer As Integer) As Boolean

	'--The capCreateCaptureWindow function creates a capture window--
	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 hWnd As Integer, _
		ByVal nID As Integer) As Integer

	'--This function sends the specified message to a window or windows--
	Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
	   (ByVal hwnd As Integer, ByVal Msg As Integer, _
		ByVal wParam As Integer, _
	   <MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer

	'--Sets the position of the window relative to the screen buffer--
	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

	'--This function destroys the specified window--
	Declare Function DestroyWindow Lib "user32" _
	   (ByVal hndw As Integer) As Boolean

	'---used to identify the video source---
	Dim VideoSource As Integer
	'---used as a window handle---
	Dim hWnd As Integer

	Private Sub Form1_Load( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) Handles MyBase.Load
		btnStartRecording.Enabled = True
		btnStopRecording.Enabled = False
		'---list all the video sources---
		ListVideoSources()
	End Sub
	Private Sub ListVideoSources()
		Dim DriverName As String = Space(80)
		Dim DriverVersion As String = Space(80)
		For i As Integer = 0 To 9
			If capGetDriverDescriptionA(i, DriverName, 80, _
			   DriverVersion, 80) Then
				lstVideoSources.Items.Add(DriverName.Trim)
			End If
		Next
	End Sub
	'---list all the video sources---
	Private Sub lstVideoSources_SelectedIndexChanged( _
	   ByVal sender As System.Object, ByVal e As System.EventArgs) _
	   Handles lstVideoSources.SelectedIndexChanged
		'---check which video source is selected---
		VideoSource = lstVideoSources.SelectedIndex
		'---preview the selected video source
		PreviewVideo(pbCtrl)
	End Sub
	'---preview the selected video source---
	Private Sub PreviewVideo(ByVal pbCtrl As PictureBox)
		hWnd = capCreateCaptureWindowA(VideoSource, _
			WS_VISIBLE Or WS_CHILD, 0, 0, 0, _
			0, pbCtrl.Handle.ToInt32, 0)
	   
		If SendMessage( _
		   hWnd, WM_CAP_DRIVER_CONNECT, _
		   VideoSource, 0) Then

			'---set the preview scale---
			SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
			'---set the preview rate (ms)---
			SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0)
			'---start previewing the image---
			SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0)
			'---resize window to fit in PictureBox control---
			SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, _
			   pbCtrl.Width, pbCtrl.Height, _
			   SWP_NOMOVE Or SWP_NOZORDER)
		Else
			'--error connecting to video source---
			DestroyWindow(hWnd)
		End If
	End Sub
	'---stop the preview window---
	Private Sub btnStopCamera_Click( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) _
	   Handles btnStopCamera.Click
		StopPreviewWindow()
	End Sub
	'--disconnect from video source---
	Private Sub StopPreviewWindow()
		SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, VideoSource, 0)
		DestroyWindow(hWnd)
	End Sub
	'---Start recording the video---
	Private Sub btnStartRecording_Click( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) _
	   Handles btnStartRecording.Click
		btnStartRecording.Enabled = False
		btnStopRecording.Enabled = True
		'---start recording---
		SendMessage(hWnd, WM_CAP_SEQUENCE, 0, 0)
	End Sub
	'---stop recording and save it on file---
	Private Sub btnStopRecording_Click( _
	   ByVal sender As System.Object, _
	   ByVal e As System.EventArgs) _
	   Handles btnStopRecording.Click
		btnStartRecording.Enabled = True
		btnStopRecording.Enabled = False
		'---save the recording to file---
		SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, "C:\Users\UserName\Videos\recordedvideo.avi")

	End Sub



End Class



*Fixed your code tags, alsmo moving to the VB.Net Forum as this is a VB.Net question :)

Was This Post Helpful? 0

Page 1 of 1