C# .NET - WebCam Video Capture VB.Net Windows Application to C#.Net Web Application

Asked By Basha on 01-Jun-10 06:25 AM
Hi, Below is a VB.net Windows application to capture WebCam Video. I need to convert this code from VB.Net Windows Application to C#.Net Web Application.

Could you please provide me the solution for this.

Thank You.


Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Class Form1
    Inherits System.Windows.Forms.Form
    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 CamSource As Integer
    '---used as a window handle---
    Dim hWnd As Integer

    Private Sub cameraSource()
        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
                ListBox1.Items.Add(DriverName.Trim)
            End If
        Next

    End Sub
    Private Sub previewCamera(ByVal pbCtrl As PictureBox)
        hWnd = capCreateCaptureWindowA(CamSource, _
            WS_VISIBLE Or WS_CHILD, 0, 0, 0, _
            0, pbCtrl.Handle.ToInt32, 0)
        If SendMessage( _
           hWnd, WM_CAP_DRIVER_CONNECT, _
           CamSource, 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
    Private Sub stopPreviewCamera()
        SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, CamSource, 0)
        DestroyWindow(hWnd)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cameraSource()

        Button4.Enabled = False
        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = False
    End Sub
    Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        Label1.Visible = False
        previewCamera(PictureBox1)
        Button1.Enabled = True
        Button4.Enabled = False
        Button2.Enabled = True
    End Sub
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        CamSource = ListBox1.SelectedIndex

        '---preview the selected video source
    End Sub
    'stop preview
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        stopPreviewCamera()
        Button4.Enabled = True
        Button1.Enabled = False
    End Sub
    ' recording
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Button3.Enabled = True
        Button2.Enabled = False
        SendMessage(hWnd, WM_CAP_SEQUENCE, 0, 0)

    End Sub
    ' stop recording and ask to save video
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'Dim save As Integer
        'save = MsgBox("Do you want to save your recording video", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "Recording Video")
        'If (save = MsgBoxResult.Yes) Then
        '    Dim saveName As New SaveFileDialog
        '    saveName.Filter = "Avi file(*.avi)|*.avi"
        '    If saveName.ShowDialog = DialogResult.OK Then
        SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, "C:\RecordedVideo.avi")
        'SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, saveName.FileName)
        '    End If
        'End If

        'Me.Cursor = System.Windows.Forms.Cursors.Default
        Button2.Enabled = True
        Button3.Enabled = False
    End Sub
    ' preview
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        CamSource = ListBox1.SelectedIndex
        previewCamera(PictureBox1)
        Button4.Enabled = False
        Button1.Enabled = True
    End Sub

End Class


DL M replied to Basha on 01-Jun-10 06:52 AM
i try to convert vb.net to c# code

show code in c# code here will got you.


using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;

public class Form1 : System.Windows.Forms.Form
{
    const  WM_CAP_START = 0x400;
    const  WS_CHILD = 0x40000000;

    const  WS_VISIBLE = 0x10000000;
    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 = 0x2;
    const  SWP_NOSIZE = 1;
    const  SWP_NOZORDER = 0x4;
    const  HWND_BOTTOM = 1;
    [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    public static extern bool capGetDriverDescriptionA(short wDriverIndex, string lpszName, int cbName, string lpszVer, int cbVer);
    [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    public static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWnd, int nID);
    [DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    //--The capGetDriverDescription function retrieves the version
    // description of the capture driver--

    //--The capCreateCaptureWindow function creates a capture window--

    //--This function sends the specified message to a window or windows--
    public static extern int SendMessage(int hwnd, int Msg, int wParam,     [MarshalAs(UnmanagedType.AsAny)]
object lParam);
    [DllImport("user32", EntryPoint = "SetWindowPos", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
    [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    public static extern bool DestroyWindow(int hndw);

    //--Sets the position of the window relative to the screen buffer--

    //--This function destroys the specified window--

    //---used to identify the video source---
    int CamSource;
    //---used as a window handle---

    int hWnd;
    private void cameraSource()
    {
        string DriverName = Strings.Space(80);
        string DriverVersion = Strings.Space(80);
        for (int i = 0; i <= 9; i++) {
            if (capGetDriverDescriptionA(i, DriverName, 80, DriverVersion, 80)) {
                ListBox1.Items.Add(DriverName.Trim);
            }
        }

    }
    private void previewCamera(PictureBox pbCtrl)
    {
        hWnd = capCreateCaptureWindowA(CamSource, WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, pbCtrl.Handle.ToInt32, 0);
        if (SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, CamSource, 0)) {
            //---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 | SWP_NOZORDER);
        } else {
            //--error connecting to video source---
            DestroyWindow(hWnd);
        }

    }
    private void stopPreviewCamera()
    {
        SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, CamSource, 0);
        DestroyWindow(hWnd);

    }

    private void  // ERROR: Handles clauses are not supported in C#
Form1_Load(System.Object sender, System.EventArgs e)
    {
        cameraSource();

        Button4.Enabled = false;
        Button1.Enabled = false;
        Button2.Enabled = false;
        Button3.Enabled = false;
    }
    private void  ListBox1_DoubleClick(System.Object sender, System.EventArgs e)
    {
        Label1.Visible = false;
        previewCamera(PictureBox1);
        Button1.Enabled = true;
        Button4.Enabled = false;
        Button2.Enabled = true;
    }

    private void ListBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
    {
        CamSource = ListBox1.SelectedIndex;

        //---preview the selected video source
    }
    //stop preview
    private void  Button1_Click(System.Object sender, System.EventArgs e)
    {
        stopPreviewCamera();
        Button4.Enabled = true;
        Button1.Enabled = false;
    }
    // recording

    private void Button2_Click(System.Object sender, System.EventArgs e)
    {
        Button3.Enabled = true;
        Button2.Enabled = false;
        SendMessage(hWnd, WM_CAP_SEQUENCE, 0, 0);

    }
    // stop recording and ask to save video
    private void Button3_Click(System.Object sender, System.EventArgs e)
    {
        //Dim save As Integer
        //save = MsgBox("Do you want to save your recording video", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "Recording Video")
        //If (save = MsgBoxResult.Yes) Then
        //    Dim saveName As New SaveFileDialog
        //    saveName.Filter = "Avi file(*.avi)|*.avi"
        //    If saveName.ShowDialog = DialogResult.OK Then
        SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, "C:\\RecordedVideo.avi");
        //SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, saveName.FileName)
        //    End If
        //End If

        //Me.Cursor = System.Windows.Forms.Cursors.Default
        Button2.Enabled = true;
        Button3.Enabled = false;
    }
    // preview
    private void  Button4_Click(System.Object sender, System.EventArgs e)
    {
        CamSource = ListBox1.SelectedIndex;
        previewCamera(PictureBox1);
        Button4.Enabled = false;
        Button1.Enabled = true;
    }

}
Super Man replied to Basha on 01-Jun-10 07:03 AM

1.  using System;

2.  using System.IO;

3.  using System.Runtime.InteropServices;

4.  using Microsoft.Win32;

5.   

6.  public class Form1 : System.Windows.Forms.Form

7.  {

8.      const var WM_CAP_START = 0x400;

9.      const var WS_CHILD = 0x40000000;

10.     const var WS_VISIBLE = 0x10000000;

11.    

12.     const var WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;

13.     const var WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;

14.     const var WM_CAP_EDIT_COPY = WM_CAP_START + 30;

15.     const var WM_CAP_SEQUENCE = WM_CAP_START + 62;

16.     const var WM_CAP_FILE_SAVEAS = WM_CAP_START + 23;

17.    

18.     const var WM_CAP_SET_SCALE = WM_CAP_START + 53;

19.     const var WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;

20.     const var WM_CAP_SET_PREVIEW = WM_CAP_START + 50;

21.    

22.     const var SWP_NOMOVE = 0x2;

23.     const var SWP_NOSIZE = 1;

24.     const var SWP_NOZORDER = 0x4;

25.     const var HWND_BOTTOM = 1;

26.     [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

27.     public static extern bool capGetDriverDescriptionA(short wDriverIndex, string lpszName, int cbName, string lpszVer, int cbVer);

28.     [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

29.     public static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWnd, int nID);

30.     [DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

31.     //--The capGetDriverDescription function retrieves the version

32.     // description of the capture driver--

33.    

34.     //--The capCreateCaptureWindow function creates a capture window--

35.    

36.     //--This function sends the specified message to a window or windows--

37.     public static extern int SendMessage(int hwnd, int Msg, int wParam,     [MarshalAs(UnmanagedType.AsAny)]

38. object lParam);

39.     [DllImport("user32", EntryPoint = "SetWindowPos", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

40.     public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);

41.     [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

42.     public static extern bool DestroyWindow(int hndw);

43.    

44.     //--Sets the position of the window relative to the screen buffer--

45.    

46.     //--This function destroys the specified window--

47.    

48.     //---used to identify the video source---

49.     int CamSource;

50.     //---used as a window handle---

51.     int hWnd;

52.    

53.     private void cameraSource()

54.     {

55.         string DriverName = Strings.Space(80);

56.         string DriverVersion = Strings.Space(80);

57.         for (int i = 0; i <= 9; i++) {

58.             if (capGetDriverDescriptionA(i, DriverName, 80, DriverVersion, 80)) {

59.                 ListBox1.Items.Add(DriverName.Trim);

60.             }

61.            

62.         }

63.     }

64.     private void previewCamera(PictureBox pbCtrl)

65.     {

66.         hWnd = capCreateCaptureWindowA(CamSource, WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, pbCtrl.Handle.ToInt32, 0);

67.         if (SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, CamSource, 0)) {

68.             //---set the preview scale---

69.             SendMessage(hWnd, WM_CAP_SET_SCALE, true, 0);

70.             //---set the preview rate (ms)---

71.             SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0);

72.             //---start previewing the image---

73.             SendMessage(hWnd, WM_CAP_SET_PREVIEW, true, 0);

74.             //---resize window to fit in PictureBox control---

75.             SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, pbCtrl.Width, pbCtrl.Height, SWP_NOMOVE | SWP_NOZORDER);

76.         }

77.         else {

78.             //--error connecting to video source---

79.             DestroyWindow(hWnd);

80.            

81.         }

82.     }

83.     private void stopPreviewCamera()

84.     {

85.         SendMessage(hWnd, WM_CAP_DRIVER_DISCONNECT, CamSource, 0);

86.            

87.         DestroyWindow(hWnd);

88.     }

89.    

90.     private void // ERROR: Handles clauses are not supported in C# Form1_Load(System.Object sender, System.EventArgs e)

91.     {

92.         cameraSource();

93.        

94.         Button4.Enabled = false;

95.         Button1.Enabled = false;

96.         Button2.Enabled = false;

97.         Button3.Enabled = false;

98.     }

99.     private void // ERROR: Handles clauses are not supported in C# ListBox1_DoubleClick(System.Object sender, System.EventArgs e)

100.             {

101.                 Label1.Visible = false;

102.                 previewCamera(PictureBox1);

103.                 Button1.Enabled = true;

104.                 Button4.Enabled = false;

105.                 Button2.Enabled = true;

106.             }

107.             private void // ERROR: Handles clauses are not supported in C# ListBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)

108.             {

109.                

110.                    

111.                     //---preview the selected video source

112.                 CamSource = ListBox1.SelectedIndex;

113.             }

114.             //stop preview

115.             private void // ERROR: Handles clauses are not supported in C# Button1_Click(System.Object sender, System.EventArgs e)

116.             {

117.                 stopPreviewCamera();

118.                 Button4.Enabled = true;

119.                 Button1.Enabled = false;

120.             }

121.             // recording

122.             private void // ERROR: Handles clauses are not supported in C# Button2_Click(System.Object sender, System.EventArgs e)

123.             {

124.                

125.                 Button3.Enabled = true;

126.                 Button2.Enabled = false;

127.                    

128.                 SendMessage(hWnd, WM_CAP_SEQUENCE, 0, 0);

129.             }

130.             // stop recording and ask to save video

131.             private void // ERROR: Handles clauses are not supported in C# Button3_Click(System.Object sender, System.EventArgs e)

132.             {

133.                 //Dim save As Integer

134.                 //save = MsgBox("Do you want to save your recording video", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "Recording Video")

135.                 //If (save = MsgBoxResult.Yes) Then

136.                 // Dim saveName As New SaveFileDialog

137.                 // saveName.Filter = "Avi file(*.avi)|*.avi"

138.                 // If saveName.ShowDialog = DialogResult.OK Then

139.                 SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, "C:\\RecordedVideo.avi");

140.                 //SendMessage(hWnd, WM_CAP_FILE_SAVEAS, 0, saveName.FileName)

141.                 // End If

142.                 //End If

143.                

144.                 //Me.Cursor = System.Windows.Forms.Cursors.Default

145.                 Button2.Enabled = true;

146.                 Button3.Enabled = false;

147.             }

148.             // preview

149.             private void // ERROR: Handles clauses are not supported in C# Button4_Click(System.Object sender, System.EventArgs e)

150.             {

151.                 CamSource = ListBox1.SelectedIndex;

152.                 previewCamera(PictureBox1);

153.                 Button4.Enabled = false;

154.                 Button1.Enabled = true;

155.             }

156.            

157.         }

 

Vasanthakumar D replied to Basha on 01-Jun-10 07:09 AM
Hi,

Lot of free Code converters are there for this. find some eg below...

http://www.developerfusion.com/tools/convert/vb-to-csharp/
http://www.dotnetspider.com/convert/vb-to-csharp.aspx
Basha replied to DL M on 01-Jun-10 07:47 AM

Hi Lalit,

Thanks for your time and help. I have some queries here.

1. public class Form1 : System.Windows.Forms.Form  ( library is not there in Web C#.net )
2. When I compiled this code it says PictureBox  is missing as PictureBox is not supported in Web application.

Could you pleae tell me is there any other alternative for this.

Thank You.

Regargs,
Basha


Basha replied to Vasanthakumar D on 01-Jun-10 09:11 AM

Hi,

I have some queries here.

When I compiled this C#.net code I am getting error PictureBox  library is missing as PictureBox is not supported in Web application.

Could you pleae tell me is there any other alternative for this.

Thank You.

Regargs,
Basha


Vasanthakumar D replied to Basha on 02-Jun-10 01:33 AM
Hi,

is your application web or windows?... if its a web application, there is no picturebox. Instead of picturebox, use Image control.
Basha replied to Vasanthakumar D on 03-Jun-10 01:31 AM

Hi,

Any idea of the below part as I am getting error at strings.space(80)


private void cameraSource()

54.     {

55.         string DriverName = Strings.Space(80);

56.         string DriverVersion = Strings.Space(80);

57.         for (int i = 0; i <= 9; i++) {

58.             if (capGetDriverDescriptionA(i, DriverName, 80, DriverVersion, 80)) {

59.                 ListBox1.Items.Add(DriverName.Trim);

60.             }

61.            

62.         }

63.     }

sourabh malani replied to Basha on 09-Jul-10 09:18 AM

Hello Friends,


Try this code for cameraSource in C#.NET


private void cameraSource()
{    
        string Name = String.Empty.PadRight(100);
        string Version = String.Empty.PadRight(100);
        bool EndOfDeviceList = false;
        short index = 0;

        // Load name of all avialable devices into the lstDevices .
        do
        {
          // Get Driver name and version
          EndOfDeviceList = capGetDriverDescriptionA(index, ref Name, 100, ref Version, 100);
          // If there was a device add device name to the list
          if (EndOfDeviceList) ListBox1.Items.Add(Name.Trim());
          index += 1;
        }
        while (!(EndOfDeviceList == false));        
  }