Ozeki Camera Recorder Stat

How to record from multiple cameras simultaneously in C#

In this guide you can find information on how to implement multiple live camera images simultaneously in C#. To implement this example, you need to have OZEKI Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project.

How to record the image of multiple IP cameras using C#?

To establish the connection properly between your application and an IP camera you should apply the same code snippet what you have used in the example (How to connect to an IP camera device using C#?). Important: you should study this article in order to find out how to setup your Windows Forms/WPF Application correctly.

Using multiple cameras simultaneously allow you to cover a larger area or multiple angles at the same time from the same application.

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\
NVR_Multiple_Cameras_WF\NVR_Multiple_Cameras_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
NVR_Multiple_Cameras_WPF\NVR_Multiple_Cameras_WPF.sln

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

All the additional code sections of this example serve the purpose to duplicate the capacity of the maximally displayable camera images.

Implement recording from multiple IP cameras simultaneously in C#

Windows Form WPF  

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ozeki.Media;
using Ozeki.Camera;


namespace Network_Video_Recorder05_WF
{
    public partial class Form1 : Form
    {
        private Recorder recorder1;
        private Recorder recorder2;

        public Form1()
        {
            InitializeComponent();

            recorder1 = new Recorder();
            recorder2 = new Recorder();
            videoViewerWF1.SetImageProvider( recorder1.ImageProvider);
            videoViewerWF2.SetImageProvider(recorder2.ImageProvider);
            string path = AppDomain.CurrentDomain.BaseDirectory;
            textbox_saveto1.Text = path;
            textbox_saveto2.Text = path;
        }

        private void btn_browse1_Click(object sender, EventArgs e)
        {
            textbox_url1.Text = recorder1.getUrl();
            btn_con1.Enabled = true;
        }

        private void btn_browse2_Click(object sender, EventArgs e)
        {
            textbox_url2.Text = recorder2.getUrl();
            btn_con2.Enabled = true;
        }

        private void btn_con1_Click(object sender, EventArgs e)
        {
            if(recorder1.Camera !=null)
                recorder1.Camera.CameraStateChanged -= _camera_CameraStateChanged1;

            recorder1.connect();
            recorder1.Camera.CameraStateChanged += _camera_CameraStateChanged1;
             btn_con1.Enabled = false;

            recorder1.Camera.Start();
            videoViewerWF1.Start();
        }

        private void btn_dcon1_Click(object sender, EventArgs e)
        {
            recorder1.Disconnect();
        }

        private void btn_con2_Click(object sender, EventArgs e)
        {
            if (recorder2.Camera != null)
                recorder2.Camera.CameraStateChanged -= _camera_CameraStateChanged1;

            recorder2.connect();
            recorder2.Camera.CameraStateChanged += _camera_CameraStateChanged1;
            btn_con2.Enabled = false;

            recorder2.Camera.Start();
            videoViewerWF2.Start();
        }

        private void btn_dcon2_Click(object sender, EventArgs e)
        {
            recorder2.Disconnect();
        }

        void _camera_CameraStateChanged1(object sender, CameraStateEventArgs e)
        {
            InvokeThread(() =>
            {
                if (e.State == CameraState.Streaming)
                    Streaming(1);

                if (e.State == CameraState.Disconnected)
                    Disconnect(1);
            });
        }

        private void Disconnect(int number)
        {
            if (number == 1)
            {
                btn_con1.Enabled = true;
                btn_dcon1.Enabled = false;
            }
            else
            {
                btn_con2.Enabled = true;
                btn_dcon2.Enabled = false;
            }
        }

        private void Streaming(int number)
        {
            if(number == 1)
                 btn_dcon1.Enabled = true;
            else
                btn_dcon2.Enabled = true;
        }

        void _camera_CameraStateChanged2(object sender, CameraStateEventArgs e)
        {
            InvokeThread(() =>
            {
                if (e.State == CameraState.Streaming)
                    Streaming(2);

                if (e.State == CameraState.Disconnected)
                    Disconnect(2);
            });
        }

        void InvokeThread(Action action)
        {
            BeginInvoke(action);
        }

        private void btn_save1_Click(object sender, EventArgs e)
        {
            textbox_saveto1.Text = recorder1.GetFolderPath();
        } 

        private void btn_start1_Click(object sender, EventArgs e)
        {
            recorder1.StartRecord();
        }

        private void btn_stop1_Click(object sender, EventArgs e)
        {
            recorder1.StopRecord();
        }

        private void btn_save2_Click(object sender, EventArgs e)
        {
            textbox_saveto2.Text = recorder2.GetFolderPath();
        }

        private void btn_start2_Click(object sender, EventArgs e)
        {
            recorder2.StartRecord();
        }

        private void btn_stop2_Click(object sender, EventArgs e)
        {
            recorder2.StopRecord();
        }
    }
}

	

Code 1 - Implement multiple cameras simultaneously in C#

Please note that none of the cancel and disconnect methods are included in the example because of the demonstrating intent and briefness of the article.

GUI

The Graphical User Interface of an application for recording from multiple cameras in C#
Figure 1 - The graphical user interface of your application

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.

Form1.Designer.cs

	namespace Network_Video_Recorder05_WF
{
    partial class Form1
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.label1 = new System.Windows.Forms.Label();
            this.btn_dcon1 = new System.Windows.Forms.Button();
            this.textbox_url1 = new System.Windows.Forms.TextBox();
            this.btn_browse1 = new System.Windows.Forms.Button();
            this.btn_con1 = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.label2 = new System.Windows.Forms.Label();
            this.btn_dcon2 = new System.Windows.Forms.Button();
            this.textbox_url2 = new System.Windows.Forms.TextBox();
            this.btn_con2 = new System.Windows.Forms.Button();
            this.btn_browse2 = new System.Windows.Forms.Button();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.videoViewerWF1 = new Ozeki.Media.VideoViewerWF();
            this.groupBox4 = new System.Windows.Forms.GroupBox();
            this.videoViewerWF2 = new Ozeki.Media.VideoViewerWF();
            this.groupBox5 = new System.Windows.Forms.GroupBox();
            this.textbox_saveto1 = new System.Windows.Forms.TextBox();
            this.btn_save1 = new System.Windows.Forms.Button();
            this.btn_stop1 = new System.Windows.Forms.Button();
            this.btn_start1 = new System.Windows.Forms.Button();
            this.groupBox6 = new System.Windows.Forms.GroupBox();
            this.textbox_saveto2 = new System.Windows.Forms.TextBox();
            this.btn_save2 = new System.Windows.Forms.Button();
            this.btn_stop2 = new System.Windows.Forms.Button();
            this.btn_start2 = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.groupBox4.SuspendLayout();
            this.groupBox5.SuspendLayout();
            this.groupBox6.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.btn_dcon1);
            this.groupBox1.Controls.Add(this.textbox_url1);
            this.groupBox1.Controls.Add(this.btn_browse1);
            this.groupBox1.Controls.Add(this.btn_con1);
            this.groupBox1.Location = new System.Drawing.Point(17, 18);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(290, 96);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Connect camera #1";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(6, 30);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(32, 13);
            this.label1.TabIndex = 5;
            this.label1.Text = "URL:";
            // 
            // btn_dcon1
            // 
            this.btn_dcon1.Enabled = false;
            this.btn_dcon1.Location = new System.Drawing.Point(131, 52);
            this.btn_dcon1.Name = "btn_dcon1";
            this.btn_dcon1.Size = new System.Drawing.Size(72, 26);
            this.btn_dcon1.TabIndex = 4;
            this.btn_dcon1.Text = "Disconnect";
            this.btn_dcon1.UseVisualStyleBackColor = true;
            this.btn_dcon1.Click += new System.EventHandler(this.btn_dcon1_Click);
            // 
            // textbox_url1
            // 
            this.textbox_url1.Location = new System.Drawing.Point(44, 26);
            this.textbox_url1.Name = "textbox_url1";
            this.textbox_url1.Size = new System.Drawing.Size(159, 20);
            this.textbox_url1.TabIndex = 3;
            // 
            // btn_browse1
            // 
            this.btn_browse1.Location = new System.Drawing.Point(210, 23);
            this.btn_browse1.Name = "btn_browse1";
            this.btn_browse1.Size = new System.Drawing.Size(72, 26);
            this.btn_browse1.TabIndex = 2;
            this.btn_browse1.Text = "Browse";
            this.btn_browse1.UseVisualStyleBackColor = true;
            this.btn_browse1.Click += new System.EventHandler(this.btn_browse1_Click);
            // 
            // btn_con1
            // 
            this.btn_con1.Enabled = false;
            this.btn_con1.Location = new System.Drawing.Point(44, 52);
            this.btn_con1.Name = "btn_con1";
            this.btn_con1.Size = new System.Drawing.Size(72, 26);
            this.btn_con1.TabIndex = 0;
            this.btn_con1.Text = "Connect";
            this.btn_con1.UseVisualStyleBackColor = true;
            this.btn_con1.Click += new System.EventHandler(this.btn_con1_Click);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Controls.Add(this.btn_dcon2);
            this.groupBox2.Controls.Add(this.textbox_url2);
            this.groupBox2.Controls.Add(this.btn_con2);
            this.groupBox2.Controls.Add(this.btn_browse2);
            this.groupBox2.Location = new System.Drawing.Point(344, 18);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(290, 96);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Connect camera #2";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(6, 30);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(32, 13);
            this.label2.TabIndex = 6;
            this.label2.Text = "URL:";
            // 
            // btn_dcon2
            // 
            this.btn_dcon2.Enabled = false;
            this.btn_dcon2.Location = new System.Drawing.Point(133, 52);
            this.btn_dcon2.Name = "btn_dcon2";
            this.btn_dcon2.Size = new System.Drawing.Size(72, 26);
            this.btn_dcon2.TabIndex = 5;
            this.btn_dcon2.Text = "Disconnect";
            this.btn_dcon2.UseVisualStyleBackColor = true;
            this.btn_dcon2.Click += new System.EventHandler(this.btn_dcon2_Click);
            // 
            // textbox_url2
            // 
            this.textbox_url2.Location = new System.Drawing.Point(42, 26);
            this.textbox_url2.Name = "textbox_url2";
            this.textbox_url2.Size = new System.Drawing.Size(163, 20);
            this.textbox_url2.TabIndex = 4;
            // 
            // btn_con2
            // 
            this.btn_con2.Enabled = false;
            this.btn_con2.Location = new System.Drawing.Point(42, 52);
            this.btn_con2.Name = "btn_con2";
            this.btn_con2.Size = new System.Drawing.Size(72, 26);
            this.btn_con2.TabIndex = 2;
            this.btn_con2.Text = "Connect";
            this.btn_con2.UseVisualStyleBackColor = true;
            this.btn_con2.Click += new System.EventHandler(this.btn_con2_Click);
            // 
            // btn_browse2
            // 
            this.btn_browse2.Location = new System.Drawing.Point(211, 23);
            this.btn_browse2.Name = "btn_browse2";
            this.btn_browse2.Size = new System.Drawing.Size(72, 26);
            this.btn_browse2.TabIndex = 3;
            this.btn_browse2.Text = "Browse";
            this.btn_browse2.UseVisualStyleBackColor = true;
            this.btn_browse2.Click += new System.EventHandler(this.btn_browse2_Click);
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.videoViewerWF1);
            this.groupBox3.Location = new System.Drawing.Point(17, 129);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(290, 272);
            this.groupBox3.TabIndex = 2;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Camera #1";
            // 
            // videoViewerWF1
            // 
            this.videoViewerWF1.BackColor = System.Drawing.Color.Black;
            this.videoViewerWF1.FlipMode = Ozeki.Media.FlipMode.None;
            this.videoViewerWF1.FrameStretch = Ozeki.Media.FrameStretch.Uniform;
            this.videoViewerWF1.FullScreenEnabled = true;
            this.videoViewerWF1.Location = new System.Drawing.Point(7, 20);
            this.videoViewerWF1.Name = "videoViewerWF1";
            this.videoViewerWF1.RotateAngle = 0;
            this.videoViewerWF1.Size = new System.Drawing.Size(275, 246);
            this.videoViewerWF1.TabIndex = 0;
            this.videoViewerWF1.Text = "videoViewerWF1";
            // 
            // groupBox4
            // 
            this.groupBox4.Controls.Add(this.videoViewerWF2);
            this.groupBox4.Location = new System.Drawing.Point(346, 129);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new System.Drawing.Size(288, 272);
            this.groupBox4.TabIndex = 3;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "Camera #2";
            // 
            // videoViewerWF2
            // 
            this.videoViewerWF2.BackColor = System.Drawing.Color.Black;
            this.videoViewerWF2.FlipMode = Ozeki.Media.FlipMode.None; 
            this.videoViewerWF2.FrameStretch = Ozeki.Media.FrameStretch.Uniform; 
            this.videoViewerWF2.FullScreenEnabled = true;
            this.videoViewerWF2.Location = new System.Drawing.Point(6, 19);
            this.videoViewerWF2.Name = "videoViewerWF2";
            this.videoViewerWF2.RotateAngle = 0;
            this.videoViewerWF2.Size = new System.Drawing.Size(275, 246);
            this.videoViewerWF2.TabIndex = 1;
            this.videoViewerWF2.Text = "videoViewerWF2";
            // 
            // groupBox5
            // 
            this.groupBox5.Controls.Add(this.textbox_saveto1);
            this.groupBox5.Controls.Add(this.btn_save1);
            this.groupBox5.Controls.Add(this.btn_stop1);
            this.groupBox5.Controls.Add(this.btn_start1);
            this.groupBox5.Location = new System.Drawing.Point(13, 417);
            this.groupBox5.Name = "groupBox5";
            this.groupBox5.Size = new System.Drawing.Size(294, 105);
            this.groupBox5.TabIndex = 4;
            this.groupBox5.TabStop = false;
            this.groupBox5.Text = "Video recording #1";
            // 
            // textbox_saveto1
            // 
            this.textbox_saveto1.Location = new System.Drawing.Point(83, 32);
            this.textbox_saveto1.Name = "textbox_saveto1";
            this.textbox_saveto1.Size = new System.Drawing.Size(203, 20);
            this.textbox_saveto1.TabIndex = 3;
            // 
            // btn_save1
            // 
            this.btn_save1.Location = new System.Drawing.Point(6, 28);
            this.btn_save1.Name = "btn_save1";
            this.btn_save1.Size = new System.Drawing.Size(66, 26);
            this.btn_save1.TabIndex = 2;
            this.btn_save1.Text = "Save to:";
            this.btn_save1.UseVisualStyleBackColor = true;
            this.btn_save1.Click += new System.EventHandler(this.btn_save1_Click);
            // 
            // btn_stop1
            // 
            this.btn_stop1.Location = new System.Drawing.Point(194, 68);
            this.btn_stop1.Name = "btn_stop1";
            this.btn_stop1.Size = new System.Drawing.Size(92, 26);
            this.btn_stop1.TabIndex = 1;
            this.btn_stop1.Text = "Stop capture";
            this.btn_stop1.UseVisualStyleBackColor = true;
            this.btn_stop1.Click += new System.EventHandler(this.btn_stop1_Click);
            // 
            // btn_start1
            // 
            this.btn_start1.Location = new System.Drawing.Point(6, 68);
            this.btn_start1.Name = "btn_start1";
            this.btn_start1.Size = new System.Drawing.Size(92, 26);
            this.btn_start1.TabIndex = 0;
            this.btn_start1.Text = "Start capture";
            this.btn_start1.UseVisualStyleBackColor = true;
            this.btn_start1.Click += new System.EventHandler(this.btn_start1_Click);
            // 
            // groupBox6
            // 
            this.groupBox6.Controls.Add(this.textbox_saveto2);
            this.groupBox6.Controls.Add(this.btn_save2);
            this.groupBox6.Controls.Add(this.btn_stop2);
            this.groupBox6.Controls.Add(this.btn_start2);
            this.groupBox6.Location = new System.Drawing.Point(344, 417);
            this.groupBox6.Name = "groupBox6";
            this.groupBox6.Size = new System.Drawing.Size(290, 105);
            this.groupBox6.TabIndex = 5;
            this.groupBox6.TabStop = false;
            this.groupBox6.Text = "Video recording #2";
            // 
            // textbox_saveto2
            // 
            this.textbox_saveto2.Location = new System.Drawing.Point(84, 34);
            this.textbox_saveto2.Name = "textbox_saveto2";
            this.textbox_saveto2.Size = new System.Drawing.Size(199, 20);
            this.textbox_saveto2.TabIndex = 4;
            // 
            // btn_save2
            // 
            this.btn_save2.Location = new System.Drawing.Point(6, 30);
            this.btn_save2.Name = "btn_save2";
            this.btn_save2.Size = new System.Drawing.Size(66, 26);
            this.btn_save2.TabIndex = 3;
            this.btn_save2.Text = "Save to:";
            this.btn_save2.UseVisualStyleBackColor = true;
            this.btn_save2.Click += new System.EventHandler(this.btn_save2_Click);
            // 
            // btn_stop2
            // 
            this.btn_stop2.Location = new System.Drawing.Point(191, 68);
            this.btn_stop2.Name = "btn_stop2";
            this.btn_stop2.Size = new System.Drawing.Size(92, 26);
            this.btn_stop2.TabIndex = 2;
            this.btn_stop2.Text = "Stop capture";
            this.btn_stop2.UseVisualStyleBackColor = true;
            this.btn_stop2.Click += new System.EventHandler(this.btn_stop2_Click);
            // 
            // btn_start2
            // 
            this.btn_start2.Location = new System.Drawing.Point(6, 68);
            this.btn_start2.Name = "btn_start2";
            this.btn_start2.Size = new System.Drawing.Size(92, 26);
            this.btn_start2.TabIndex = 1;
            this.btn_start2.Text = "Start capture";
            this.btn_start2.UseVisualStyleBackColor = true;
            this.btn_start2.Click += new System.EventHandler(this.btn_start2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(648, 538);
            this.Controls.Add(this.groupBox6);
            this.Controls.Add(this.groupBox5);
            this.Controls.Add(this.groupBox4);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "Camera recording";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox4.ResumeLayout(false);
            this.groupBox5.ResumeLayout(false);
            this.groupBox5.PerformLayout();
            this.groupBox6.ResumeLayout(false);
            this.groupBox6.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TextBox textbox_url1;
        private System.Windows.Forms.Button btn_browse1;
        private System.Windows.Forms.Button btn_con1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.TextBox textbox_url2;
        private System.Windows.Forms.Button btn_con2;
        private System.Windows.Forms.Button btn_browse2;
        private System.Windows.Forms.GroupBox groupBox3;
        private Ozeki.Media.VideoViewerWF videoViewerWF1;
        private System.Windows.Forms.GroupBox groupBox4;
        private Ozeki.Media.VideoViewerWF videoViewerWF2; 
        private System.Windows.Forms.GroupBox groupBox5;
        private System.Windows.Forms.TextBox textbox_saveto1;
        private System.Windows.Forms.Button btn_save1;
        private System.Windows.Forms.Button btn_stop1;
        private System.Windows.Forms.Button btn_start1;
        private System.Windows.Forms.GroupBox groupBox6;
        private System.Windows.Forms.TextBox textbox_saveto2;
        private System.Windows.Forms.Button btn_save2;
        private System.Windows.Forms.Button btn_stop2;
        private System.Windows.Forms.Button btn_start2;
        private System.Windows.Forms.Button btn_dcon1;
        private System.Windows.Forms.Button btn_dcon2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
    }
}
	

Code 2 - GUI example in C#

DISCLAIMER: Please note that the following features will only work if your IP camera supports the given function. You should check the user manual of your IP camera to make sure it supports the feature that you wish to implement in C#.

Related Pages

FAQ

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

  1. How can I get the URL of the camera?

    You can get the URL from the producer of the camera. (In the 10th tutorial you can find information on how to create an own IP camera discoverer program.)

  2. I have not managed to build the solution. How to solve it?

    • Please set the Target framework property of the project to .NET 4.0.
    • You should add the OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.
  3. At recording I got an error. Why?

    Make sure the you have enough place recording.