How to implement automatic movement (left and right scan between two points) for an IP camera at different speeds in C#

This example demonstrates how to implement automatic scan function into your PTZ camera application with motion control using C# language. With these features your application will be able to pan the environment automatically between two points, in left and right directions. 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.

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: https://www.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\
PTZ_Automatic_Movement_WF\PTZ_Automatic_Movement_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
PTZ_Automatic_Movement_WPF\PTZ_Automatic_Movement_WPF.sln

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

The following methods can be used to scan automatically wtih your PTZ camera.

  • _camera.CameraMovement.Patrol(patrol, duration);
  • _camera.CameraMovement.StopMovement();

These two methods provide the function to start and stop automatic movement at different patrolling durations.

_camera.CameraMovement.Patrol can start scan with these given parameters:

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

Automatic control

The following lines will show you a possible way to make your application to be able to scan automatically. With the presented method the camera can pan the area between two fix points. With the code below you can set the direction of the scan and start the automatic process that will operate the camera. Beside the described function you can also set how much time the camera should spend between the sides of the scanned area. Here is the code you can use to add these features.

Implement automatic movement in C#

Windows Form WPF  

Windows forms version

Form1.cs

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

namespace PTZ_Camera_Motion_Control02
{
    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();
            comboBox_Direction.DataSource = Enum.GetValues(typeof(PatrolDirection));
        }

        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 button_ScanStart_Click(object sender, EventArgs e)
        {
            var patrol = (PatrolDirection)comboBox_Direction.SelectedItem;
            if (!String.IsNullOrEmpty(textBox_Duration.Text))
            {
                double duration;

                if(double.TryParse(textBox_Duration.Text, out duration))
                    _camera.CameraMovement.Patrol(patrol, duration);

            }
        }

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

Code 1 - Implement automatic movement 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 gui 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 PTZ_Camera_Motion_Control02
{
    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.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();
            this.groupBox1.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 ";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.label3);
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Controls.Add(this.label1);
            this.groupBox2.Controls.Add(this.textBox_Duration);
            this.groupBox2.Controls.Add(this.button_ScanStop);
            this.groupBox2.Controls.Add(this.button_ScanStart);
            this.groupBox2.Controls.Add(this.comboBox_Direction);
            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.Controls.Add(this.groupBox2);
            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 Automatic Movements";
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.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.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 2 - GUI example in C#


WPF version

MainWindow.xaml.cs

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


namespace PTZ_Camera_Motion_Control02Wpf
{
    /// 
    /// 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();
            videoViewer.SetImageProvider(_drawingImageProvider);
            comboBox_Direction.ItemsSource = Enum.GetValues(typeof(PatrolDirection));
            comboBox_Direction.SelectedIndex = 0;
        }

        private void Connect_Click(object sender, RoutedEventArgs e)
        {
            _camera = new IPCamera("192.168.112.109:8080", "user", "qwe123");

            _connector.Connect(_camera.VideoChannel, _drawingImageProvider);
            _camera.Start();
            videoViewer.Start();
        }

        private void button_ScanStart_Click(object sender, RoutedEventArgs e)
        {
           
            var patrol = (PatrolDirection)comboBox_Direction.SelectedItem;
            if (!String.IsNullOrEmpty(textBox_Duration.Text))
            {
                double duration;
                if(double.TryParse(textBox_Duration.Text, out duration))
                    _camera.CameraMovement.Patrol(patrol, duration);
            }
        }

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

Code 1 - Implement automatic movement 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 gui 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="PTZ_Camera_Motion_Control02Wpf.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="PTZ camera automatic movements" Height="509" Width="340" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
    <Grid>
        <GroupBox Header="Live camera" HorizontalAlignment="Left" Margin="10,82,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="10,10,0,0" VerticalAlignment="Top" Height="67" Width="91">
            <Button Content="Connect" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.107,-0.364" Click="Connect_Click" Margin="0,10,0,0"/>
        </GroupBox>
        <GroupBox Header="Scan" HorizontalAlignment="Left" Margin="12,313,0,0" VerticalAlignment="Top" Height="154" Width="308">
            <Grid HorizontalAlignment="Left" Height="132" VerticalAlignment="Top" Width="296">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="2*"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>
                
                <Label Content="Direction:" HorizontalAlignment="Right"  VerticalAlignment="Center" Grid.Row="0" Grid.Column="0"/>
                <ComboBox x:Name="comboBox_Direction" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120" Grid.Row="0" Grid.Column="1"/>
                <Label Content="Duration:" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0"/>
                <TextBox x:Name="textBox_Duration" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" VerticalAlignment="Center" Width="48" Grid.Row="1" Grid.Column="1" Margin="0,22" Text="2"/>
                <Label Content="seconds" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1"/>
                <Button Content="Scan" HorizontalAlignment="Left"  VerticalAlignment="Center" Width="70" Grid.Row="0" Grid.Column="2" Click="button_ScanStart_Click"/>
                <Button Content="Stop" HorizontalAlignment="Left"  VerticalAlignment="Center" Width="70" Grid.Row="1" Grid.Column="2" Click="button_ScanStop_Click"/>

            </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 OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.
  3. Why is the camera scanning, when I didn't order it?

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

More information