Ozeki Camera Recorder Stat

How to implement Pan-Tilt-Zoom (PTZ) control in C#

This example demonstrates how to implement the PTZ control methods of an IP camera 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.

What is a PTZ camera?

An IP camera with pan-tilt-zoom (PTZ) functionalities is capable of directional and zoom controlling remotely. These systems can be remotely controlled by automated systems. The PTZ holders/controls are generally sold separately from the cameras.

PTZ is an abbreviation for pan, tilt and zoom and reflects the movement options of the camera. Other types of cameras are ePTZ or virtual pan-tilt-zoom (VPTZ) where there is a high-resolution camera digitally zooming and panning into portions of the image, without any physical camera movement. Ultra-low bandwidth surveillance streaming technologies, such as TVI from Digital Barriers, use VPTZ to stream user-defined areas in higher quality without increasing the overall bandwidth usage. Surveillance cameras of this type are often connected to a digital video recorder which records the full field of view in full quality providing material for further analysis in the future.

Using the SDK's motion control features

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.

Getting started

To get started it is recomended to Download and Install the latest version of Ozeki Camera SDK. After installation you can find the example code discussed in this page with full source code in the following location on your harddisk:

Download Ozeki Camera SDK: http://www.camera-sdk.com/p_13-download-camera-onvif.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
PTZ_Control_WF\PTZ_Control_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
PTZ_Control_WPF\PTZ_Control_WPF.sln

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

To find more about the previously used functions included here as well, please, visit this tutorial

The following sections will present you how to control your PTZ camera. First, we will show the method to physically move the camera. After this, the next session will introduce the zoom functions of the application.

Move your device

private void MouseDownMove(object sender, MouseEventArgs e),
private void MouseUpMove(object sender, MouseEventArgs e),
private void Move(string direction):

  • These three methods provide the additional functionality of this example. MouseDownMove and MouseUpMove are listening mouse key events and call the Move method which will move and zoom PTZ camera.

    _camera.CameraMovement.ContinuousMove(MoveDirection.LeftUp): this method moves the camera in the required direction. This motion will continue till the camera can move. It has one parameter:

  • MoveDirection: you can set the direction of movement or zooming by selection the enumariton's correct member. It can be one of these:
    • MoveDirection.LeftUp
    • MoveDirection.Up
    • MoveDirection.RightUp
    • MoveDirection.Right
    • MoveDirection.RightDown
    • MoveDirection.Down
    • MoveDirection.LeftDown
    • MoveDirection.Left

Implement camera pan-tilt-zoom control functionality in C#

Windows Form WPF  

Form1.cs

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


namespace PTZ_Camera_Motion_Control01
{
    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(14, 19);
            _videoViewerWf.Name = "_videoViewerWf";
        }

        private void button_Connect_Click(object sender, EventArgs e)
        {
            _camera=new IPCamera("192.168.112.109:8080","user","qwe123");
            _connector.Connect(_camera.VideoChannel, _imageProvider);
            _videoViewerWf.SetImageProvider(_imageProvider);
            _videoViewerWf.Start();
            _camera.Start();
        }

        private void MouseDownMove(object sender, MouseEventArgs e)
        {
            var button = sender as Button;
            if (button != null) Move(button.Text);
        }

        private void MouseUpMove(object sender, MouseEventArgs e)
        {
            if(_camera!=null)
            _camera.CameraMovement.StopMovement();
        }

        private void Move(string direction)
        {
            if (_camera == null) return;
            switch (direction)
            {
                case "Up Left":
                    _camera.CameraMovement.ContinuousMove(MoveDirection.LeftUp);
                    break;
                case "Up":
                    _camera.CameraMovement.ContinuousMove(MoveDirection.Up);
                    break;
                case "Up Right":
                    _camera.CameraMovement.ContinuousMove(MoveDirection.RightUp);
                    break;
                case "Left":
                    _camera.CameraMovement.ContinuousMove(MoveDirection.Left);
                    break;
                case "Right":
                    _camera.CameraMovement.ContinuousMove(MoveDirection.Right);
                    break;
                case "Down Left":
                    _camera.CameraMovement.ContinuousMove(MoveDirection.LeftDown);
                    break;
                case "Down":
                    _camera.CameraMovement.ContinuousMove(MoveDirection.Down);
                    break;
                case "Down Right":
                    _camera.CameraMovement.ContinuousMove(MoveDirection.RightDown);
                    break;
                case "In":
                    _camera.CameraMovement.Zoom(CameraMovement.In);
                    break;
                case "Out":
                    _camera.CameraMovement.Zoom(CameraMovement.Out);
                    break;
            }
        }
    }
}
	

Code 1 - Implement camera pan-tilt-zoom control functionality in C#

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

GUI

The Graphical User Interface of an application with motion control features
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/WPF Application will be able to work properly.

Form1.Designer.cs

	namespace PTZ_Camera_Motion_Control01
{
    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.groupBox1 = new System.Windows.Forms.GroupBox();
            this.button_Connect = new System.Windows.Forms.Button();
            this.CameraBox = new System.Windows.Forms.GroupBox();
            this.CameraMoveBox = new System.Windows.Forms.GroupBox();
            this.buttonDownRight = new System.Windows.Forms.Button();
            this.buttonRight = new System.Windows.Forms.Button();
            this.buttonUpRight = new System.Windows.Forms.Button();
            this.buttonDown = new System.Windows.Forms.Button();
            this.buttonUp = new System.Windows.Forms.Button();
            this.buttonDownLeft = new System.Windows.Forms.Button();
            this.buttonLeft = new System.Windows.Forms.Button();
            this.buttonUpLeft = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.button_ZoomOut = new System.Windows.Forms.Button();
            this.button_ZoomIn = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.CameraMoveBox.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.button_Connect);
            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 ";
            // 
            // CameraMoveBox
            // 
            this.CameraMoveBox.Controls.Add(this.buttonDownRight);
            this.CameraMoveBox.Controls.Add(this.buttonRight);
            this.CameraMoveBox.Controls.Add(this.buttonUpRight);
            this.CameraMoveBox.Controls.Add(this.buttonDown);
            this.CameraMoveBox.Controls.Add(this.buttonUp);
            this.CameraMoveBox.Controls.Add(this.buttonDownLeft);
            this.CameraMoveBox.Controls.Add(this.buttonLeft);
            this.CameraMoveBox.Controls.Add(this.buttonUpLeft);
            this.CameraMoveBox.Location = new System.Drawing.Point(10, 300);
            this.CameraMoveBox.Name = "CameraMoveBox";
            this.CameraMoveBox.Size = new System.Drawing.Size(290, 115);
            this.CameraMoveBox.TabIndex = 4;
            this.CameraMoveBox.TabStop = false;
            this.CameraMoveBox.Text = "Control";
            // 
            // buttonDownRight
            // 
            this.buttonDownRight.Location = new System.Drawing.Point(205, 80);
            this.buttonDownRight.Name = "buttonDownRight";
            this.buttonDownRight.Size = new System.Drawing.Size(75, 25);
            this.buttonDownRight.TabIndex = 7;
            this.buttonDownRight.Text = "Down Right";
            this.buttonDownRight.UseVisualStyleBackColor = true;
            this.buttonDownRight.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDownMove);
            this.buttonDownRight.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUpMove);
            // 
            // buttonRight
            // 
            this.buttonRight.Location = new System.Drawing.Point(205, 50);
            this.buttonRight.Name = "buttonRight";
            this.buttonRight.Size = new System.Drawing.Size(75, 25);
            this.buttonRight.TabIndex = 6;
            this.buttonRight.Text = "Right";
            this.buttonRight.UseVisualStyleBackColor = true;
            this.buttonRight.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDownMove);
            this.buttonRight.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUpMove);
            // 
            // buttonUpRight
            // 
            this.buttonUpRight.Location = new System.Drawing.Point(205, 20);
            this.buttonUpRight.Name = "buttonUpRight";
            this.buttonUpRight.Size = new System.Drawing.Size(75, 25);
            this.buttonUpRight.TabIndex = 5;
            this.buttonUpRight.Text = "Up Right";
            this.buttonUpRight.UseVisualStyleBackColor = true;
            this.buttonUpRight.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDownMove);
            this.buttonUpRight.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUpMove);
            // 
            // buttonDown
            // 
            this.buttonDown.Location = new System.Drawing.Point(105, 80);
            this.buttonDown.Name = "buttonDown";
            this.buttonDown.Size = new System.Drawing.Size(75, 25);
            this.buttonDown.TabIndex = 4;
            this.buttonDown.Text = "Down";
            this.buttonDown.UseVisualStyleBackColor = true;
            this.buttonDown.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDownMove);
            this.buttonDown.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUpMove);
            // 
            // buttonUp
            // 
            this.buttonUp.Location = new System.Drawing.Point(105, 20);
            this.buttonUp.Name = "buttonUp";
            this.buttonUp.Size = new System.Drawing.Size(75, 23);
            this.buttonUp.TabIndex = 3;
            this.buttonUp.Text = "Up";
            this.buttonUp.UseVisualStyleBackColor = true;
            this.buttonUp.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDownMove);
            this.buttonUp.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUpMove);
            // 
            // buttonDownLeft
            // 
            this.buttonDownLeft.Location = new System.Drawing.Point(10, 80);
            this.buttonDownLeft.Name = "buttonDownLeft";
            this.buttonDownLeft.Size = new System.Drawing.Size(75, 25);
            this.buttonDownLeft.TabIndex = 2;
            this.buttonDownLeft.Text = "Down Left";
            this.buttonDownLeft.UseVisualStyleBackColor = true;
            this.buttonDownLeft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDownMove);
            this.buttonDownLeft.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUpMove);
            // 
            // buttonLeft
            // 
            this.buttonLeft.Location = new System.Drawing.Point(10, 50);
            this.buttonLeft.Name = "buttonLeft";
            this.buttonLeft.Size = new System.Drawing.Size(75, 25);
            this.buttonLeft.TabIndex = 1;
            this.buttonLeft.Text = "Left";
            this.buttonLeft.UseVisualStyleBackColor = true;
            this.buttonLeft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDownMove);
            this.buttonLeft.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUpMove);
            // 
            // buttonUpLeft
            // 
            this.buttonUpLeft.Location = new System.Drawing.Point(10, 20);
            this.buttonUpLeft.Name = "buttonUpLeft";
            this.buttonUpLeft.Size = new System.Drawing.Size(75, 25);
            this.buttonUpLeft.TabIndex = 0;
            this.buttonUpLeft.Text = "Up Left";
            this.buttonUpLeft.UseVisualStyleBackColor = true;
            this.buttonUpLeft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDownMove);
            this.buttonUpLeft.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUpMove);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.button_ZoomOut);
            this.groupBox2.Controls.Add(this.button_ZoomIn);
            this.groupBox2.Location = new System.Drawing.Point(12, 420);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(290, 60);
            this.groupBox2.TabIndex = 5;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Zoom";
            // 
            // button_ZoomOut
            // 
            this.button_ZoomOut.Location = new System.Drawing.Point(205, 20);
            this.button_ZoomOut.Name = "button_ZoomOut";
            this.button_ZoomOut.Size = new System.Drawing.Size(75, 25);
            this.button_ZoomOut.TabIndex = 1;
            this.button_ZoomOut.Text = "Out";
            this.button_ZoomOut.UseVisualStyleBackColor = true;
            // 
            // button_ZoomIn
            // 
            this.button_ZoomIn.Location = new System.Drawing.Point(10, 20);
            this.button_ZoomIn.Name = "button_ZoomIn";
            this.button_ZoomIn.Size = new System.Drawing.Size(75, 25);
            this.button_ZoomIn.TabIndex = 0;
            this.button_ZoomIn.Text = "In";
            this.button_ZoomIn.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(309, 489);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.CameraMoveBox);
            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 = "PTZ Camera Motion Control";
            this.groupBox1.ResumeLayout(false);
            this.CameraMoveBox.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            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.GroupBox CameraMoveBox;
        private System.Windows.Forms.Button buttonDownRight;
        private System.Windows.Forms.Button buttonRight;
        private System.Windows.Forms.Button buttonUpRight;
        private System.Windows.Forms.Button buttonDown;
        private System.Windows.Forms.Button buttonUp;
        private System.Windows.Forms.Button buttonDownLeft;
        private System.Windows.Forms.Button buttonLeft;
        private System.Windows.Forms.Button buttonUpLeft;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Button button_ZoomOut;
        private System.Windows.Forms.Button button_ZoomIn;
    }
}
	

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.
  3. Why can not I zoom?

    Some cameras does not support it.

  4. Why is the camera moving, when I am not using it?

    It is possible that someone else is also using the camera.

  5. Why cannot I turn the camera in bigger angle than the current?

    Because all camera have a limit in rotation.