Ozeki Camera Recorder Stat

How to implement Frame capture from an USB camera in C#

This lecture will make you successful in capturing frames from an USB camera in C# using the Ozeki Camera SDK. To succeed, OZEKI Camera SDK has to be installed and a reference to OzekiSDK.dll has to be added to your Visual Studio project.

Captured frames from a video
Figure 1 - Captured frames from video

Important: you should study this article in order to find out how to set up your Windows Forms Application correctly.

Getting started

To get started it is recomended to Download and Install the latest version of Ozeki Camera SDK. After installation you can find the example code discussed in this page with full source code in the following location on your harddisk:

Download Ozeki Camera SDK: http://www.camera-sdk.com/p_13-download-camera-onvif.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\Frame_Capture_From_USB_Camera_WF\

To compile this example you will need Microsoft Visual Studio installed on your computer.

Corporate use of frame capture

There are many ways to use the frame capture function effectively. Frame capture is the ideal option for those who wish to examine the security of their home, office, builiding, industry, parking area or any territory where a camera is placed. It is useful if you want to create flowcharts using video records as the source of data or when you wish to conduct a laboratory experiment where you have to record the alterations on a given timeframe.

For example, if you wish to examine items (tools, food, etc.) which are placed on a belt, you can capture the needed frames from the stream of the camera and you can view the images in all details.

Frame capturing is a fantastic solution for image analysis. This function provides effective help in the case of using surveillance video systems in shops, hospitals, banks or in the food industry.

Implement frame capture from the video stream of an USB camera example in C#

namespace: Ozeki.Media

With the help of frame capture function you can determine the number of images coming from different cameras. You can provide a time interval which determines the time between the arriving of the 2 frames or you can provide an FPS (frame/sec) value which determines the amount of frames that should be sent by the camera in a second. This function can be used with USB, IP and ONVIF IP cameras as well. The provided frame rate cannot be higher than the frame rate which can be handled by your device.


  • IsRunning - It is a logical type indicator which indicates whether the frame capture function is running or not.


  • OnFrameCaptured - This is an event which is called when a new frame arrives. With the SnapShot which can be got from the parameter we can save the incoming image to our computer.


  • Start() - with the help of this method we can start the controlling of the number of frames. The frames will appear based on the time interval we determined previously. If this method is not started, the frames will appear based on the speed with which they are sent by the camera
  • Stop() - with this method we can stop the controlling of the number of frames and the image sent by the camera will be continous again.

We can determinate the time difference between the arriving of the 2 frame rates in three ways. The time interval can be changed during the operating.

  • SetInterval(int frame) - In the parameter list we can determine the function which frame coming from the camera should be forwarded by the frame capture function. For example: SetInterval(5), in this case every fifth frame will be forwarded
  • SetInterval(double frame, double sec) - In the parameter list we can determine a unique FPS (frames per second) which will be the measure of the camera transmitting. For example: 100 frames / 30 seconds
  • SetInterval(TimeSpan time) - We can determine a time interval between the 2 frames. For example: new TimeSpan(0,0,1,0) in this case the camera will forward one frame in a minute

C# code example for frame capture from an USB camera

Windows Form WPF  


using System;
using System.Windows.Forms;
using Ozeki.Media;
using Ozeki.Camera;

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()
            _liveProvider = new DrawingImageProvider();
            _frameCaptureProvider = new DrawingImageProvider();

            _capture = new FrameCapture();
            _connector = new MediaConnector();

        private void connectBt_Click(object sender, EventArgs e)
                disconnectBt.Enabled = true;
                startBt.Enabled = true;

                _webCamera = new WebCamera();

                _connector.Connect(_webCamera, _liveProvider);

                _connector.Connect(_webCamera, _capture);
                _connector.Connect(_capture, _frameCaptureProvider);




                connectBt.Enabled = false;
            catch (Exception ex)

        private void disconnectBt_Click(object sender, EventArgs e)

                _connector.Disconnect(_webCamera, _liveProvider);

                _connector.Disconnect(_webCamera, _capture);
                _connector.Disconnect(_capture, _frameCaptureProvider);



                connectBt.Enabled = true;
                disconnectBt.Enabled = false;
                startBt.Enabled = false;
                stopBt.Enabled = false;
            catch (Exception ex)

        private void startBt_Click(object sender, EventArgs e)
                var sec = int.Parse(secondText.Text);

                _capture.SetInterval(new TimeSpan(0, 0, 0, sec));
                _capture.OnFrameCaptured += _capture_OnFrameCaptured;

                startBt.Enabled = false;
                stopBt.Enabled = true;
            catch (Exception ex)

        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.OnFrameCaptured -= _capture_OnFrameCaptured;
            startBt.Enabled = true;
            stopBt.Enabled = false;

Code 1 - Frame capture from the video stream of an USB camera in C#


Below you can find the code that belongs to the interface of the previously presented application. With the help of this section your Windows Forms Application will be able to work properly.


namespace FrameCaptureFromUSBCamera
    partial class MainForm
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
            if (disposing && (components != null))

        #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.VideoViewerWF();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.captureViewer = new Ozeki.Media.VideoViewerWF();
            this.startBt = new System.Windows.Forms.Button();
            this.stopBt = new System.Windows.Forms.Button();
            // 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.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.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.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.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.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Name = "FrameCapture";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Frame capture";



        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.VideoViewerWF liveViewer;
        private Ozeki.Media.VideoViewerWF captureViewer;
        private System.Windows.Forms.Button startBt;
        private System.Windows.Forms.Button stopBt;

Code 2 - GUI example in C#

The Graphical User Interface of an application for capturing frames from a USB camera in C#
Figure 2 - The graphical user interface of your application


With the help of this guide you will able able to implement the frame capture function to control the number of images from the camera to see as many frames as you want. You can specify a time interval between two frames appearance (in seconds). This function works with USB, IP and ONVIF IP cameras as well.

Related pages


Below you can find the answers for the most frequently asked questions related to this topic:

  1. What devices are compatible with this frame capture function?

    You can use the frame capture function of the Ozeki Camera SDK with any device which has a video stream. For example: web cameras, IP cameras, .avi etc.

  2. What is the maximum resolution of the frames that I can capture with the Ozeki Camera SDK?

    You can capture frames with as high resolution as you wish.

  3. I cannot capture the frames on the basis of the provided time interval. Why?

    In this case the video source does not send the frames. Please verify whether it is connected appropiately.