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: https://camera-sdk.com/p_6513-download-onvif-ozeki-camera-sdk-for-c-sharp.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  

Windows forms version

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 your application
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#

WPF version

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.ComponentModel;
using Ozeki.Camera;

namespace Network_Video_Recorder05_WPF
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        private Recorder recorder1;
        private Recorder recorder2;

        public MainWindow()
        {
            InitializeComponent();

            recorder1 = new Recorder();
            recorder2 = new Recorder();

            VideoViewer1.SetImageProvider(recorder1.ImageProvider);
            VideoViewer2.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.IsEnabled = true;
        }

        private void btn_browse2_Click(object sender, EventArgs e)
        {
            Textbox_url2.Text = recorder2.getUrl();
            btn_con2.IsEnabled = 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.IsEnabled = false;

            recorder1.Camera.Start();
            VideoViewer1.Start();
            btn_start1.IsEnabled = true;
            btn_stop1.IsEnabled = true;
        }

        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.IsEnabled = false;

            recorder2.Camera.Start();
            VideoViewer2.Start();
            btn_start2.IsEnabled = true;
            btn_stop2.IsEnabled = true;
        }

        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.IsEnabled = true;
                btn_dcon1.IsEnabled = false;
            }
            else
            {
                btn_con2.IsEnabled = true;
                btn_dcon2.IsEnabled = false;
            }
        }

        private void Streaming(int number)
        {
            if (number == 1)
                btn_dcon1.IsEnabled = true;
            else
                btn_dcon2.IsEnabled = 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)
        {
            Dispatcher.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 your application
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 WPF Application will be able to work properly.

MainWindow.xaml

	<Window x:Class="Network_Video_Recorder05_WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:controls1="clr-namespace:Ozeki.Media;assembly=OzekiSDK"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Camera recorder" Height="546" Width="700"
        ResizeMode="NoResize"
        Background="#FFE5E5E5">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="30" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" Margin="10,10,0,10">
            <Grid.RowDefinitions>
                <RowDefinition Height="0.9*"/>
                <RowDefinition Height="3*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <GroupBox Header="Connect camera #1">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="2*"/>
                            <RowDefinition Height="2*"/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0" Margin="0,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="4*"/>
                                <ColumnDefinition Width="2*"/>
                            </Grid.ColumnDefinitions>
                            <Label Grid.Column="0" Content="URL:" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            <Button Grid.Column="2" Content="Browse" Name="btn_browse1" Height="26" Width="72" HorizontalAlignment="Right" Click="btn_browse1_Click"/>
                            <TextBox Grid.Column="1" Name="Textbox_url1" Height="20" Width="170" />
                        </Grid>  
                        <Grid Grid.Row="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="4*"/>
                                <ColumnDefinition Width="2*"/>
                            </Grid.ColumnDefinitions>
                            <Button Grid.Column="1" Content="Connect" Name="btn_con1" Height="26" Width="72" HorizontalAlignment="Left" Click="btn_con1_Click" IsEnabled="False"/>
                            <Button Grid.Column="1" Content="Disconnect" Name="btn_dcon1" Height="26" Width="72" HorizontalAlignment="Right" Click="btn_dcon1_Click" IsEnabled="False"/>
                        </Grid>
                    </Grid>
                </GroupBox>
            </Grid>
            <GroupBox Header="Camera #1" Grid.Row="1" Margin="0,15,0,15">
                <controls1:VideoViewerWPF Name="VideoViewer1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black" />
            </GroupBox>
            <Grid Grid.Row="2">
                <GroupBox Header="Connect camera #1">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="2*"/>
                            <RowDefinition Height="0.8*"/>
                            <RowDefinition Height="2*"/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="2" Margin="0,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Button Grid.Column="0" Content="Start capture" Name="btn_start1" Height="26" Width="92" Click="btn_start1_Click" IsEnabled="False"/>
                            <Button Grid.Column="2" Content="Stop capture" Name="btn_stop1" Height="26" Width="92" Click="btn_stop1_Click" IsEnabled="False"/>
                        </Grid>
                        <Grid Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="2.5*"/>
                            </Grid.ColumnDefinitions>
                            <Button Grid.Column="0" Content="Save to:" Name="btn_saveto1"  Height="26" Width="72" HorizontalAlignment="Left" Margin="5,0,0,0" Click="btn_save1_Click"/>
                            <TextBox Grid.Column="1" Height="20" Width="200" Name="Textbox_saveto1"/>
                        </Grid>
                    </Grid>
                </GroupBox>
            </Grid>
        </Grid>
        <Grid Grid.Column="2" Margin="10,10,10,10">
            <Grid.RowDefinitions>
                <RowDefinition Height="0.9*"/>
                <RowDefinition Height="3*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <GroupBox Header="Connect camera #2">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="2*"/>
                            <RowDefinition Height="2*"/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0" Margin="0,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="4*"/>
                                <ColumnDefinition Width="2*"/>
                            </Grid.ColumnDefinitions>
                            <Label Grid.Column="0" Content="URL:" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            <Button Grid.Column="2" Content="Browse" Name="btn_browse2" Height="26" Width="72" HorizontalAlignment="Right" Click="btn_browse2_Click"/>
                            <TextBox Grid.Column="1" Name="Textbox_url2" Height="20" Width="170" />
                        </Grid>
                        <Grid Grid.Row="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="4*"/>
                                <ColumnDefinition Width="2*"/>
                            </Grid.ColumnDefinitions>
                            <Button Grid.Column="1" Content="Connect" Name="btn_con2" Height="26" Width="72" HorizontalAlignment="Left" Click="btn_con2_Click" IsEnabled="False"/>
                            <Button Grid.Column="1" Content="Disconnect" Name="btn_dcon2" Height="26" Width="72" HorizontalAlignment="Right" Click="btn_dcon2_Click" IsEnabled="False"/>
                        </Grid>
                    </Grid>
                </GroupBox>
            </Grid>
            <Grid Grid.Row="1" Margin="0,15,0,15">
                <GroupBox Header="Camera #2">
                    <controls1:VideoViewerWPF Name="VideoViewer2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black" />
                </GroupBox>
            </Grid>
            <Grid Grid.Row="2">
                <GroupBox Header="Connect camera #2">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="2*"/>
                            <RowDefinition Height="0.8*"/>
                            <RowDefinition Height="2*"/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="2" Margin="0,0,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Button Grid.Column="0" Content="Start capture" Name="btn_start2" Height="26" Width="92" Click="btn_start2_Click" IsEnabled="False"/>
                            <Button Grid.Column="2" Content="Stop capture" Name="btn_stop2" Height="26" Width="92" Click="btn_stop2_Click" IsEnabled="False"/>
                        </Grid>
                        <Grid Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="2.5*"/>
                            </Grid.ColumnDefinitions>
                            <Button Grid.Column="0" Content="Save to:" Name="btn_saveto2"  Height="26" Width="72" HorizontalAlignment="Left" Margin="5,0,0,0" Click="btn_save2_Click"/>
                            <TextBox Grid.Column="1" Height="20" Width="200" Name="Textbox_saveto2"/>
                        </Grid>
                    </Grid>
                </GroupBox>
            </Grid>
        </Grid>
    </Grid>
</Window>
	
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.

More information