How to setup viewer side motion detection in C#

This example demonstrates how to create a viewer side motion detector for an Onvif camera in C#. To implement this example, you must have OZEKI Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project.

Using the SDK's motion detection features

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:
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\\Examples\Other\
WPF version: C:\Program Files\Ozeki\Ozeki SDK\\Examples\Other\

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

The following articles will present you how to start and stop a motion detector. With the motion detector of the OZEKI Camera SDK you can detect motions and handle alarms in many different ways. For example, you can take a snapshot or record a video that you are able to send as an e-mail or upload to an FTP server. Or if any motions have been detected you can make a VoIP phone call to an alarm center with the Text to Speech function.

First of all, you need to declare a MotionDetector object from the OZEKI Camera SDK:

private MotionDetector _motionDetector;

Of course, you need to initialize this object after the GUI components have been initialized:

_motionDetector = new MotionDetector();

You need to connect the VideoChannel of your camera to the MotionDetector object and the MotionDetector to the ImageProvider object. You can do all of this using the MediaConnector object in the following way:

_connector.Connect(_camera.VideoChannel, _motionDetector);
_connector.Connect(_motionDetector, _imageProvider);

Now, you can implement further features, just like HighlightMotion, MotionColor properties, subscribing to MotionDetection events in order to get notified if motion has been detected by your camera device. Finally, you have to start your motion detector object as well.

_motionDetector.HighlightMotion = HighlightMotion.Highlight;
_motionDetector.MotionColor = MotionColor.Red;
_motionDetector.MotionDetection += _motionDetector_MotionDetection;

You can handle alarms in the _motionDetector_MotionDetection event handler method. In this example, if motion has been detected (the value of e.Detection is true) or motion has ended (the value of e.Detection is false), the application is going to indicate it on one of the GUI labels:

switch (e.Detection)
    case true:
        InvokeGuiThread(() => label_Motion.Text = "Motion detected");

    case false:
        InvokeGuiThread(() => label_Motion.Text = "Motion ended");

You can stop your MotionDetector object by clicking on the Stop button that can be seen on the figure below, too. This button's click event is going to complete the unsubscribing from the motion detection events and stop the MotionDetector object:

_motionDetector.MotionDetection -= _motionDetector_MotionDetection;

Viewer side motion detection functionality in C#

Windows Form WPF  


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

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

        public Form1()
            _motionDetector = new MotionDetector();
            _imageProvider = new DrawingImageProvider();
            _connector = new MediaConnector();
            _videoViewerWf = new VideoViewerWF();

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

        private void button_Connect_Click(object sender, EventArgs e)
            _camera = new IPCamera("", "user", "qwe123");
            _connector.Connect(_camera.VideoChannel, _motionDetector);
            _connector.Connect(_motionDetector, _imageProvider);


        private void _motionDetector_MotionDetection(object sender, MotionDetectionEvent e)
            switch (e.Detection)
                case true:
                    InvokeGuiThread(() => label_Motion.Text = "Motion detected");

                case false:
                    InvokeGuiThread(() => label_Motion.Text = "Motion ended");

        private void button_Start_Click(object sender, EventArgs e)
            _motionDetector.HighlightMotion = HighlightMotion.Highlight;
            _motionDetector.MotionColor = MotionColor.Red;
            _motionDetector.MotionDetection += _motionDetector_MotionDetection;
            label_Detector.Text = "Motion detector started";

        private void button_Stop_Click(object sender, EventArgs e)
            _motionDetector.MotionDetection -= _motionDetector_MotionDetection;
            label_Detector.Text = "Motion detector stopped";

        private void InvokeGuiThread(Action action)

Code 1 - Viewer side motion detection 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.


The Graphical User Interface of an application for motion detection 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.


	namespace OnvifIPCameraMotionDetection03
    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.label_Motion = new System.Windows.Forms.Label();
            this.label_Detector = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.button_Stop = new System.Windows.Forms.Button();
            this.button_Start = new System.Windows.Forms.Button();
            // groupBox1
            this.groupBox1.Location = new System.Drawing.Point(10, 10);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(100, 70);
            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, 25);
            this.button_Connect.Name = "button_Connect";
            this.button_Connect.Size = new System.Drawing.Size(80, 23);
            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, 85);
            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(12, 301);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(290, 105);
            this.groupBox2.TabIndex = 4;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Motion detector";
            // label_Motion
            this.label_Motion.AutoSize = true;
            this.label_Motion.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label_Motion.Location = new System.Drawing.Point(110, 75);
            this.label_Motion.Name = "label_Motion";
            this.label_Motion.Size = new System.Drawing.Size(2, 15);
            this.label_Motion.TabIndex = 5;
            // label_Detector
            this.label_Detector.AutoSize = true;
            this.label_Detector.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label_Detector.Location = new System.Drawing.Point(110, 50);
            this.label_Detector.Name = "label_Detector";
            this.label_Detector.Size = new System.Drawing.Size(2, 15);
            this.label_Detector.TabIndex = 4;
            // label2
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(10, 75);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(42, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "Motion:";
            // label1
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(10, 50);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(84, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Motion detector:";
            // button_Stop
            this.button_Stop.Location = new System.Drawing.Point(100, 20);
            this.button_Stop.Name = "button_Stop";
            this.button_Stop.Size = new System.Drawing.Size(75, 23);
            this.button_Stop.TabIndex = 1;
            this.button_Stop.Text = "Stop";
            this.button_Stop.UseVisualStyleBackColor = true;
            this.button_Stop.Click += new System.EventHandler(this.button_Stop_Click);
            // button_Start
            this.button_Start.Location = new System.Drawing.Point(10, 20);
            this.button_Start.Name = "button_Start";
            this.button_Start.Size = new System.Drawing.Size(75, 23);
            this.button_Start.TabIndex = 0;
            this.button_Start.Text = "Start";
            this.button_Start.UseVisualStyleBackColor = true;
            this.button_Start.Click += new System.EventHandler(this.button_Start_Click);
            // Form1
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(309, 414);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Onvif Camera Motion Detection";

        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.Button button_Stop;
        private System.Windows.Forms.Button button_Start;
        private System.Windows.Forms.Label label_Motion;
        private System.Windows.Forms.Label label_Detector;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;

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


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. I moved my hand and it has not been detected. Why?

    The pixel intensity (sensitivity of movement) has a default value, but in the next chapter we will learn how to change it.

Ozeki Camera Recorder

If you are not a developer, but you need a great software to view or record your camera, we recommend you to use Ozeki Camera Recorder. This is a great end user product, that allows you to control your cameras from a webbrowser. It's easy and great. You can download it from the following link: Download Ozeki Camera Recorder.

Download link:
Download Ozeki Camera Recorder for Windows

Copyright © 2000- - Ozeki Ltd |
Home > Online manual > Motion detection and alarms > Viewer side motion detection
Legal | Privacy | Terms of use | 6559 | | Login