Connect to a remote camera manually by URL in C#

In this guide you can find information on how to connect to an IP camera device with your Windows Forms/WPF Application written in C#. To implement this example, you must have Ozeki Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project.

How to connect to a remote camera manually by URL C#?

Windows Form WPF  

Windows forms version

To perform the connection to your camera device in C# using the Ozeki Camera SDK, you need to use the classes of the SDK whose objects have been declared at the beginning of the code (in the Form1 class). Then you need to initialize these objects in the constructor of the form (it's possbile within the declaration statements as well). The final steps are to call the method named SetVideoViewer() and to add your VideoViewerWF object to the control collection of the panel that you can see on the GUI. Important: you should study this article in order to find out how to setup 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 the 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://www.camera-sdk.com/https://ozeki-sms-gateway.com/p_595-how-to-configure-the-firewall-for-smpp.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Connect_Manually_WF\Connect_Manually_WF.sln

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

Functions

Method calls for reacting the to the "Connect button clicked" event:

If a camera was connected, first we need to disconnect the previous one before connecting the new camera. It is recommended to clear the text boxes of the previous camera:

if(_camera != null)
_camera.Disconnect();

Clear();

The following method initializes the camera device that has been declared as a private member of the class. The used arguments are the following: first, we need the URL of the device, then the necessary username and password:

_camera = new IPCamera(textBox_URL.Text, textBox_Username.Text, textBox_Password.Text);

We need to subscribe to CameraStateChanged and CameraErrorOccured event of the Camera object to get notified about camera state and error:

_camera.CameraStateChanged += _camera_CameraStateChanged;
_camera.CameraErrorOccured += _camera_CameraErrorOccured;

The next method establishes the connection between the image we get from our IP camera and the image provider object that is used to display the camera's image on the graphical user interface:

_connector.Connect(_camera.VideoChannel, _imageProvider);

In order to get image from the camera and display it on the GUI, we need to set image provider than start VideoViewerWF and Camera object:

_videoViewerWf.SetImageProvider(  _imageProvider);
_videoViewerWf.Start();
_camera.Start();

Connect to a remote camera manually by URL example in C#

Form1.cs

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

namespace OnvifIPCameraManager03
{
    public partial class Form1 : Form
    {
        private IIPCamera _camera;
        private DrawingImageProvider _imageProvider;
        private MediaConnector _connector;
        private VideoViewerWF _videoViewerWf;

        public Form1()
        {
            InitializeComponent();
            _imageProvider = new DrawingImageProvider();
            _connector = new MediaConnector();
            _videoViewerWf = new VideoViewerWF();
            SetVideoViewer();
        }

        private void SetVideoViewer()
        {
            CameraBox.Controls.Add(_videoViewerWf);
            _videoViewerWf.Size = new Size(260, 180);
            _videoViewerWf.BackColor = Color.Black;
            _videoViewerWf.TabStop = false;
            _videoViewerWf.Location = new Point(10, 20);
            _videoViewerWf.Name = "_videoViewerWf";
        }

        private void button_Connect_Click(object sender, EventArgs e)
        {
            if (_camera != null)
                _camera.Disconnect();

            Clear();

            _camera = IPCameraFactory.GetCamera(textBox_URL.Text, textBox_Username.Text, textBox_Password.Text);
            _camera.CameraStateChanged += _camera_CameraStateChanged;
            _camera.CameraErrorOccurred += _camera_CameraErrorOccured;

            _connector.Connect(_camera.VideoChannel, _imageProvider);

            _videoViewerWf.SetImageProvider(_imageProvider);
            _videoViewerWf.Start();

            _camera.Start();
        }

        private void _camera_CameraStateChanged(object sender, CameraStateEventArgs e)
        {
            InvokeGUI(() => textBox_Status.Text = e.State.ToString());
        }

        private void _camera_CameraErrorOccured(object sender, CameraErrorEventArgs e)
        {
            InvokeGUI(() => textBox_Error.Text = e.Error.ToString());
        }

        private void Clear()
        {
            InvokeGUI(() =>
            {
                textBox_Status.Text = "";
                textBox_Error.Text = "";
            });
        }

        private void InvokeGUI(Action action)
        {
            BeginInvoke(action);
        }
    }
}
		
Code 1 - Connect to a remote camera manually by URL example in C#

GUI

connect to a remote camera manually by url
Figure 1 - The graphical user interface of your application

After the successful implementation of the functions and the GUI elements, the application will work properly. Pressing the connect button will load in the image of the IP camera device connected to your PC.

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 OnvifIPCameraManager03
	{
	    partial class Form1
	    {
	        /// <summary>
	        /// Required designer variable.
	        /// </summary>
	        private System.ComponentModel.IContainer components = null;
	
	        /// <summary>
	        /// Clean up any resources being used.
	        /// </summary>
	        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	        protected override void Dispose(bool disposing)
	        {
	            if (disposing && (components != null))
	            {
	                components.Dispose();
	            }
	            base.Dispose(disposing);
	        }
	
	        #region Windows Form Designer generated code
	
	        /// <summary>
	        /// Required method for Designer support - do not modify
	        /// the contents of this method with the code editor.
	        /// </summary>
	
	        #endregion
	
	        private void InitializeComponent()
	        {
	            this.groupBox1 = new System.Windows.Forms.GroupBox();
	            this.button_Connect = new System.Windows.Forms.Button();
	            this.CameraBox = new System.Windows.Forms.GroupBox();
	            this.textBox_URL = new System.Windows.Forms.TextBox();
	            this.textBox_Username = new System.Windows.Forms.TextBox();
	            this.textBox_Password = new System.Windows.Forms.TextBox();
	            this.label1 = new System.Windows.Forms.Label();
	            this.label2 = new System.Windows.Forms.Label();
	            this.label3 = new System.Windows.Forms.Label();
	            this.label4 = new System.Windows.Forms.Label();
	            this.textBox_Status = new System.Windows.Forms.TextBox();
	            this.textBox_Error = new System.Windows.Forms.TextBox();
	            this.label5 = new System.Windows.Forms.Label();
	            this.groupBox1.SuspendLayout();
	            this.SuspendLayout();
	            // 
	            // groupBox1
	            // 
	            this.groupBox1.Controls.Add(this.label5);
	            this.groupBox1.Controls.Add(this.textBox_Error);
	            this.groupBox1.Controls.Add(this.textBox_Status);
	            this.groupBox1.Controls.Add(this.label4);
	            this.groupBox1.Controls.Add(this.label3);
	            this.groupBox1.Controls.Add(this.label2);
	            this.groupBox1.Controls.Add(this.label1);
	            this.groupBox1.Controls.Add(this.textBox_Password);
	            this.groupBox1.Controls.Add(this.textBox_Username);
	            this.groupBox1.Controls.Add(this.textBox_URL);
	            this.groupBox1.Controls.Add(this.button_Connect);
	            this.groupBox1.Location = new System.Drawing.Point(306, 12);
	            this.groupBox1.Name = "groupBox1";
	            this.groupBox1.Size = new System.Drawing.Size(264, 210);
	            this.groupBox1.TabIndex = 0;
	            this.groupBox1.TabStop = false;
	            this.groupBox1.Text = "Connect";
	            // 
	            // button_Connect
	            // 
	            this.button_Connect.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
	            this.button_Connect.ForeColor = System.Drawing.Color.Black;
	            this.button_Connect.Location = new System.Drawing.Point(94, 162);
	            this.button_Connect.Name = "button_Connect";
	            this.button_Connect.Size = new System.Drawing.Size(80, 23);
	            this.button_Connect.TabIndex = 6;
	            this.button_Connect.Text = "Connect";
	            this.button_Connect.UseVisualStyleBackColor = true;
	            this.button_Connect.Click += new System.EventHandler(this.button_Connect_Click);
	            // 
	            // CameraBox
	            // 
	            this.CameraBox.Location = new System.Drawing.Point(10, 12);
	            this.CameraBox.Name = "CameraBox";
	            this.CameraBox.Size = new System.Drawing.Size(290, 210);
	            this.CameraBox.TabIndex = 3;
	            this.CameraBox.TabStop = false;
	            this.CameraBox.Text = "Live camera ";
	            // 
	            // textBox_URL
	            // 
	            this.textBox_URL.Location = new System.Drawing.Point(94, 19);
	            this.textBox_URL.Name = "textBox_URL";
	            this.textBox_URL.Size = new System.Drawing.Size(164, 20);
	            this.textBox_URL.TabIndex = 7;
	            // 
	            // textBox_Username
	            // 
	            this.textBox_Username.Location = new System.Drawing.Point(94, 46);
	            this.textBox_Username.Name = "textBox_Username";
	            this.textBox_Username.Size = new System.Drawing.Size(164, 20);
	            this.textBox_Username.TabIndex = 8;
	            // 
	            // textBox_Password
	            // 
	            this.textBox_Password.Location = new System.Drawing.Point(94, 73);
	            this.textBox_Password.Name = "textBox_Password";
	            this.textBox_Password.Size = new System.Drawing.Size(164, 20);
	            this.textBox_Password.TabIndex = 9;
	            // 
	            // label1
	            // 
	            this.label1.AutoSize = true;
	            this.label1.Location = new System.Drawing.Point(6, 22);
	            this.label1.Name = "label1";
	            this.label1.Size = new System.Drawing.Size(71, 13);
	            this.label1.TabIndex = 10;
	            this.label1.Text = "Camera URL:";
	            // 
	            // label2
	            // 
	            this.label2.AutoSize = true;
	            this.label2.Location = new System.Drawing.Point(6, 49);
	            this.label2.Name = "label2";
	            this.label2.Size = new System.Drawing.Size(58, 13);
	            this.label2.TabIndex = 11;
	            this.label2.Text = "Username:";
	            // 
	            // label3
	            // 
	            this.label3.AutoSize = true;
	            this.label3.Location = new System.Drawing.Point(6, 76);
	            this.label3.Name = "label3";
	            this.label3.Size = new System.Drawing.Size(56, 13);
	            this.label3.TabIndex = 12;
	            this.label3.Text = "Password:";
	            // 
	            // label4
	            // 
	            this.label4.AutoSize = true;
	            this.label4.Location = new System.Drawing.Point(6, 102);
	            this.label4.Name = "label4";
	            this.label4.Size = new System.Drawing.Size(77, 13);
	            this.label4.TabIndex = 13;
	            this.label4.Text = "Camera status:";
	            // 
	            // textBox_Status
	            // 
	            this.textBox_Status.Location = new System.Drawing.Point(94, 99);
	            this.textBox_Status.Name = "textBox_Status";
	            this.textBox_Status.ReadOnly = true;
	            this.textBox_Status.Size = new System.Drawing.Size(164, 20);
	            this.textBox_Status.TabIndex = 14;
	            // 
	            // textBox_Error
	            // 
	            this.textBox_Error.Location = new System.Drawing.Point(94, 126);
	            this.textBox_Error.Name = "textBox_Error";
	            this.textBox_Error.ReadOnly = true;
	            this.textBox_Error.Size = new System.Drawing.Size(164, 20);
	            this.textBox_Error.TabIndex = 15;
	            // 
	            // label5
	            // 
	            this.label5.AutoSize = true;
	            this.label5.Location = new System.Drawing.Point(6, 129);
	            this.label5.Name = "label5";
	            this.label5.Size = new System.Drawing.Size(70, 13);
	            this.label5.TabIndex = 16;
	            this.label5.Text = "Camera error:";
	            // 
	            // Form1
	            // 
	            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
	            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
	            this.ClientSize = new System.Drawing.Size(582, 233);
	            this.Controls.Add(this.CameraBox);
	            this.Controls.Add(this.groupBox1);
	            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
	            this.MaximizeBox = false;
	            this.Name = "Form1";
	            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
	            this.Text = "Connect to a remote camera manually by URL";
	            this.groupBox1.ResumeLayout(false);
	            this.groupBox1.PerformLayout();
	            this.ResumeLayout(false);
	
	        }
	
	        private System.Windows.Forms.GroupBox groupBox1;
	        private System.Windows.Forms.Button button_Connect;
	        private System.Windows.Forms.GroupBox CameraBox;
	        private System.Windows.Forms.TextBox textBox_Status;
	        private System.Windows.Forms.Label label4;
	        private System.Windows.Forms.Label label3;
	        private System.Windows.Forms.Label label2;
	        private System.Windows.Forms.Label label1;
	        private System.Windows.Forms.TextBox textBox_Password;
	        private System.Windows.Forms.TextBox textBox_Username;
	        private System.Windows.Forms.TextBox textBox_URL;
	        private System.Windows.Forms.Label label5;
	        private System.Windows.Forms.TextBox textBox_Error;
	    }
	}
		
Code 2 - GUI example in C#

WPF version

To perform the connection to your camera device in C# using the Ozeki Camera SDK, you need to use the classes of the SDK whose objects have been declared at the beginning of the code (in the MainWindow class). Then you need to initialize these objects in the constructor of the form (it's possbile within the declaration statements as well). Important: you should study this article in order to find out how to setup your WPF 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: https://www.camera-sdk.com/https://ozeki-sms-gateway.com/p_595-how-to-configure-the-firewall-for-smpp.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Connect_Manually_WPF\Connect_Manually_WPF.sln

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

Method calls for reacting the to the "Connect button clicked" event:

If a camera was connected, first we need to disconnect the previous camera before connect a new camera. It is recommended to clear text boxes of the previous camera:

if(_camera != null)
_camera.Disconnect();

Clear();

The following method initializes the camera device that has been declared as a private member of the class. The used arguments are the following: first, we need the URL of the device, then the necessary username and password:

_camera = IPCameraFactory.GetCamera(textBox_URL.Text, textBox_Username.Text, textBox_Password.Text);

We need to subscribe to CameraStateChanged and CameraErrorOccured event of the Camera object to get notified about camera state and error:

_camera.CameraStateChanged += _camera_CameraStateChanged;
_camera.CameraErrorOccured += _camera_CameraErrorOccured;

The next method establishes the connection between the image we get from our IP camera and the image provider object that is used to display the camera's image on the graphical user interface:

_connector.Connect(_camera.VideoChannel, _imageProvider);

In order to get image from the camera and display it on the GUI, we need to set image provider than start VideoViewerWF and Camera object:

_videoViewerWf.SetImageProvider(  _imageProvider);
_videoViewerWf.Start();
_camera.Start();

Connect to a remote camera manually by URL example in C#

MainWindow.xaml.cs

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

namespace OnvifIPCameraManager03Wpf
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        private IIPCamera _camera;
        private DrawingImageProvider _drawingImageProvider;
        private MediaConnector _connector;

        public MainWindow()
        {
            InitializeComponent();

            _drawingImageProvider = new DrawingImageProvider();
            _connector = new MediaConnector();
        }

        private void Connect_Click(object sender, RoutedEventArgs e)
        {
            if (_camera != null)
                _camera.Disconnect();

            Clear();

            _camera = IPCameraFactory.GetCamera(textBox_URL.Text, textBox_Username.Text, textBox_Password.Text);

            _camera.CameraStateChanged += _camera_CameraStateChanged;
            _camera.CameraErrorOccurred += _camera_CameraErrorOccurred;
            _connector.Connect(_camera.VideoChannel, _drawingImageProvider);
            videoViewer.SetImageProvider(_drawingImageProvider);
            videoViewer.Start();
            _camera.Start();
        }

        private void _camera_CameraStateChanged(object sender, CameraStateEventArgs e)
        {
            InvokeGUI(() => textBox_State.Text = e.State.ToString());
        }

        private void _camera_CameraErrorOccurred(object sender, CameraErrorEventArgs e)
        {
            InvokeGUI(() => textBox_Error.Text = e.Error.ToString());
        }

        private void Clear()
        {
            InvokeGUI(() =>
            {
                textBox_State.Text = String.Empty;
                textBox_Error.Text = String.Empty;
            });
        }

        private void InvokeGUI(Action action)
        {
            Dispatcher.BeginInvoke(action);
        }
    }
}
		
Code 1 - Connect to a remote camera manually by URL example in C#

GUI

connect to a remote camera manually by url
Figure 1 - The graphical user interface of your application

After the successful implementation of the functions and the GUI elements, the application will work properly. Pressing the connect button will load in the image of the IP camera device connected to your PC.

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.

MainWindow.xaml

<Window x:Class="OnvifIPCameraManager03Wpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:Ozeki.Media;assembly=OzekiSDK"
    Title="Connect to a remote camera manually by URL" Height="277" Width="650" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
<Grid>
    <GroupBox Header="Live camera" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="226" Width="308">
        <Grid HorizontalAlignment="Left" Height="204" VerticalAlignment="Top" Width="296">
            <controls:VideoViewerWPF Name="videoViewer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black"/>
        </Grid>
    </GroupBox>
    <GroupBox Header="Connect" HorizontalAlignment="Left" Margin="323,10,0,0" VerticalAlignment="Top" Height="226" Width="311">
        <Grid HorizontalAlignment="Left" Height="204" VerticalAlignment="Top" Width="299">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="2*"/>
            </Grid.ColumnDefinitions>
            <Label Content="Camera URL" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="0"/>
            <Label Content="Username" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1"/>
            <Label Content="Password" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="2"/>
            <Label Content="Camera state" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="3"/>
            <Label Content="Camera error" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="4"/>
            <TextBox x:Name="textBox_URL" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="189" Margin="0,6"/>
            <TextBox x:Name="textBox_Username" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="189" Margin="0,6"/>
            <TextBox x:Name="textBox_Password" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="189" Margin="0,6"/>
            <TextBox x:Name="textBox_State" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="189" Margin="0,6" IsEnabled="False"/>
            <TextBox x:Name="textBox_Error" Grid.Column="1" Grid.Row="4" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="189" Margin="0,6" IsEnabled="False"/>
            <Button Content="Connect" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Connect_Click" Grid.Row="5" Grid.ColumnSpan="2"/>
        </Grid>
    </GroupBox>
</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 System.Drawing.dll and the OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.

More information