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

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;
_motionDetector.Start();
	

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");
        break;

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

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;
_motionDetector.Stop();

Viewer side motion detection functionality 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 OnvifIPCameraMotionDetection03
{
    public partial class Form1 : Form
    {
        private IIPCamera _camera;
        private DrawingImageProvider _imageProvider;
        private MediaConnector _connector;
        private VideoViewerWF _videoViewerWf;
        private MotionDetector _motionDetector;

        public Form1()
        {
            InitializeComponent();
            _motionDetector = new MotionDetector();
            _imageProvider = new DrawingImageProvider();
            _connector = new MediaConnector();
            _videoViewerWf = new VideoViewerWF();
            SetVideoViewer();
        }

        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";
            CameraBox.Controls.Add(_videoViewerWf);
        }

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

            _videoViewerWf.SetImageProvider(_imageProvider);
            _videoViewerWf.Start();
            _camera.Start();
        }

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

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

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

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

        private void InvokeGuiThread(Action action)
        {
            BeginInvoke(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.

GUI

the gui of you 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 OnvifIPCameraMotionDetection03
{
    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.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();
            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, 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.Controls.Add(this.label_Motion);
            this.groupBox2.Controls.Add(this.label_Detector);
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Controls.Add(this.label1);
            this.groupBox2.Controls.Add(this.button_Stop);
            this.groupBox2.Controls.Add(this.button_Start);
            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.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 = "Onvif Camera Motion Detection";
            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.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#

WPF version

MainWindow.xaml.cs

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

namespace OnvifIPCameraMotionDetection03Wpf
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        private IIPCamera _camera;
        private DrawingImageProvider _drawingImageProvider;
        private MediaConnector _connector;
        private MotionDetector _motionDetector;

        public MainWindow()
        {
            InitializeComponent();

            _drawingImageProvider = new DrawingImageProvider();
            _connector = new MediaConnector();
            _motionDetector = new MotionDetector();
            videoViewer.SetImageProvider(_drawingImageProvider);
        }

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

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

        private void button_Start_Click(object sender, RoutedEventArgs e)
        {
            _motionDetector.HighlightMotion = HighlightMotion.Highlight;
            _motionDetector.MotionColor = MotionColor.Red;
            _motionDetector.MotionDetection += _motionDetector_MotionDetection;
            _motionDetector.Start();
            label_Detector.Content = "started";
        }

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

                case false:
                    InvokeGuiThread(() => label_Motion.Content = "Motion ended");
                    break;
            }
        }

        private void button_Stop_Click(object sender, RoutedEventArgs e)
        {
            _motionDetector.MotionDetection -= _motionDetector_MotionDetection;
            _motionDetector.Stop();
            label_Detector.Content = "stopped";
        }

        private void InvokeGuiThread(Action action)
        {
            Dispatcher.BeginInvoke(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.

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="OnvifIPCameraMotionDetection03Wpf.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="Onvif camera motion detection" Height="407" Width="336" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
    <Grid>
        <GroupBox Header="Live camera" HorizontalAlignment="Left" Margin="10,37,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>
        <Button Content="Connect" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.107,-0.364" Click="Connect_Click"/>
        <GroupBox Header="Motion detector" HorizontalAlignment="Left" Margin="14,268,0,0" VerticalAlignment="Top" Height="101" Width="304">
            <Grid HorizontalAlignment="Left" Height="79" VerticalAlignment="Top" Width="292">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                
                <Button Content="Start" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Click="button_Start_Click"/>
                <Button Content="Stop" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="button_Stop_Click"/>
                <Label Content="Motion detector:" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Center"/>
                <Label x:Name="label_Detector" Content="" HorizontalAlignment="Center" Grid.Row="1" VerticalAlignment="Center"/>
                <Label Content="Motion:" HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Center"/>
                <Label x:Name="label_Motion" Content="" HorizontalAlignment="Center" Grid.Row="2" VerticalAlignment="Center"/>
            </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 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.

More information