none
Using the Webcam with C# RRS feed

  • Question

  • Hi all,

    I've been programming in VB.Net for a while and decided to give Visual C# a try to learn something new.  Currently, I'm working on converting one of my VB applications to C#. The program has a feature that allows the user to capture images using the webcam via avicap32.dll and user32.dll.  It works fine in VB.Net, but when I put it into Telerik's code converter and put the result into a C# class, it threw a whopping 27 errors at me!  Here's the original code.  Can someone please tell me how to properly convert it so it works in C#?  Thanks in advance! 

    Jason

    The Code:

    Imports System.Runtime.InteropServices
    Imports System.Data.SQLite
    
    Module Camera
        Public FileName As String
        Public Const WM_USER As Integer = 1024
        Public Const WM_CAP As Short = &H400S
        Public Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
        Public Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
        Public Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
    
        Public Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
        Public Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
        Public Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
        Public Const WS_CHILD As Integer = &H40000000
        Public Const WS_VISIBLE As Integer = &H10000000
        Public Const SWP_NOMOVE As Short = &H2S
        Public Const SWP_NOSIZE As Short = 1
        Public Const SWP_NOZORDER As Short = &H4S
        Public Const HWND_BOTTOM As Short = 1
        Public Const WM_CAP_START As Long = WM_USER
    
        Public Const WM_CAP_DLG_VIDEOFORMAT As Integer = WM_CAP_START + 41
        Public Const WM_CAP_DLG_VIDEOSOURCE As Integer = WM_CAP_START + 42
        Public Const WM_CAP_DLG_VIDEODISPLAY As Integer = WM_CAP_START + 43
        Public Const WM_CAP_GET_VIDEOFORMAT As Integer = WM_CAP_START + 44
        Public Const WM_CAP_SET_VIDEOFORMAT As Integer = WM_CAP_START + 45
        Public Const WM_CAP_DLG_VIDEOCOMPRESSION As Integer = WM_CAP_START + 46
    
        Public iDevice As Integer = 0
        Public hHwnd As Integer
    
        Public 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
        Public 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
        Public Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
        Public 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
        Public 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
    
        Public Sub LoadDeviceList()
            Dim CurForm As Form = Form.ActiveForm
            Dim cmb As ComboBox = TryCast(CurForm.ActiveControl, ComboBox)
            If Not IsNothing(cmb) Then
                Dim strName As String = Space(100)
                Dim strVer As String = Space(100)
                Dim bReturn As Boolean
                Dim x As Integer = 0
                Do
                    bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
                    If bReturn Then cmb.Items.Add(strName.Trim)
                    x += 1
                Loop Until bReturn = False
            End If
        End Sub
        Public Sub OpenPreviewWindow()
            Dim CurForm As Form = Form.ActiveForm
            Dim pbx As PictureBox = TryCast(CurForm.ActiveControl, PictureBox)
            If Not IsNothing(pbx) Then
                Dim iHeight As Integer = pbx.Height
                Dim iWidth As Integer = pbx.Width
                hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, pbx.Handle.ToInt32, 0)
                If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
                    SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
                    SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
                    SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
                    SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, iWidth, iHeight, SWP_NOMOVE Or SWP_NOZORDER)
                Else
                    DestroyWindow(hHwnd)
                End If
            End If
        End Sub
        Public Sub ClosePreviewWindow()
            SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
            DestroyWindow(hHwnd)
        End Sub
        Public Function DeviceCount() As Integer
            Dim strName As String = Space(100)
            Dim strVer As String = Space(100)
            Dim bReturn As Boolean
            Dim x As Integer = 0
            Dim Devices As New List(Of String)
            Do
                bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
                If bReturn Then Devices.Add(strName.Trim)
            Loop Until bReturn = False
            Return Devices.Count
        End Function
    End Module

    Monday, June 18, 2018 6:14 AM

All replies

  • you can to using this tool WebCam_Capture.dll and then display in picturebox this.WebCamCapture.CaptureHeight = this.pictureBox1.Height; this.WebCamCapture.CaptureWidth = this.pictureBox1.Width; this.WebCamCapture.TimeToCapture_milliseconds = 100; this.WebCamCapture.Start(0);
    Monday, June 18, 2018 9:03 AM
  • Ahmed_Soft3,

    As it turns out, I was able to find the answer here.  Seems the problem was with the Code Converter site.  It didn't add some necessary code to make it work!

    • Proposed as answer by Dolen Zhang Wednesday, June 20, 2018 1:14 AM
    Tuesday, June 19, 2018 2:55 AM
  • Hi Jason, 

    I notice that your question is resolved by yourself. Please mark the reply as a answer in order to let others know that this case has been resolved and doesn't need to follow up further. Meanwhile, it also provide a reference for person who has the same issue. 

    Thanks for your cooperation.

    Regards,

    Dolen


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Dolen Zhang Wednesday, June 20, 2018 6:31 AM
    Wednesday, June 20, 2018 6:31 AM