Capture Camera from C Sharp

This sample will show you how to use the Camera Capture ActiveX from C# dot net. This ActiveX component is an OCX and it allows you to use any camera connected to your PC. You can display the camera, capture frame as an image, capture frames as a video, and more.


Requirements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.Compatibility;
 
namespace VIDEO_CAMERA_ACTIVEX_SAMPLE
{
/// <summary>
/// Summary description for frmMain.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
 
public frmMain()
{
	//
	// Required for Windows Form Designer support
	//
	InitializeComponent();
 
	//
	// TODO: Add any constructor code after InitializeComponent call
	//
	if (_InstancePtr == null) _InstancePtr = this;
}
 
internal System.Windows.Forms.Button cmdFrame;
internal System.Windows.Forms.Button cmdCapture;
internal System.Windows.Forms.Button cmdAudioFormat;
internal System.Windows.Forms.Button cmdSetCaptureFile;
internal System.Windows.Forms.Button cmdSavePalette;
internal System.Windows.Forms.Button cmdLoadPalette;
internal System.Windows.Forms.Button cmdCompression;
internal System.Windows.Forms.Button cmdDisplay;
internal System.Windows.Forms.Button cmdFormat;
internal System.Windows.Forms.Button cmdSource;
internal System.Windows.Forms.Button cmdConnect;
internal System.Windows.Forms.ComboBox cboDrivers;
private AxCamera_Capture_ActiveX.AxcCam axcCam1;
 
/// <summary>
/// Default instance for Form
/// </summary>
public static frmMain InstancePtr
{
	get
	{
		return _InstancePtr == null ? _InstancePtr = new frmMain() : _InstancePtr;
	}
}
protected static frmMain _InstancePtr = null;
 
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
	if( disposing )
	{
		if (components != null)
		{
			components.Dispose();
		}
	}
	base.Dispose( disposing );
}
 
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
	System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmMain));
	this.cmdFrame = new System.Windows.Forms.Button();
	this.cmdCapture = new System.Windows.Forms.Button();
	this.cmdAudioFormat = new System.Windows.Forms.Button();
	this.cmdSetCaptureFile = new System.Windows.Forms.Button();
	this.cmdSavePalette = new System.Windows.Forms.Button();
	this.cmdLoadPalette = new System.Windows.Forms.Button();
	this.cmdCompression = new System.Windows.Forms.Button();
	this.cmdDisplay = new System.Windows.Forms.Button();
	this.cmdFormat = new System.Windows.Forms.Button();
	this.cmdSource = new System.Windows.Forms.Button();
	this.cmdConnect = new System.Windows.Forms.Button();
	this.cboDrivers = new System.Windows.Forms.ComboBox();
	this.axcCam1 = new AxCamera_Capture_ActiveX.AxcCam();
	((System.ComponentModel.ISupportInitialize)(this.axcCam1)).BeginInit();
	this.SuspendLayout();
	// 
	// cmdFrame
	// 
	this.cmdFrame.Location = new System.Drawing.Point(328, 136);
	this.cmdFrame.Name = "cmdFrame";
	this.cmdFrame.Size = new System.Drawing.Size(80, 24);
	this.cmdFrame.TabIndex = 24;
	this.cmdFrame.Text = "Frame";
	this.cmdFrame.Click += new System.EventHandler(this.cmdFrame_Click);
	// 
	// cmdCapture
	// 
	this.cmdCapture.Location = new System.Drawing.Point(248, 136);
	this.cmdCapture.Name = "cmdCapture";
	this.cmdCapture.Size = new System.Drawing.Size(80, 24);
	this.cmdCapture.TabIndex = 23;
	this.cmdCapture.Text = "Capture";
	this.cmdCapture.Click += new System.EventHandler(this.cmdCapture_Click);
	// 
	// cmdAudioFormat
	// 
	this.cmdAudioFormat.Location = new System.Drawing.Point(144, 136);
	this.cmdAudioFormat.Name = "cmdAudioFormat";
	this.cmdAudioFormat.Size = new System.Drawing.Size(96, 24);
	this.cmdAudioFormat.TabIndex = 22;
	this.cmdAudioFormat.Text = "Audio Format";
	this.cmdAudioFormat.Click += new System.EventHandler(this.cmdAudioFormat_Click);
	// 
	// cmdSetCaptureFile
	// 
	this.cmdSetCaptureFile.Location = new System.Drawing.Point(40, 136);
	this.cmdSetCaptureFile.Name = "cmdSetCaptureFile";
	this.cmdSetCaptureFile.Size = new System.Drawing.Size(96, 24);
	this.cmdSetCaptureFile.TabIndex = 21;
	this.cmdSetCaptureFile.Text = "Set Capture File";
	this.cmdSetCaptureFile.Click += new System.EventHandler(this.cmdSetCaptureFile_Click);
	// 
	// cmdSavePalette
	// 
	this.cmdSavePalette.Location = new System.Drawing.Point(328, 104);
	this.cmdSavePalette.Name = "cmdSavePalette";
	this.cmdSavePalette.Size = new System.Drawing.Size(80, 24);
	this.cmdSavePalette.TabIndex = 20;
	this.cmdSavePalette.Text = "Save Palette";
	this.cmdSavePalette.Click += new System.EventHandler(this.cmdSavePalette_Click);
	// 
	// cmdLoadPalette
	// 
	this.cmdLoadPalette.Location = new System.Drawing.Point(328, 72);
	this.cmdLoadPalette.Name = "cmdLoadPalette";
	this.cmdLoadPalette.Size = new System.Drawing.Size(80, 24);
	this.cmdLoadPalette.TabIndex = 19;
	this.cmdLoadPalette.Text = "Load Palette";
	this.cmdLoadPalette.Click += new System.EventHandler(this.cmdLoadPalette_Click);
	// 
	// cmdCompression
	// 
	this.cmdCompression.Location = new System.Drawing.Point(128, 104);
	this.cmdCompression.Name = "cmdCompression";
	this.cmdCompression.Size = new System.Drawing.Size(80, 24);
	this.cmdCompression.TabIndex = 18;
	this.cmdCompression.Text = "Compression";
	this.cmdCompression.Click += new System.EventHandler(this.cmdCompression_Click);
	// 
	// cmdDisplay
	// 
	this.cmdDisplay.Location = new System.Drawing.Point(40, 104);
	this.cmdDisplay.Name = "cmdDisplay";
	this.cmdDisplay.Size = new System.Drawing.Size(80, 24);
	this.cmdDisplay.TabIndex = 17;
	this.cmdDisplay.Text = "Display";
	this.cmdDisplay.Click += new System.EventHandler(this.cmdDisplay_Click);
	// 
	// cmdFormat
	// 
	this.cmdFormat.Location = new System.Drawing.Point(128, 72);
	this.cmdFormat.Name = "cmdFormat";
	this.cmdFormat.Size = new System.Drawing.Size(80, 24);
	this.cmdFormat.TabIndex = 16;
	this.cmdFormat.Text = "Format";
	this.cmdFormat.Click += new System.EventHandler(this.cmdFormat_Click);
	// 
	// cmdSource
	// 
	this.cmdSource.Location = new System.Drawing.Point(40, 72);
	this.cmdSource.Name = "cmdSource";
	this.cmdSource.Size = new System.Drawing.Size(80, 24);
	this.cmdSource.TabIndex = 15;
	this.cmdSource.Text = "Source";
	this.cmdSource.Click += new System.EventHandler(this.cmdSource_Click);
	// 
	// cmdConnect
	// 
	this.cmdConnect.Location = new System.Drawing.Point(40, 40);
	this.cmdConnect.Name = "cmdConnect";
	this.cmdConnect.Size = new System.Drawing.Size(368, 24);
	this.cmdConnect.TabIndex = 14;
	this.cmdConnect.Text = "Connect";
	this.cmdConnect.Click += new System.EventHandler(this.cmdConnect_Click);
	// 
	// cboDrivers
	// 
	this.cboDrivers.Location = new System.Drawing.Point(40, 16);
	this.cboDrivers.Name = "cboDrivers";
	this.cboDrivers.Size = new System.Drawing.Size(368, 21);
	this.cboDrivers.TabIndex = 13;
	this.cboDrivers.Text = "ComboBox1";
	// 
	// axcCam1
	// 
	this.axcCam1.Enabled = true;
	this.axcCam1.Location = new System.Drawing.Point(40, 168);
	this.axcCam1.Name = "axcCam1";
	this.axcCam1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axcCam1.OcxState")));
	this.axcCam1.Size = new System.Drawing.Size(368, 256);
	this.axcCam1.TabIndex = 25;
	this.axcCam1.Error += new AxCamera_Capture_ActiveX.__cCam_ErrorEventHandler(this.axcCam1_Error);
	// 
	// frmMain
	// 
	this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
	this.BackColor = System.Drawing.SystemColors.Control;
	this.ClientSize = new System.Drawing.Size(450, 451);
	this.Controls.Add(this.axcCam1);
	this.Controls.Add(this.cmdFrame);
	this.Controls.Add(this.cmdCapture);
	this.Controls.Add(this.cmdAudioFormat);
	this.Controls.Add(this.cmdSetCaptureFile);
	this.Controls.Add(this.cmdSavePalette);
	this.Controls.Add(this.cmdLoadPalette);
	this.Controls.Add(this.cmdCompression);
	this.Controls.Add(this.cmdDisplay);
	this.Controls.Add(this.cmdFormat);
	this.Controls.Add(this.cmdSource);
	this.Controls.Add(this.cmdConnect);
	this.Controls.Add(this.cboDrivers);
	this.ForeColor = System.Drawing.SystemColors.ControlText;
	this.Name = "frmMain";
	this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
	this.Text = "Camera Capture ActiveX Sample 1.0";
	this.Load += new System.EventHandler(this.frmMain_Load);
	((System.ComponentModel.ISupportInitialize)(this.axcCam1)).EndInit();
	this.ResumeLayout(false);
 
}
#endregion
 
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
	Application.Run(new frmMain());
}
 
private void GetDrivers() 
{ 
 
	//Load the drivers 
	VBA.Collection cDrivers; // = new VBA.Collection(); 
	int cntr = 0; 
	object oDriverCnt = 0;
 
	cDrivers = axcCam1.GetDrivers(); 
 
	if (cDrivers.Count() > 0) 
	{ 
 
		for (cntr = 1; cntr <= cDrivers.Count(); cntr++) 
		{ 
			oDriverCnt = (object) cntr;
			cboDrivers.Items.Add(cDrivers.Item(ref oDriverCnt).ToString()); 
		} 
 
		//Select the first driver 
 
		cboDrivers.SelectedIndex = 0; 
 
	} 
} 
 
private void GetDriversX() 
{ 
 
	//Load the drivers without VB collection 
	int cntr = 0; 
	long lDriversCount = 0; 
	object oDriverCnt = 0;
 
	lDriversCount = axcCam1.GetDriversX(); 
 
	if (lDriversCount > 0) 
	{ 
 
		for (cntr = 1; cntr <= lDriversCount; cntr++) 
		{ 
			oDriverCnt = (object) cntr;
			cboDrivers.Items.Add(axcCam1.GetDriverXVal(cntr).ToString()); 
		} 
 
		//Select the first driver 
 
		cboDrivers.SelectedIndex = 0; 
 
	} 
} 
 
private void frmMain_Load(object sender, System.EventArgs e)
{
	//Init the control: 
	//Must be call before using any 
	//functionality of the control. 
	//sKey = Your registration key. 
	//On trial mode use the "Trial Mode." 
 
	axcCam1.Init("Trial Mode."); 
 
 
	GetDrivers(); 
 
	//Or get the drivers without VB collection 
	//GetDriversX() 
}
 
private void cmdConnect_Click(object sender, System.EventArgs e)
{
	//Connect to the driver 
 
	axcCam1.ConnectDriver(cboDrivers.SelectedIndex);  
 
}
 
private void cmdSource_Click(object sender, System.EventArgs e)
{
 
	//Set the source options
	axcCam1.SetSource();
 
}
 
private void cmdFormat_Click(object sender, System.EventArgs e)
{
	//Set format options
	axcCam1.SetFormat();
 
}
 
private void cmdLoadPalette_Click(object sender, System.EventArgs e)
{
 
	//Load Palette
	axcCam1.PaletteLoad();
 
}
 
private void cmdDisplay_Click(object sender, System.EventArgs e)
{
	//Set display options
	axcCam1.SetDisplay();
 
}
 
private void cmdCompression_Click(object sender, System.EventArgs e)
{
 
	//Set video compression
	axcCam1.SetCompression();
 
}
 
private void cmdSavePalette_Click(object sender, System.EventArgs e)
{
	//Save Palette
	axcCam1.PaletteSave();
 
}
 
private void cmdSetCaptureFile_Click(object sender, System.EventArgs e)
{
	//Set the capture video file
	axcCam1.SetCapFile("C:\\camera_capture.avi");
 
}
 
private void cmdAudioFormat_Click(object sender, System.EventArgs e)
{
	//Set audio compression
	axcCam1.SetAudioFormatDlg();
 
}
 
private void cmdCapture_Click(object sender, System.EventArgs e)
{
 
	int lFps = 15;
	bool bLimitSec = false;
	int lLimitSec = 30;
	bool bCaptureAudio = true;
	bool bUserHitOKToCapture = true;
	bool bAbortLeftMouse = true;
	bool bAbortRightMouse = true;
	bool bAVStreamMaster = true;
	int lIndexSize = -1;
 
	//Set the movie file
	axcCam1.SetCapFile("C:\\camera_capture.avi");
 
	//Start capture
	axcCam1.Capture(ref lFps, ref bLimitSec, ref lLimitSec, 
		ref bCaptureAudio, ref bUserHitOKToCapture, ref bAbortLeftMouse, 
		ref bAbortRightMouse, ref bAVStreamMaster, ref lIndexSize);
}
 
private void cmdFrame_Click(object sender, System.EventArgs e)
{
 
	//Capture frame to bmp
	axcCam1.SaveSingleFrame("C:\\CamCapture.bmp");
 
}
 
private void axcCam1_Error(object sender, AxCamera_Capture_ActiveX.__cCam_ErrorEvent e)
{
	//Display error message
	MessageBox.Show("Error: " + e.sErrMessage + "(" + e.lErrNumber + ")");
}
 
//=========================================================
 
}
}

Links:





read the original story ...
comments: Closed tags: , , , , ,

2 Responses to “Capture Camera from C Sharp”

  1. pharmacist says:

    What a great resource!

  2. math games says:

    post not working in firefox



Pings responses to this post