Page 1 of 1

How to build auto scanning for a PTZ (Pan-Tilt-Zoom) IP camera in C#

#1 timo.walker   User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 1
  • Joined: 10-October 14

Posted 13 October 2014 - 06:17 AM


In this C# tutorial…

In this C# tutorial I will demonstrate how to implement automatic movement to improve your PTZ (Pan-Tilt-Zoom) camera using C#.NET. With this functionality your PTZ camera will be capable of automatic scanning both horizontally and vertically between two points at different speeds. To present a comprehensive guide, my tutorial contains the following 6 chapters:

1. Background information
2. Prerequisites
3. Connecting to an IP camera in C#
4. Implementing the automatic scan feature in C#
5. Implementing the GUI in C#
6. Summary

Download the full C# source code for this tutorial:
Attached File (64.72K)
Number of downloads: 1123

1. Background information

As the implementation of any Pan-Tilt-Zoom-based functionality requires a PTZ-compliant camera, this tutorial assumes that you have one or more PTZ IP camera. For the sake of completeness, let's talk briefly about the PTZ technology.

PTZ is the acronym for Pan-Tilt-Zoom. It refers to the movement options of the camera: panning horizontally, tilting vertically and zooming in to enlarge an object. An IP camera can be called ’PTZ IP camera’ if it is capable of remote directional and zoom control. (An IP camera – that is also called network camera – is a type of digital video cameras that transmits data over the Internet Protocol.) The PTZ functionality provides a great way for improving any IP surveillance system. (IP surveillance is a digitized and networked version of CCTV. Digitization provides easy-to-use user interface, better image quality, video recording functionality and the ability to compress content for improved storage.)

The following figure illustrates my solution (and the essence of the PTZ technology as well). A PTZ-capable IP camera is able to pan horizontally and tilt vertically. During this tutorial I have used this functionality to implement the automatic scanning between two points in C#. This way you will be able to monitor a wider/bigger area between these points without any human intervention. (This area can be called ’ROI’, that is Region of Interest.)

Attached Image
Figure 1: Scanning between two points automatically using a PTZ camera in C#

2. Prerequisites

For implementing automatic scan feature for your PTZ (Pan-Tilt-Zoom) IP camera in C#, your system needs to meet some hardware and software requirements as follows:

After you have obtained and installed all the necessary devices and software, before starting the programming, make sure that you have connected the PC and the camera to the same network and your camera has PTZ functionality. If everything is OK, open the Visual Studio and create a new Windows Forms Application. Add the camera SDK’s .dll file to your references and change your target framework to ’.NET Framwork 4.0’.

3. Connecting to an IP camera in C#

To implement this C# application three classes has been used: Program.cs, Form1.cs and Form1.Designer.cs. The functions of the auto-scanning camera application (so all the necessary features) have been written in the Form1.cs class. The Form1.Designer.cs class contains all elements that is needed for the GUI. (They are similar to MainForm.cs and Main.Designer.cs. ) But first, let me show the Program.cs class, that contains the Main() method that is the main entry point for the application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace PTZ_Camera_Motion_Control02
    static class Program
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
            Application.Run(new Form1());

Code 1: The Main() method in the Program.cs

If you want to monitor a specific area, then it is essentially needed to dispay the camera image in your application. To be able to display it, you need to connect your application to your camera. Let’s see what methods are needed for creating connection between them.

The following method initializes the camera. Three arguments, the IP address of your IP camera, the username and password are used:
_camera = IPCameraFactory.GetCamera("", "admin", "admin")

The method below can be used to create connection between the camera image and the image provider object (the image provider object is used to display the camera image):
_connector.Connect(_camera.VideoChannel, _imageProvider)

The following line allows your application to receive the camera image:

And the next one is responsible for displaying the camera image on the GUI:

4. Implementing the automatic scan feature in C#

If you have connected the application and the camera successfully, there is a need for some additional methods for the auto-scanning functionality.

The two methods below can be used to scan automatically by using your PTZ camera. These methods provide the function to start and stop automatic movement at different monitoring durations:
_camera.CameraMovement.Patrol(patrol, duration);

The _camera.CameraMovement.Patrol() method can start scanning by using the following specific parameters:

  • patrol: ToRight, ToLeft, ToUp, ToDown
  • duration: double value

Below the full Form1.cs class can be seen. It contains all functions that are needed to connect to a camera and display its image, and to be able to pan/tilt automatically. As the previous methods allow your PTZ camera to scan the designated area between two points automatically, now you only need to specify the direction of scanning and start the automatic movement by using the following code. This code can be also used to set manually how long should it take to do the way between the two points.

using System;
using System.Drawing;
using System.Windows.Forms;
using Ozeki.Media.MediaHandlers.IPCamera.Types;
using Ozeki.Media.MediaHandlers.Video;
using Ozeki.Media.Video.Controls;
using Ozeki.Media.MediaHandlers;
using Ozeki.Media.MediaHandlers.IPCamera;
using Ozeki.Media.IPCamera;
using Ozeki.Media.IPCamera.PTZ;

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

        public Form1()
            _imageProvider = new DrawingImageProvider();
            _connector = new MediaConnector();
            _videoViewerWf = new VideoViewerWF();
            comboBox_Direction.DataSource = Enum.GetValues(typeof(PatrolDirection));

        private void SetVideoViewer()
            _videoViewerWf.Size = new Size(260, 180);
            _videoViewerWf.BackColor = Color.Black;
            _videoViewerWf.TabStop = false;
            _videoViewerWf.Location = new Point(14, 19);
            _videoViewerWf.Name = "_videoViewerWf";

        private void button_Connect_Click(object sender, EventArgs e)
            _camera = IPCameraFactory.GetCamera("", "admin", "admin");
            _connector.Connect(_camera.VideoChannel, _imageProvider);

        private void button_ScanStart_Click(object sender, EventArgs e)
            var patrol = (PatrolDirection)comboBox_Direction.SelectedItem;
            var duration = double.Parse(textBox_Duration.Text);
            _camera.CameraMovement.Patrol(patrol, duration);

        private void button_ScanStop_Click(object sender, EventArgs e)

Code 2: The functions for implementing the auto-scanning feature in the Form1.cs

5. Implementing the GUI in C#

After implementing the core functionality, take a look at the following code that presents the Form1.Designer.cs class. This code can be used to create a simple Graphical User Interface for your auto-scanning application:

namespace PTZ_Camera_Motion_Control02
    partial class Form1
        private System.ComponentModel.IContainer components = null;

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

        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.groupBox2 = new System.Windows.Forms.GroupBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox_Duration = new System.Windows.Forms.TextBox();
            this.button_ScanStop = new System.Windows.Forms.Button();
            this.button_ScanStart = new System.Windows.Forms.Button();
            this.comboBox_Direction = new System.Windows.Forms.ComboBox();
            // groupBox1
            this.groupBox1.Location = new System.Drawing.Point(10, 10);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(100, 60);
            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(10, 20);
            this.button_Connect.Name = "button_Connect";
            this.button_Connect.Size = new System.Drawing.Size(75, 25);
            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, 90);
            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 ";
            // groupBox2
            this.groupBox2.Location = new System.Drawing.Point(10, 310);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(290, 85);
            this.groupBox2.TabIndex = 4;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Motion";
            // label3
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(116, 50);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(47, 13);
            this.label3.TabIndex = 6;
            this.label3.Text = "seconds";
            // label2
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(10, 50);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(50, 13);
            this.label2.TabIndex = 5;
            this.label2.Text = "Duration:";
            // label1
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(10, 20);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(52, 13);
            this.label1.TabIndex = 4;
            this.label1.Text = "Direction:";
            // textBox_Duration
            this.textBox_Duration.Location = new System.Drawing.Point(60, 47);
            this.textBox_Duration.Name = "textBox_Duration";
            this.textBox_Duration.Size = new System.Drawing.Size(50, 20);
            this.textBox_Duration.TabIndex = 3;
            // button_ScanStop
            this.button_ScanStop.Location = new System.Drawing.Point(200, 44);
            this.button_ScanStop.Name = "button_ScanStop";
            this.button_ScanStop.Size = new System.Drawing.Size(75, 25);
            this.button_ScanStop.TabIndex = 2;
            this.button_ScanStop.Text = "Stop";
            this.button_ScanStop.UseVisualStyleBackColor = true;
            this.button_ScanStop.Click += new System.EventHandler(this.button_ScanStop_Click);
            // button_ScanStart
            this.button_ScanStart.Location = new System.Drawing.Point(200, 14);
            this.button_ScanStart.Name = "button_ScanStart";
            this.button_ScanStart.Size = new System.Drawing.Size(75, 25);
            this.button_ScanStart.TabIndex = 1;
            this.button_ScanStart.Text = "Scan";
            this.button_ScanStart.UseVisualStyleBackColor = true;
            this.button_ScanStart.Click += new System.EventHandler(this.button_ScanStart_Click);
            // comboBox_Direction
            this.comboBox_Direction.FormattingEnabled = true;
            this.comboBox_Direction.Location = new System.Drawing.Point(60, 17);
            this.comboBox_Direction.Name = "comboBox_Direction";
            this.comboBox_Direction.Size = new System.Drawing.Size(100, 21);
            this.comboBox_Direction.TabIndex = 0;
            // Form1
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(309, 404);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "PTZ Camera Automatic Movements";

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Button button_Connect;
        private System.Windows.Forms.GroupBox CameraBox;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox_Duration;
        private System.Windows.Forms.Button button_ScanStop;
        private System.Windows.Forms.Button button_ScanStart;
        private System.Windows.Forms.ComboBox comboBox_Direction;

Code 3: The GUI elements in the Form1.Designer.cs

Using this source code my application looks like this (if you do not change anything in the code, you should see such a GUI as well):

Attached Image

Figure 2: The appearance of the GUI

If you have run your application successfully, let’s interpret the user interface. You can connect to your camera by clicking on the Connect button. As you can see below, after pressing this button, the camera image will appear.

Attached Image

Figure 3: The camera image can be seen in the C# auto-scanning application

In order to configure the auto-scanning, take a look at the Motion section. First of all select a direction (ToRight, ToLeft, ToUp, ToDown) that determines the direction of the camera movement. After this specify the duration in seconds then click on the Scan button to start scanning. The camera will move to the selected direction for the provided seconds. After this time has elapsed, the camera will start to move in the opposite direction for the same number of seconds. After this time has elapsed again, the process will start from the beginning automatically. To stop scanning click on the Stop button.

Attached Image

Figure 4: Automatic scanning is in progress by using your own C# application

As it was shown on the previous figures, due to this functionality, I can monitor the parking lot in front of my office continuously. Although my camera is unable to show the whole area in only one camera image, with this C#-based auto-scanning feature I can monitor the whole parking lot without using two or more cameras. And - what is the best - the whole process is automated.

6. Summary

In this tutorial I wanted to introduce an easy-to-use solution on how to increase the efficiency of your IP-based surveillance system. You could see above how simple to build automatic scan feature for your PTZ (Pan-Tilt-Zoom) IP camera in C# by using prewritten Onvif-based components. For further reading and getting the necessary software take a look at the following reference list that was used to prepare this tutorial:

Reference list

[1] About the PTZ technology
[2] The official website of ONVIF
[3] Download and install Visual Studio
[4] Download and install .NET Framework 4.0
[5] Download and install the Camera SDK

Is This A Good Question/Topic? 5
  • +

Replies To: How to build auto scanning for a PTZ (Pan-Tilt-Zoom) IP camera in C#

#2 ari03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 17-November 12

Posted 05 November 2014 - 12:45 PM

that is such a good tutorial , thanks , i have question if you can give me some idea ,
i do work in such place we have put ptz camera and application that is used for alert if something is steal or something is moved ,
so how does it work is it compare both pic ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1