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.
}