Return to Snippet

Revision: 68658
at February 6, 2015 18:29 by AdrianVasilyev


Initial Code
// MainForm.cs


using System;
using System.Windows.Forms;
using Ozeki.Media.MediaHandlers;
using Ozeki.Media.MediaHandlers.Video;
 
namespace FrameCaptureFromUSBCamera
{
    public partial class MainForm : Form
    {
        private FrameCapture _capture;
 
        private WebCamera _webCamera;
 
        private MediaConnector _connector;
 
        private DrawingImageProvider _liveProvider;
 
        private DrawingImageProvider _frameCaptureProvider;
 
        private int _index = 0;
 
        public MainForm()
        {
            InitializeComponent();
            _liveProvider = new DrawingImageProvider();
            _frameCaptureProvider = new DrawingImageProvider();
 
            _capture = new FrameCapture();
            _connector = new MediaConnector();
        }
 
        private void connectBt_Click(object sender, EventArgs e)
        {
            try
            {
                disconnectBt.Enabled = true;
                startBt.Enabled = true;
 
                _webCamera = WebCamera.GetDefaultDevice();
 
                _connector.Connect(_webCamera, _liveProvider);
 
                _connector.Connect(_webCamera, _capture);
                _connector.Connect(_capture, _frameCaptureProvider);
 
                liveViewer.SetImageProvider(_liveProvider);
                liveViewer.Start();
 
                captureViewer.SetImageProvider(_frameCaptureProvider);
                captureViewer.Start();
 
                _webCamera.Start();
 
                connectBt.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        private void disconnectBt_Click(object sender, EventArgs e)
        {
            try
            {
                _capture.Stop();
                _webCamera.Stop();
 
                _connector.Disconnect(_webCamera, _liveProvider);
 
                _connector.Disconnect(_webCamera, _capture);
                _connector.Disconnect(_capture, _frameCaptureProvider);
 
                liveViewer.Stop();
                captureViewer.Stop();
 
                _capture.Dispose();
 
                connectBt.Enabled = true;
                disconnectBt.Enabled = false;
                startBt.Enabled = false;
                stopBt.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        private void startBt_Click(object sender, EventArgs e)
        {
            try
            {
                var sec = int.Parse(secondText.Text);
 
                _capture.SetInterval(new TimeSpan(0, 0, 0, sec));
                _capture.Start();
                _capture.OnFrameCaptured += _capture_OnFrameCaptured;
 
                startBt.Enabled = false;
                stopBt.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        void _capture_OnFrameCaptured(object sender, Ozeki.Media.Video.Snapshot e)
        {
            var image = e.ToImage();
            image.Save("test"+_index+".jpg");
        }
 
        private void stopBt_Click(object sender, EventArgs e)
        {
            _capture.Stop();
            _capture.OnFrameCaptured -= _capture_OnFrameCaptured;
            startBt.Enabled = true;
            stopBt.Enabled = false;
        }
    }
}



// MainForm.Designer.cs

namespace FrameCaptureFromUSBCamera
{
    partial class MainForm
    {
        private System.ComponentModel.IContainer components = null;
 
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        private void InitializeComponent()
        {
            this.connectBt = new System.Windows.Forms.Button();
            this.disconnectBt = new System.Windows.Forms.Button();
            this.secondText = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.liveViewer = new Ozeki.Media.Video.Controls.VideoViewerWF();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.captureViewer = new Ozeki.Media.Video.Controls.VideoViewerWF();
            this.startBt = new System.Windows.Forms.Button();
            this.stopBt = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // connectBt
            // 
            this.connectBt.Location = new System.Drawing.Point(12, 9);
            this.connectBt.Name = "connectBt";
            this.connectBt.Size = new System.Drawing.Size(131, 23);
            this.connectBt.TabIndex = 0;
            this.connectBt.Text = "Connect to web camera";
            this.connectBt.UseVisualStyleBackColor = true;
            this.connectBt.Click += new System.EventHandler(this.connectBt_Click);
            // 
            // disconnectBt
            // 
            this.disconnectBt.Enabled = false;
            this.disconnectBt.Location = new System.Drawing.Point(698, 9);
            this.disconnectBt.Name = "disconnectBt";
            this.disconnectBt.Size = new System.Drawing.Size(169, 23);
            this.disconnectBt.TabIndex = 2;
            this.disconnectBt.Text = "Disconnect from web camera";
            this.disconnectBt.UseVisualStyleBackColor = true;
            this.disconnectBt.Click += new System.EventHandler(this.disconnectBt_Click);
            // 
            // secondText
            // 
            this.secondText.Location = new System.Drawing.Point(330, 11);
            this.secondText.Name = "secondText";
            this.secondText.Size = new System.Drawing.Size(106, 20);
            this.secondText.TabIndex = 4;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(197, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(127, 13);
            this.label1.TabIndex = 5;
            this.label1.Text = "Seconds between frames";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.liveViewer);
            this.groupBox1.Location = new System.Drawing.Point(12, 42);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(424, 428);
            this.groupBox1.TabIndex = 6;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Live";
            // 
            // liveViewer
            // 
            this.liveViewer.FlipMode = Ozeki.Media.Video.FlipMode.None;
            this.liveViewer.Location = new System.Drawing.Point(6, 19);
            this.liveViewer.Name = "liveViewer";
            this.liveViewer.RotateAngle = 0;
            this.liveViewer.Size = new System.Drawing.Size(412, 403);
            this.liveViewer.TabIndex = 0;
            this.liveViewer.Text = "liveViewer";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.captureViewer);
            this.groupBox2.Location = new System.Drawing.Point(442, 42);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(424, 428);
            this.groupBox2.TabIndex = 7;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Frame capture";
            // 
            // captureViewer
            // 
            this.captureViewer.FlipMode = Ozeki.Media.Video.FlipMode.None;
            this.captureViewer.Location = new System.Drawing.Point(6, 19);
            this.captureViewer.Name = "captureViewer";
            this.captureViewer.RotateAngle = 0;
            this.captureViewer.Size = new System.Drawing.Size(413, 403);
            this.captureViewer.TabIndex = 0;
            this.captureViewer.Text = "captureViewer";
            // 
            // startBt
            // 
            this.startBt.Enabled = false;
            this.startBt.Location = new System.Drawing.Point(442, 9);
            this.startBt.Name = "startBt";
            this.startBt.Size = new System.Drawing.Size(75, 23);
            this.startBt.TabIndex = 8;
            this.startBt.Text = "Start";
            this.startBt.UseVisualStyleBackColor = true;
            this.startBt.Click += new System.EventHandler(this.startBt_Click);
            // 
            // stopBt
            // 
            this.stopBt.Enabled = false;
            this.stopBt.Location = new System.Drawing.Point(523, 9);
            this.stopBt.Name = "stopBt";
            this.stopBt.Size = new System.Drawing.Size(75, 23);
            this.stopBt.TabIndex = 9;
            this.stopBt.Text = "Stop";
            this.stopBt.UseVisualStyleBackColor = true;
            this.stopBt.Click += new System.EventHandler(this.stopBt_Click);
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.ClientSize = new System.Drawing.Size(879, 482);
            this.Controls.Add(this.stopBt);
            this.Controls.Add(this.startBt);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.secondText);
            this.Controls.Add(this.disconnectBt);
            this.Controls.Add(this.connectBt);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Name = "FrameCapture";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Frame capture";
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        private System.Windows.Forms.Button connectBt;
        private System.Windows.Forms.Button disconnectBt;
        private System.Windows.Forms.TextBox secondText;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.GroupBox groupBox2;
        private Ozeki.Media.Video.Controls.VideoViewerWF liveViewer;
        private Ozeki.Media.Video.Controls.VideoViewerWF captureViewer;
        private System.Windows.Forms.Button startBt;
        private System.Windows.Forms.Button stopBt;
    }
}

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

Initial Description
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!

Initial Title
How to capture frames from a camera in C#

Initial Tags
image, video, c#

Initial Language
C#