How to connect to an RTSP camera and display the picture in C#

On this webpage you will find detailed information on how to connect to an RTSP camera device with your Windows Forms/WPF Application written 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 connect to an RTSP camera device using C#?

Windows Form WPF  

Windows forms version

In order to connect to your camera device with the Ozeki Camera SDK you need to use the classes of the SDK whose objects have been declared at the beginning of the Form1 class. After this you should initialize these objects in the constructor of the form ( it is possible within the declaration statements as well). The final steps are to call the method named SetVideoViewer() and to add your VideoViewerFM 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 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\
Camera_Viewer_Connect_RTSP_WF\Camera_Viewer_Connect_RTSP_WF.sln

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

The following Method calls which can react to the "Connect button pushed" event:

_camera = IPCameraFactory.GetCamera("rtsp://192.168.113.150:554/ufirststream", "root", "pass"): this method initializes the camera device that has been declared as a private member of the class. The used arguments are the following: The first component is the adress of the camera. It must be an RTSP (Real Time Streaming Protocol) camera which is marked by the, rtsp:// opening and followed by the URI (Uniform Resource Identifier) containing the IP address of the device, then its port number and stream identifier. The second component is the username and the third is the password for the camera.

_connector.Connect(_camera.VideoChannel, _imageProvider): This method establishes the connection between the image that we recive from our RTSP camera and the image provider object which is used for displaying the image of the camera on the Graphical User Interface.

_camera.Start(): you should use this method in order to receive image from the camera.

_videoViewerWF1.Start(): by using this method the image on the GUI will be displayed.

RTSP camera connection example in C#

Form1.cs

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

namespace VideoCameraViewer02
{
    public partial class Form1 : Form
    {
        private IIPCamera _camera;
        private DrawingImageProvider _imageProvider = new DrawingImageProvider();
        private MediaConnector _connector = new MediaConnector();
        private VideoViewerWF _videoViewerWF1;

        public Form1()
        {
            InitializeComponent();

            // Create video viewer UI control
            _videoViewerWF1 = new VideoViewerWF();
            _videoViewerWF1.Name = "videoViewerWF1";
            _videoViewerWF1.Size = panel1.Size;
            panel1.Controls.Add(_videoViewerWF1);

            // Bind the camera image to the UI control
            _videoViewerWF1.SetImageProvider(_imageProvider);
        }

        // Connect camera video channel to image provider and start
        private void connectBtn_Click(object sender, EventArgs e)
        {
            _camera = IPCameraFactory.GetCamera("rtsp://192.168.113.150:554/ufirststream", "root", "pass");
            _connector.Connect(_camera.VideoChannel, _imageProvider);
            _camera.Start();
            _videoViewerWF1.Start();
        }
    }
}
	

Code 1 - IP camera connection example 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 the RTSP camera viewer
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. By pressing the connect button the image of the RTSP camera will be loaded in.

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 VideoCameraViewer02
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.connectBtn = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // connectBtn
            // 
            this.connectBtn.Location = new System.Drawing.Point(10, 230);
            this.connectBtn.Name = "connectBtn";
            this.connectBtn.Size = new System.Drawing.Size(75, 23);
            this.connectBtn.TabIndex = 2;
            this.connectBtn.Text = "Connect";
            this.connectBtn.UseVisualStyleBackColor = true;
            this.connectBtn.Click += new System.EventHandler(this.connectBtn_Click);
            // 
            // panel1
            // 
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Location = new System.Drawing.Point(10, 10);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(250, 210);
            this.panel1.TabIndex = 3;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(274, 264);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.connectBtn);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "RTSP Camera\'s Live Image";
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.Button connectBtn;
        private System.Windows.Forms.Panel panel1;
    }
}
	

Code 2 - GUI example in C#


WPF version

In order to connect to your camera device with the Ozeki Camera SDK you need to use the classes of the SDK whose objects have been declared at the beginning of the MainWindow class. After this you should initialize these objects in the constructor of the window ( it is possible 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://camera-sdk.com/p_6513-download-onvif-ozeki-camera-sdk-for-c-sharp.html
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Camera_Viewer_Connect_RTSP_WPF\Camera_Viewer_Connect_RTSP_WPF.sln

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

The following Method calls which can react to the "Connect button pushed" event:

_camera = new IPCamera("rtsp://192.168.113.150:554/ufirststream", "root", "pass");: this method initializes the camera device that has been declared as a private member of the class. The used arguments are the following: The first component is the adress of the camera. It must be an RTSP (Real Time Streaming Protocol) camera which is marked by the, rtsp:// opening and followed by the URI (Uniform Resource Identifier) containing the IP address of the device, then its port number and stream identifier. The second component is the username and the third is the password for the camera.

_connector.Connect(_camera.VideoChannel, _drawingImageProvider): this method establishes the connection between the image that we recive from our RTSP camera and the drawing image provider object which is used for displaying the image of the camera on the Graphical User Interface.

_camera.Start(): you should use this method in order to receive image from the camera.

videoViewer.Start(): with this method the image on the GUI will be displayed.

RTSP camera connection example in C#

MainWindow.xaml.cs

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

namespace VideoCameraViewer02Wpf
{
    public partial class MainWindow : Window
    {
        private IIPCamera _camera;
        private DrawingImageProvider _drawingImageProvider;
        private MediaConnector _connector;

        public MainWindow()
        {
            InitializeComponent();

            _drawingImageProvider = new DrawingImageProvider();
            _connector = new MediaConnector();
            videoViewer.SetImageProvider(_drawingImageProvider);
        }

        private void Connect_Click(object sender, RoutedEventArgs e)
        {
            _camera = IPCameraFactory.GetCamera("rtsp://192.168.113.150:554/ufirststream", "root", "pass");
            _connector.Connect(_camera.VideoChannel, _drawingImageProvider);
            _camera.Start();
            videoViewer.Start();
        }
    }
}
	

Code 1 - IP camera connection example 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 the RTSP camera viewer
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. By pressing the connect button the image of the RTSP camera will be loaded in.

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.

The code to the GUI layout can be seen below:

MainWindow.xaml

	<Window x:Class="VideoCameraViewer02Wpf.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="RTSP camera's live image" Height="305" Width="336" 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>
        <Button Content="Connect" HorizontalAlignment="Left" Margin="10,241,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.107,-0.364" Click="Connect_Click"/>
    </Grid>
</Window>

	

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 and System.Drawing.dll to the references of the solution.
    • Please import the missing classes.
  3. What's the difference among USB camera, RTSP camera and ONVIF camera?

    In the case of USB camera we do not need camera url and some functions(like moving, zooming, etc.) are not available. In the case of RTSP camera and ONVIF camera there are more available functions. The difference between the RTSP camera and the ONVIF camera is while the RTSP camera can have only one stream at a time, the ONVIF camera can have multiple.

More information