locked
Webcam recording but will not save to file RRS feed

  • Question

  •        I have downloaded a program which is used to capture an image via the webcam on my laptop. That works fine. I added the required code to record and save the recorded video to an .avi file. The record function appears to be working fine. When I click on the Stop Record button the save file dialog appears and I enter in a file name to save the recorded video. But the message WM_CAP_FILE_SAVEAS does not appear to work as no file ever gets created in the location I choose (desktop).

            I have been on many, many, many links comparing my code for this to what apparently works fine for others. Can anybody tell me what the problem is?

    'Start recording
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            DeviceID = lstDevices.SelectedIndex
            OpenPreviewWindow()
            SendMessage(hHwnd, WM_CAP_SEQUENCE, 0, 0)
        End Sub
    
        'Stop recording and save file
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim saveName As New SaveFileDialog
            saveName.Filter = "Avi file(*.avi)|*.avi"
            If saveName.ShowDialog = DialogResult.OK Then
                SendMessage(hHwnd, WM_CAP_FILE_SAVEAS, 0, saveName.FileName)
            End If
            ClosePreviewWindow()
        End Sub


    You've taught me everything I know but not everything you know.


    • Edited by Mr. Monkeyboy Sunday, June 10, 2012 4:19 AM correction
    Sunday, June 10, 2012 1:43 AM

Answers

  • In your first "SendMessage" declaration, the last parameter is passed ByRef. Correct is ByVal.

    Be aware that your code only works as a 32 bit process. It fails as a 64 bit process. You may consider changing this later.

    EDIT: You find out what a function returns by assigning it to a variable and examining it's content.

             Dim result As Integer
    
             result = SendMessage(hHwnd, WM_CAP_FILE_SAVEAS, 0, saveName.FileName)
    
             If result = 0 Then
                'error occured
             Else
                'no problem
             End If
    



    Armin


    Sunday, June 10, 2012 5:16 PM

All replies

  • What does SendMessage return? Show us all involved declarations. If not already done, use Option Strict On.

    Armin

    Sunday, June 10, 2012 11:10 AM
  •         I will try the Option Strict On option Armin Zingler but here is the code for now which is mostly from a website Cor Ligthert (hope I got that right) maintains. Also I am too new at this to figure out how to obtain what SendMessage returns. On another note I had to use System.Threading.Thread.Sleep in order to give the camera time to receive and intitiate a bunch of commands that were being sent to it at the same time in order to get the initial program to work correctly.

                                                                               Thanks for any assistance you can provide....

    Public Class Form1
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstDevices.SelectedIndexChanged
    
        End Sub
    
        Private Sub sfdImage_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles sfdImage.FileOk
    
        End Sub
    
        Const WM_CAP_STOP As Integer = WM_CAP + 61
        Const WM_CAP_FILE_SET_CAPTURE_FILE As Integer = WM_CAP + 63
        Const WM_CAP_SEQUENCE As Integer = WM_CAP + 62
        Const WM_CAP_FILE_SAVEAS As Long = WM_CAP + 23
        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
        Public Const WM_CAP_GET_STATUS As Integer = WM_CAP + 54
        Public Const WM_CAP_DLG_VIDEOFORMAT As Integer = WM_CAP + 41
        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
        Private DeviceID As Integer = 0 ' Current device ID
        Private 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, _
            ByRef lParam As String) As Integer
        Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
            (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
            ByRef lParam As CAPSTATUS) As Boolean
        Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
           (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Boolean, _
           ByRef lParam As Integer) As Boolean
        Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
             (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
             ByRef lParam As Integer) As Boolean
        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
        Structure POINTAPI
            Dim x As Integer
            Dim y As Integer
        End Structure
    
        Public Structure CAPSTATUS
            Dim uiImageWidth As Integer                    '// Width of the image
            Dim uiImageHeight As Integer                   '// Height of the image
            Dim fLiveWindow As Integer                     '// Now Previewing video?
            Dim fOverlayWindow As Integer                  '// Now Overlaying video?
            Dim fScale As Integer                          '// Scale image to client?
            Dim ptScroll As POINTAPI                    '// Scroll position
            Dim fUsingDefaultPalette As Integer            '// Using default driver palette?
            Dim fAudioHardware As Integer                  '// Audio hardware present?
            Dim fCapFileExists As Integer                  '// Does capture file exist?
            Dim dwCurrentVideoFrame As Integer             '// # of video frames cap'td
            Dim dwCurrentVideoFramesDropped As Integer     '// # of video frames dropped
            Dim dwCurrentWaveSamples As Integer            '// # of wave samples cap'td
            Dim dwCurrentTimeElapsedMS As Integer          '// Elapsed capture duration
            Dim hPalCurrent As Integer                     '// Current palette in use
            Dim fCapturingNow As Integer                   '// Capture in progress?
            Dim dwReturn As Integer                        '// Error value after any operation
            Dim wNumVideoAllocated As Integer              '// Actual number of video buffers
            Dim wNumAudioAllocated As Integer              '// Actual number of audio buffers
        End Structure
    
        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
            Me.AutoScrollMinSize = New Size(100, 100)
            btnStop.Enabled = False
            btnSave.Enabled = False
            btnInfo.Enabled = False
        End Sub
    
        Private Sub LoadDeviceList()
            Dim strName As String = Space(100)
            Dim strVer As String = Space(100)
            Dim bReturn As Boolean
            Dim x As Short = 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 += CType(1, Short)
            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(DeviceID.ToString, WS_VISIBLE Or WS_CHILD, 0, 0, 1280, _
                1024, picCapture.Handle.ToInt32, 0)
            '
            ' Connect to device
            '
            If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, DeviceID, 0) Then
                '
                'Set the preview scale
                '
                SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
    
                System.Threading.Thread.Sleep(100)
                '
                'Set the preview rate in milliseconds
                '
                SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
    
                System.Threading.Thread.Sleep(100)
                '
                'Start previewing the image from the camera
                '
                SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
    
                System.Threading.Thread.Sleep(100)
                '
                ' 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
                btnInfo.Enabled = True
            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
            DeviceID = lstDevices.SelectedIndex
            OpenPreviewWindow()
            Dim bReturn As Boolean
            Dim s As CAPSTATUS
            bReturn = SendMessage(hHwnd, WM_CAP_GET_STATUS, Marshal.SizeOf(s), s)
            Debug.WriteLine(String.Format("Video Size {0} x {1}", s.uiImageWidth, s.uiImageHeight))
        End Sub
    
        Private Sub ClosePreviewWindow()
            '
            ' Disconnect from device
            '
            SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, DeviceID, 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
            btnInfo.Enabled = False
            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 Bitmap
    
            '
            ' 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)), Bitmap)
                picCapture.Image = bmap
                ClosePreviewWindow()
                btnSave.Enabled = False
                btnStop.Enabled = False
                btnStart.Enabled = True
                btnInfo.Enabled = False
                Trace.Assert(Not (bmap Is Nothing))
                If sfdImage.ShowDialog = DialogResult.OK Then
                    bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
                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
    
        Private Sub btnInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInfo.Click
            SendMessage(hHwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0)
        End Sub
    
        'Start recording
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            DeviceID = lstDevices.SelectedIndex
            OpenPreviewWindow()
            SendMessage(hHwnd, WM_CAP_SEQUENCE, 0, 0)
        End Sub
    
        'Stop recording and save file
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim saveName As New SaveFileDialog
            saveName.Filter = "Avi file(*.avi)|*.avi"
            If saveName.ShowDialog = DialogResult.OK Then
                SendMessage(hHwnd, WM_CAP_FILE_SAVEAS, 0, saveName.FileName)
            End If
            ClosePreviewWindow()
        End Sub
    End Class


    You've taught me everything I know but not everything you know.


    • Edited by Mr. Monkeyboy Sunday, June 10, 2012 3:56 PM Added information
    Sunday, June 10, 2012 3:49 PM
  • In your first "SendMessage" declaration, the last parameter is passed ByRef. Correct is ByVal.

    Be aware that your code only works as a 32 bit process. It fails as a 64 bit process. You may consider changing this later.

    EDIT: You find out what a function returns by assigning it to a variable and examining it's content.

             Dim result As Integer
    
             result = SendMessage(hHwnd, WM_CAP_FILE_SAVEAS, 0, saveName.FileName)
    
             If result = 0 Then
                'error occured
             Else
                'no problem
             End If
    



    Armin


    Sunday, June 10, 2012 5:16 PM
  •       Thank you Armin Zingler I wil fix those errors and also do your test. Again thanks alot.

    You've taught me everything I know but not everything you know.

    Sunday, June 10, 2012 6:53 PM
  •       Armin Zingler.....I did your test and received 0 for both SendMessage(hHwnd, WM_CAP_SEQUENCE, 0, 0) &  SendMessage(hHwnd, WM_CAP_FILE_SAVEAS, 0, saveName.FileName). I received a -1 for  SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0). I changed the code you show to this to get it to work;

    Dim result As Integer = 5
            result = CInt(SendMessage(hHwnd, WM_CAP_SEQUENCE, 0, 0))
            MessageBox.Show(CStr(result))

        Thanks for being so helpful and if you have any more ideas please let me know. I will continue in my quest...


     

    You've taught me everything I know but not everything you know.

    Sunday, June 10, 2012 8:26 PM
  • If WM_CAP_SEQUENCE returns 0, call System.Runtime.InteropServices.Marshal.GetLastWin32Error directly afterwards to get the error number. Then look up the number here: http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx

    Armin

    Sunday, June 10, 2012 8:44 PM
  •         Armin Zingler I will give that a try. Thanks again.

    You've taught me everything I know but not everything you know.

    Sunday, June 10, 2012 8:46 PM
  •       Armin Zingler the error returned was 1400 for SendMessage(hHwnd, WM_CAP_SEQUENCE, 0, 0)

          (I'm pretty sure I can fix the error 123)

    ERROR_INVALID_WINDOW_HANDLE
    1400 (0x578)

    Invalid window handle.

    and 123 for  SendMessage(hHwnd, WM_CAP_FILE_SAVEAS, 0, saveName.FileName)

    ERROR_INVALID_NAME
    123 (0x7B)

    The filename, directory name, or volume label syntax is incorrect.


    You've taught me everything I know but not everything you know.

    Sunday, June 10, 2012 9:04 PM
  • If the error is 1400, the hHwnd passed is either zero or, otherwise, the capture window has already been closed.

    Did you already say if it's running as a 64 bit process?


    Armin


    Sunday, June 10, 2012 9:10 PM
  •    I changed everything to ByVal like you said to do and I just got a -1 returned a couple of times on starting recording as well as mostly 1400's. I guess -1 means it is actually recording??? Now I'm working on the file issue to see what is wrong there.

    You've taught me everything I know but not everything you know.

    Sunday, June 10, 2012 9:20 PM
  • You've changed "everything"? I've mentioned only the last param of the first SendMessage declaration.

    -1 means TRUE. The meaning of the return value is explained in the documentation. For example here (WM_CAP_SEQUENCE): http://msdn.microsoft.com/en-us/library/dd743916(VS.85).aspx

    Again, is it a 32 bit process?


    Armin

    Sunday, June 10, 2012 9:37 PM
  •  I'm using MessageBox.Show(CStr(result) & "     "& (ErrorVal)) where "result" is the SendMessage result of either 0 (doesn't work) or -1 (working) and ErrorVal as the Marshal.GetLastWin32Error() result which is either nothing or 1400 for

    SendMessage(hHwnd, WM_CAP_SEQUENCE, 0, 0) and always 123 for SendMessage(hHwnd, WM_CAP_FILE_SAVEAS, 0, 

     "C:\users\john\desktop\Record"). Sometimes I will get just a -1 for SEQUENCE with no Win32 Error at all and all of the time I get 0 and 123 for SAVEAS.


    You've taught me everything I know but not everything you know.

    Sunday, June 10, 2012 9:42 PM
  • In your first SendMessage declaration, try changing it to 

     Declare Ansi Function SendMessage ...


    Armin

    Sunday, June 10, 2012 9:48 PM
  •        I get the same responses. But thanks for your assistance Armin Zingler.

    You've taught me everything I know but not everything you know.

    Sunday, June 10, 2012 10:15 PM
  •      On another note Acer Crystal Eye Webcam application came with my laptop and it has zero problems recording or snapshooting and saving files from the internally installed webcam of the laptop. So I am just letting you know this to zero out the possibility of anything being corrupted on my computer as the problem.

    You've taught me everything I know but not everything you know.

    Sunday, June 10, 2012 10:25 PM
  • I had the same problem now i use this:

    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
            Dim save As Integer
            save = MsgBox("Do you want to save the video", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "Recording")
            If (save = MsgBoxResult.Yes) Then
                Dim savevideo As New SaveFileDialog
                savevideo.Filter = "Avi file(*.avi)|*.avi"
                If savevideo.ShowDialog = DialogResult.OK Then
                    SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, savevideo.FileName)
                End If
            End If

        End Sub

    it is working although i seem to have problems with the cursor after saving, but it may be my computer's fault


    Wednesday, August 21, 2013 4:46 PM