Posted By

AdrianVasilyev on 02/06/15


Tagged

image video frame stream capture camera usb c#


Versions (?)

How to capture frames from a camera in C#


 / Published in: C#
 

URL: http://www.camera-sdk.com/

If you wish to make your home, office or warehouse more secure, taking frame captures can be a very useful solution for you. And I can tell you this because I’ve already implemented this function at my office. Now I am sharing the prewritten source code with you for such a solution.

What will you need for this function?

• A Visual C# WPF Application created in Visual Studio • The VoIPSDK.dll added to the references. (It can be found on the website where I found the code: www.camera-sdk.com/)

The first part of the code under MainForm.cs is the code for the frame capturing program itself. It contains all the details you need to implement this function. Later, under MainForm.Designer.cs you’ll find the source code for the GUI of this program. The code will help you to develop a user interface that will make it easy to use the program.

Good luck with implementing the code!

  1. // MainForm.cs
  2.  
  3.  
  4. using System;
  5. using System.Windows.Forms;
  6. using Ozeki.Media.MediaHandlers;
  7. using Ozeki.Media.MediaHandlers.Video;
  8.  
  9. namespace FrameCaptureFromUSBCamera
  10. {
  11. public partial class MainForm : Form
  12. {
  13. private FrameCapture _capture;
  14.  
  15. private WebCamera _webCamera;
  16.  
  17. private MediaConnector _connector;
  18.  
  19. private DrawingImageProvider _liveProvider;
  20.  
  21. private DrawingImageProvider _frameCaptureProvider;
  22.  
  23. private int _index = 0;
  24.  
  25. public MainForm()
  26. {
  27. InitializeComponent();
  28. _liveProvider = new DrawingImageProvider();
  29. _frameCaptureProvider = new DrawingImageProvider();
  30.  
  31. _capture = new FrameCapture();
  32. _connector = new MediaConnector();
  33. }
  34.  
  35. private void connectBt_Click(object sender, EventArgs e)
  36. {
  37. try
  38. {
  39. disconnectBt.Enabled = true;
  40. startBt.Enabled = true;
  41.  
  42. _webCamera = WebCamera.GetDefaultDevice();
  43.  
  44. _connector.Connect(_webCamera, _liveProvider);
  45.  
  46. _connector.Connect(_webCamera, _capture);
  47. _connector.Connect(_capture, _frameCaptureProvider);
  48.  
  49. liveViewer.SetImageProvider(_liveProvider);
  50. liveViewer.Start();
  51.  
  52. captureViewer.SetImageProvider(_frameCaptureProvider);
  53. captureViewer.Start();
  54.  
  55. _webCamera.Start();
  56.  
  57. connectBt.Enabled = false;
  58. }
  59. catch (Exception ex)
  60. {
  61. MessageBox.Show(ex.Message);
  62. }
  63. }
  64.  
  65. private void disconnectBt_Click(object sender, EventArgs e)
  66. {
  67. try
  68. {
  69. _capture.Stop();
  70. _webCamera.Stop();
  71.  
  72. _connector.Disconnect(_webCamera, _liveProvider);
  73.  
  74. _connector.Disconnect(_webCamera, _capture);
  75. _connector.Disconnect(_capture, _frameCaptureProvider);
  76.  
  77. liveViewer.Stop();
  78. captureViewer.Stop();
  79.  
  80. _capture.Dispose();
  81.  
  82. connectBt.Enabled = true;
  83. disconnectBt.Enabled = false;
  84. startBt.Enabled = false;
  85. stopBt.Enabled = false;
  86. }
  87. catch (Exception ex)
  88. {
  89. MessageBox.Show(ex.Message);
  90. }
  91. }
  92.  
  93. private void startBt_Click(object sender, EventArgs e)
  94. {
  95. try
  96. {
  97. var sec = int.Parse(secondText.Text);
  98.  
  99. _capture.SetInterval(new TimeSpan(0, 0, 0, sec));
  100. _capture.Start();
  101. _capture.OnFrameCaptured += _capture_OnFrameCaptured;
  102.  
  103. startBt.Enabled = false;
  104. stopBt.Enabled = true;
  105. }
  106. catch (Exception ex)
  107. {
  108. MessageBox.Show(ex.Message);
  109. }
  110. }
  111.  
  112. void _capture_OnFrameCaptured(object sender, Ozeki.Media.Video.Snapshot e)
  113. {
  114. var image = e.ToImage();
  115. image.Save("test"+_index+".jpg");
  116. }
  117.  
  118. private void stopBt_Click(object sender, EventArgs e)
  119. {
  120. _capture.Stop();
  121. _capture.OnFrameCaptured -= _capture_OnFrameCaptured;
  122. startBt.Enabled = true;
  123. stopBt.Enabled = false;
  124. }
  125. }
  126. }
  127.  
  128.  
  129.  
  130. // MainForm.Designer.cs
  131.  
  132. namespace FrameCaptureFromUSBCamera
  133. {
  134. partial class MainForm
  135. {
  136. private System.ComponentModel.IContainer components = null;
  137.  
  138. protected override void Dispose(bool disposing)
  139. {
  140. if (disposing && (components != null))
  141. {
  142. components.Dispose();
  143. }
  144. base.Dispose(disposing);
  145. }
  146.  
  147. #region Windows Form Designer generated code
  148.  
  149. private void InitializeComponent()
  150. {
  151. this.connectBt = new System.Windows.Forms.Button();
  152. this.disconnectBt = new System.Windows.Forms.Button();
  153. this.secondText = new System.Windows.Forms.TextBox();
  154. this.label1 = new System.Windows.Forms.Label();
  155. this.groupBox1 = new System.Windows.Forms.GroupBox();
  156. this.liveViewer = new Ozeki.Media.Video.Controls.VideoViewerWF();
  157. this.groupBox2 = new System.Windows.Forms.GroupBox();
  158. this.captureViewer = new Ozeki.Media.Video.Controls.VideoViewerWF();
  159. this.startBt = new System.Windows.Forms.Button();
  160. this.stopBt = new System.Windows.Forms.Button();
  161. this.groupBox1.SuspendLayout();
  162. this.groupBox2.SuspendLayout();
  163. this.SuspendLayout();
  164. //
  165. // connectBt
  166. //
  167. this.connectBt.Location = new System.Drawing.Point(12, 9);
  168. this.connectBt.Name = "connectBt";
  169. this.connectBt.Size = new System.Drawing.Size(131, 23);
  170. this.connectBt.TabIndex = 0;
  171. this.connectBt.Text = "Connect to web camera";
  172. this.connectBt.UseVisualStyleBackColor = true;
  173. this.connectBt.Click += new System.EventHandler(this.connectBt_Click);
  174. //
  175. // disconnectBt
  176. //
  177. this.disconnectBt.Enabled = false;
  178. this.disconnectBt.Location = new System.Drawing.Point(698, 9);
  179. this.disconnectBt.Name = "disconnectBt";
  180. this.disconnectBt.Size = new System.Drawing.Size(169, 23);
  181. this.disconnectBt.TabIndex = 2;
  182. this.disconnectBt.Text = "Disconnect from web camera";
  183. this.disconnectBt.UseVisualStyleBackColor = true;
  184. this.disconnectBt.Click += new System.EventHandler(this.disconnectBt_Click);
  185. //
  186. // secondText
  187. //
  188. this.secondText.Location = new System.Drawing.Point(330, 11);
  189. this.secondText.Name = "secondText";
  190. this.secondText.Size = new System.Drawing.Size(106, 20);
  191. this.secondText.TabIndex = 4;
  192. //
  193. // label1
  194. //
  195. this.label1.AutoSize = true;
  196. this.label1.Location = new System.Drawing.Point(197, 16);
  197. this.label1.Name = "label1";
  198. this.label1.Size = new System.Drawing.Size(127, 13);
  199. this.label1.TabIndex = 5;
  200. this.label1.Text = "Seconds between frames";
  201. //
  202. // groupBox1
  203. //
  204. this.groupBox1.Controls.Add(this.liveViewer);
  205. this.groupBox1.Location = new System.Drawing.Point(12, 42);
  206. this.groupBox1.Name = "groupBox1";
  207. this.groupBox1.Size = new System.Drawing.Size(424, 428);
  208. this.groupBox1.TabIndex = 6;
  209. this.groupBox1.TabStop = false;
  210. this.groupBox1.Text = "Live";
  211. //
  212. // liveViewer
  213. //
  214. this.liveViewer.FlipMode = Ozeki.Media.Video.FlipMode.None;
  215. this.liveViewer.Location = new System.Drawing.Point(6, 19);
  216. this.liveViewer.Name = "liveViewer";
  217. this.liveViewer.RotateAngle = 0;
  218. this.liveViewer.Size = new System.Drawing.Size(412, 403);
  219. this.liveViewer.TabIndex = 0;
  220. this.liveViewer.Text = "liveViewer";
  221. //
  222. // groupBox2
  223. //
  224. this.groupBox2.Controls.Add(this.captureViewer);
  225. this.groupBox2.Location = new System.Drawing.Point(442, 42);
  226. this.groupBox2.Name = "groupBox2";
  227. this.groupBox2.Size = new System.Drawing.Size(424, 428);
  228. this.groupBox2.TabIndex = 7;
  229. this.groupBox2.TabStop = false;
  230. this.groupBox2.Text = "Frame capture";
  231. //
  232. // captureViewer
  233. //
  234. this.captureViewer.FlipMode = Ozeki.Media.Video.FlipMode.None;
  235. this.captureViewer.Location = new System.Drawing.Point(6, 19);
  236. this.captureViewer.Name = "captureViewer";
  237. this.captureViewer.RotateAngle = 0;
  238. this.captureViewer.Size = new System.Drawing.Size(413, 403);
  239. this.captureViewer.TabIndex = 0;
  240. this.captureViewer.Text = "captureViewer";
  241. //
  242. // startBt
  243. //
  244. this.startBt.Enabled = false;
  245. this.startBt.Location = new System.Drawing.Point(442, 9);
  246. this.startBt.Name = "startBt";
  247. this.startBt.Size = new System.Drawing.Size(75, 23);
  248. this.startBt.TabIndex = 8;
  249. this.startBt.Text = "Start";
  250. this.startBt.UseVisualStyleBackColor = true;
  251. this.startBt.Click += new System.EventHandler(this.startBt_Click);
  252. //
  253. // stopBt
  254. //
  255. this.stopBt.Enabled = false;
  256. this.stopBt.Location = new System.Drawing.Point(523, 9);
  257. this.stopBt.Name = "stopBt";
  258. this.stopBt.Size = new System.Drawing.Size(75, 23);
  259. this.stopBt.TabIndex = 9;
  260. this.stopBt.Text = "Stop";
  261. this.stopBt.UseVisualStyleBackColor = true;
  262. this.stopBt.Click += new System.EventHandler(this.stopBt_Click);
  263. //
  264. // MainForm
  265. //
  266. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  267. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  268. this.AutoSize = true;
  269. this.ClientSize = new System.Drawing.Size(879, 482);
  270. this.Controls.Add(this.stopBt);
  271. this.Controls.Add(this.startBt);
  272. this.Controls.Add(this.groupBox2);
  273. this.Controls.Add(this.groupBox1);
  274. this.Controls.Add(this.label1);
  275. this.Controls.Add(this.secondText);
  276. this.Controls.Add(this.disconnectBt);
  277. this.Controls.Add(this.connectBt);
  278. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
  279. this.Name = "FrameCapture";
  280. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  281. this.Text = "Frame capture";
  282. this.groupBox1.ResumeLayout(false);
  283. this.groupBox2.ResumeLayout(false);
  284. this.ResumeLayout(false);
  285. this.PerformLayout();
  286.  
  287. }
  288.  
  289. #endregion
  290.  
  291. private System.Windows.Forms.Button connectBt;
  292. private System.Windows.Forms.Button disconnectBt;
  293. private System.Windows.Forms.TextBox secondText;
  294. private System.Windows.Forms.Label label1;
  295. private System.Windows.Forms.GroupBox groupBox1;
  296. private System.Windows.Forms.GroupBox groupBox2;
  297. private Ozeki.Media.Video.Controls.VideoViewerWF liveViewer;
  298. private Ozeki.Media.Video.Controls.VideoViewerWF captureViewer;
  299. private System.Windows.Forms.Button startBt;
  300. private System.Windows.Forms.Button stopBt;
  301. }
  302. }

Report this snippet  

You need to login to post a comment.