Ozeki Camera Recorder Stat

How to implement preset position and how to move the camera to this position in C#

In this guide you can find information on how to implement preset positions and how to move the camera to these positions. With this feature your application will be able to compose a limited set of preset positions and navigate between them during the usage of your IP camera. 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.

Basic funtions and 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_Preset_Position_WF\PTZ_Preset_Position_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
PTZ_Preset_Position_WPF\PTZ_Preset_Position_WPF.sln

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

Then the first example of this chapter gave an introduction to the control features of the camera application. The 2nd example presented how to implement automatic scan operations between fixed points.

Implement preset positions and navigation between them

Preset positions as a list

This section is about the data structure of the preset positions. With this code snippet in your application you can build a collection that contains all preset positions of the camera's scannable image. The maximum size of the repository depends on the type of your camera device that you are using your application with.

Navigation between the points with the suitable GUI elements

Your applicaton must be able to handle the preset positions. On the picture below you can see the necessary GUI elements that work as the required functions. This funcionality is based on 4 important WPF Application components: a ComboBox that makes the user able to choose from the preset positions. A "Move" Button that controls the camera to the required position. Finally, with the "Add" and "Delete" buttons you can add new and remove existing positions from your collection.

  • Add a new preset:

    _camera.CameraMovement.Preset.Add();
    comboBox_Presets.DataSource = _camera.CameraMovement.GetPresets();

  • Move to choosen preset:

    var preset = (IPCameraPreset)comboBox_Presets.SelectedItem;
    _camera.CameraMovement.Preset.MoveTo(preset.Name);

  • Go to the Home preset:

    _camera.CameraMovement.GoToHome();

  • Delete a choosen preset:

    var preset = (IPCameraPreset)comboBox_Presets.SelectedItem;
    comboBox_Presets.DataSource = null;
    comboBox_Presets.Items.Remove(preset);
    _camera.CameraMovement.Preset.Remove(preset.Name);
    comboBox_Presets.DataSource = _camera.CameraMovement.GetPresets();
    comboBox_Presets.SelectedIndex = -1;

Implementation of this example 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_Control03
{
    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(MoveDirection.In);
                    break;
                case "Out":
                    _camera.CameraMovement.Zoom(MoveDirection.Out);
                    break;
            }
        }

        private void button_AddPreset_Click(object sender, EventArgs e)
        {
            if (_camera != null)
            {
                _camera.CameraMovement.Preset.Add();
                comboBox_Presets.DataSource = _camera.CameraMovement.Preset.GetPresets();
            }
        }

        private void button_PresetMove_Click(object sender, EventArgs e)
        {
            if (_camera != null)
            {
                var preset = (IPCameraPreset)comboBox_Presets.SelectedItem;
                _camera.CameraMovement.Preset.MoveTo(preset.Name);
            }
        }

        private void button_PresetDelete_Click(object sender, EventArgs e)
        {
            if (_camera != null)
            {
                var preset = (IPCameraPreset)comboBox_Presets.SelectedItem;
                comboBox_Presets.DataSource = null;
                comboBox_Presets.Items.Remove(preset);
                _camera.CameraMovement.Preset.Remove(preset.Name);
                comboBox_Presets.DataSource = _camera.CameraMovement.Preset.GetPresets();
                comboBox_Presets.SelectedIndex = -1;
            }
        }

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

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

Code 1 - Implementation of this 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 an applicatin to configure preset position for a camera in C#
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_Control03
{
    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.buttonUpLeft = new System.Windows.Forms.Button();
            this.buttonLeft = new System.Windows.Forms.Button();
            this.buttonDownLeft = new System.Windows.Forms.Button();
            this.buttonUp = new System.Windows.Forms.Button();
            this.buttonDown = new System.Windows.Forms.Button();
            this.buttonUpRight = new System.Windows.Forms.Button();
            this.buttonRight = new System.Windows.Forms.Button();
            this.buttonDownRight = new System.Windows.Forms.Button();
            this.CameraMoveBox = new System.Windows.Forms.GroupBox();
            this.button_Home = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.button_SetHome = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox_Presets = new System.Windows.Forms.ComboBox();
            this.button_AddPreset = new System.Windows.Forms.Button();
            this.button_PresetDelete = new System.Windows.Forms.Button();
            this.button_PresetMove = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.CameraMoveBox.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();

            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";
             
            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);
             
            this.CameraBox.Location = new System.Drawing.Point(10, 90);
            this.CameraBox.Name = "CameraBox";
            this.CameraBox.Size = new System.Drawing.Size(285, 220);
            this.CameraBox.TabIndex = 3;
            this.CameraBox.TabStop = false;
            this.CameraBox.Text = "Live camera ";
             
            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);
             
            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);
             
            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);
             
            this.buttonUp.Location = new System.Drawing.Point(105, 20);
            this.buttonUp.Name = "buttonUp";
            this.buttonUp.Size = new System.Drawing.Size(75, 25);
            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);
             
            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);
             
            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);
             
            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);
             
            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);
            
            this.CameraMoveBox.Controls.Add(this.button_Home);
            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(300, 190);
            this.CameraMoveBox.Name = "CameraMoveBox";
            this.CameraMoveBox.Size = new System.Drawing.Size(295, 120);
            this.CameraMoveBox.TabIndex = 4;
            this.CameraMoveBox.TabStop = false;
            this.CameraMoveBox.Text = "Control";
             
            this.button_Home.Location = new System.Drawing.Point(105, 50);
            this.button_Home.Name = "button_Home";
            this.button_Home.Size = new System.Drawing.Size(75, 25);
            this.button_Home.TabIndex = 8;
            this.button_Home.Text = "Home";
            this.button_Home.UseVisualStyleBackColor = true;
            this.button_Home.Click += new System.EventHandler(this.button_Home_Click);
             
            this.groupBox2.Controls.Add(this.button_SetHome);
            this.groupBox2.Controls.Add(this.label1);
            this.groupBox2.Controls.Add(this.comboBox_Presets);
            this.groupBox2.Controls.Add(this.button_AddPreset);
            this.groupBox2.Controls.Add(this.button_PresetDelete);
            this.groupBox2.Controls.Add(this.button_PresetMove);
            this.groupBox2.Location = new System.Drawing.Point(300, 90);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(295, 95);
            this.groupBox2.TabIndex = 5;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Presets";
             
            this.button_SetHome.Location = new System.Drawing.Point(105, 60);
            this.button_SetHome.Name = "button_SetHome";
            this.button_SetHome.Size = new System.Drawing.Size(75, 25);
            this.button_SetHome.TabIndex = 6;
            this.button_SetHome.Text = "Set Home";
            this.button_SetHome.UseVisualStyleBackColor = true;
            this.button_SetHome.Click += new System.EventHandler(this.button_SetHome_Click);
             
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(102, 26);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(42, 13);
            this.label1.TabIndex = 5;
            this.label1.Text = "Presets";
             
            this.comboBox_Presets.FormattingEnabled = true;
            this.comboBox_Presets.Location = new System.Drawing.Point(160, 23);
            this.comboBox_Presets.Name = "comboBox_Presets";
            this.comboBox_Presets.Size = new System.Drawing.Size(120, 21);
            this.comboBox_Presets.TabIndex = 4;
            
            this.button_AddPreset.Location = new System.Drawing.Point(10, 20);
            this.button_AddPreset.Name = "button_AddPreset";
            this.button_AddPreset.Size = new System.Drawing.Size(75, 25);
            this.button_AddPreset.TabIndex = 3;
            this.button_AddPreset.Text = "Add preset";
            this.button_AddPreset.UseVisualStyleBackColor = true;
            this.button_AddPreset.Click += new System.EventHandler(this.button_AddPreset_Click);
             
            this.button_PresetDelete.Location = new System.Drawing.Point(205, 60);
            this.button_PresetDelete.Name = "button_PresetDelete";
            this.button_PresetDelete.Size = new System.Drawing.Size(75, 25);
            this.button_PresetDelete.TabIndex = 2;
            this.button_PresetDelete.Text = "Delete";
            this.button_PresetDelete.UseVisualStyleBackColor = true;
            this.button_PresetDelete.Click += new System.EventHandler(this.button_PresetDelete_Click);
             
            this.button_PresetMove.Location = new System.Drawing.Point(10, 60);
            this.button_PresetMove.Name = "button_PresetMove";
            this.button_PresetMove.Size = new System.Drawing.Size(75, 25);
            this.button_PresetMove.TabIndex = 1;
            this.button_PresetMove.Text = "Move";
            this.button_PresetMove.UseVisualStyleBackColor = true;
            this.button_PresetMove.Click += new System.EventHandler(this.button_PresetMove_Click);
             
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(609, 324);
            this.Controls.Add(this.CameraMoveBox);
            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 Preset Positions";
            this.groupBox1.ResumeLayout(false);
            this.CameraMoveBox.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.Button buttonUpLeft;
        private System.Windows.Forms.Button buttonLeft;
        private System.Windows.Forms.Button buttonDownLeft;
        private System.Windows.Forms.Button buttonUp;
        private System.Windows.Forms.Button buttonDown;
        private System.Windows.Forms.Button buttonUpRight;
        private System.Windows.Forms.Button buttonRight;
        private System.Windows.Forms.Button buttonDownRight;
        private System.Windows.Forms.GroupBox CameraMoveBox;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox comboBox_Presets;
        private System.Windows.Forms.Button button_AddPreset;
        private System.Windows.Forms.Button button_PresetDelete;
        private System.Windows.Forms.Button button_PresetMove;
        private System.Windows.Forms.Button button_SetHome;
        private System.Windows.Forms.Button button_Home;
    }
}

	

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.