Discover Onvif IP cameras on your local network in C#

In this guide you can find information on how to discover Onvif IP cameras on your local network. 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.

How to discover Onvif IP cameras on your network 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/https://ozeki-sms-gateway.com/p_595-how-to-configure-the-firewall-for-smpp.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Discover_Cameras_WF\Discover_Cameras_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Discover_Cameras_WPF\Discover_Cameras_WPF.sln

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

Functions

The additional statements and methods of this example are the following:

Whenever you click the Discover button you should clear the combobox and the list then you can call DiscoverDevices() method:

	_deviceList = new List<DiscoveredDeviceInfo>();
	InvokeGuiThread(()=>comboBox_Devices.Items.Clear());            
	IPCameraFactory.DiscoverDevices();
	

After this method discovered a device, you can handle this event in DiscoveryCompleted method by add this device to your list and to your combobox:

_deviceList.Add(e.Device);
InvokeGuiThread(() => comboBox_Devices.Items.Add(e.Device.Name));
	

Note: you have to subscribe to the DiscoveryCompleted event of IPCamera class at the beginning of your program by the following command:

IPCameraFactory.DeviceDiscovered += IPCamera_DiscoveryCompleted;
	

Finally, in this program you can write the address and port number of the selected device to the GUI labels:

var selected = comboBox_Devices.SelectedIndex;
if (selected < 0 || selected > _deviceList.Count-1) return;
label_Host.Text = _deviceList[selected].Host;
label_Port.Text = _deviceList[selected].Port.ToString();
	

Discover Onvif IP cameras on your network example in C#

Windows Form WPF  

Windows forms version

Form1.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Ozeki.Media;
using Ozeki.Camera;

namespace OnvifIPCameraManager01
{
    public partial class Form1 : Form
    {
        private List<DiscoveredDeviceInfo> _deviceList;

        public Form1()
        {
            InitializeComponent();

            IPCameraFactory.DeviceDiscovered += IPCamera_DiscoveryCompleted;
        }

        private void button_Discover_Click(object sender, EventArgs e)
        {
            _deviceList = new List<DiscoveredDeviceInfo>();

            InvokeGuiThread(() => comboBox_Devices.Items.Clear());

            IPCameraFactory.DiscoverDevices();
        }

        private void IPCamera_DiscoveryCompleted(object sender, DiscoveryEventArgs e)
        {
            _deviceList.Add(e.Device);
            InvokeGuiThread(() => comboBox_Devices.Items.Add(e.Device.Name));
        }

        private void comboBox_Devices_SelectedIndexChanged(object sender, EventArgs e)
        {
            InvokeGuiThread(() =>
            {
                var selected = comboBox_Devices.SelectedIndex;
                if (selected < 0 || selected > _deviceList.Count - 1) return;
                label_Host.Text = _deviceList[selected].Host;
                label_Port.Text = _deviceList[selected].Port.ToString();
            });
        }

        private void InvokeGuiThread(Action action)
        {
            BeginInvoke(action);
        }
    }
}
		
Code 1 - Discover Onvif IP cameras on your network 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

gui of an application for discovering onvif ip cameras on local network in C#
Figure 1 - The graphical user interface of your application

After the successful implementation of the functions and the GUI elements, the application will work properly. Pressing the connect button will load in the image of the IP camera device connected to your PC into the panel that you can see on the picture.

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 OnvifIPCameraManager01
	{
	    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.button_Discover = new System.Windows.Forms.Button();
	            this.groupBox1 = new System.Windows.Forms.GroupBox();
	            this.label_Port = new System.Windows.Forms.Label();
	            this.label_Host = new System.Windows.Forms.Label();
	            this.label2 = new System.Windows.Forms.Label();
	            this.label1 = new System.Windows.Forms.Label();
	            this.comboBox_Devices = new System.Windows.Forms.ComboBox();
	            this.groupBox1.SuspendLayout();
	            this.SuspendLayout();
	            // 
	            // button_Discover
	            // 
	            this.button_Discover.Location = new System.Drawing.Point(10, 10);
	            this.button_Discover.Name = "button_Discover";
	            this.button_Discover.Size = new System.Drawing.Size(75, 25);
	            this.button_Discover.TabIndex = 4;
	            this.button_Discover.Text = "Discover";
	            this.button_Discover.UseVisualStyleBackColor = true;
	            this.button_Discover.Click += new System.EventHandler(this.button_Discover_Click);
	            // 
	            // groupBox1
	            // 
	            this.groupBox1.Controls.Add(this.label_Port);
	            this.groupBox1.Controls.Add(this.label_Host);
	            this.groupBox1.Controls.Add(this.label2);
	            this.groupBox1.Controls.Add(this.label1);
	            this.groupBox1.Controls.Add(this.comboBox_Devices);
	            this.groupBox1.Location = new System.Drawing.Point(10, 40);
	            this.groupBox1.Name = "groupBox1";
	            this.groupBox1.Size = new System.Drawing.Size(300, 100);
	            this.groupBox1.TabIndex = 5;
	            this.groupBox1.TabStop = false;
	            this.groupBox1.Text = "Device list";
	            // 
	            // label_Port
	            // 
	            this.label_Port.AutoSize = true;
	            this.label_Port.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
	            this.label_Port.Location = new System.Drawing.Point(205, 40);
	            this.label_Port.Name = "label_Port";
	            this.label_Port.Size = new System.Drawing.Size(2, 15);
	            this.label_Port.TabIndex = 4;
	            // 
	            // label_Host
	            // 
	            this.label_Host.AutoSize = true;
	            this.label_Host.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
	            this.label_Host.Location = new System.Drawing.Point(205, 20);
	            this.label_Host.Name = "label_Host";
	            this.label_Host.Size = new System.Drawing.Size(2, 15);
	            this.label_Host.TabIndex = 3;
	            // 
	            // label2
	            // 
	            this.label2.AutoSize = true;
	            this.label2.Location = new System.Drawing.Point(160, 40);
	            this.label2.Name = "label2";
	            this.label2.Size = new System.Drawing.Size(29, 13);
	            this.label2.TabIndex = 2;
	            this.label2.Text = "Port:";
	            // 
	            // label1
	            // 
	            this.label1.AutoSize = true;
	            this.label1.Location = new System.Drawing.Point(160, 20);
	            this.label1.Name = "label1";
	            this.label1.Size = new System.Drawing.Size(48, 13);
	            this.label1.TabIndex = 1;
	            this.label1.Text = "Address:";
	            // 
	            // comboBox_Devices
	            // 
	            this.comboBox_Devices.FormattingEnabled = true;
	            this.comboBox_Devices.Location = new System.Drawing.Point(20, 20);
	            this.comboBox_Devices.Name = "comboBox_Devices";
	            this.comboBox_Devices.Size = new System.Drawing.Size(120, 21);
	            this.comboBox_Devices.TabIndex = 0;
	            this.comboBox_Devices.SelectedIndexChanged += new System.EventHandler(this.comboBox_Devices_SelectedIndexChanged);
	            // 
	            // Form1
	            // 
	            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
	            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
	            this.ClientSize = new System.Drawing.Size(324, 154);
	            this.Controls.Add(this.groupBox1);
	            this.Controls.Add(this.button_Discover);
	            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
	            this.MaximizeBox = false;
	            this.Name = "Form1";
	            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
	            this.Text = "Discover Onvif IP cameras";
	            this.groupBox1.ResumeLayout(false);
	            this.groupBox1.PerformLayout();
	            this.ResumeLayout(false);
	
	        }
	        private System.Windows.Forms.Button button_Discover;
	        private System.Windows.Forms.GroupBox groupBox1;
	        private System.Windows.Forms.ComboBox comboBox_Devices;
	        private System.Windows.Forms.Label label_Port;
	        private System.Windows.Forms.Label label_Host;
	        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.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Ozeki.Media;
using Ozeki.Camera;

namespace OnvifIPCameraManager01Wpf
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        private List<DiscoveredDeviceInfo> _deviceList;

        public MainWindow()
        {
            InitializeComponent();

            IPCameraFactory.DeviceDiscovered += IPCamera_DiscoveryCompleted;
        }

        private void button_Discover_Click(object sender, RoutedEventArgs e)
        {
            _deviceList = new List<DiscoveredDeviceInfo>();

            InvokeGuiThread(() => comboBox_Devices.Items.Clear());

            IPCameraFactory.DiscoverDevices();
        }

        private void IPCamera_DiscoveryCompleted(object sender, DiscoveryEventArgs e)
        {
            _deviceList.Add(e.Device);
            InvokeGuiThread(() => comboBox_Devices.Items.Add(e.Device.Name));
        }

        private void comboBox_Devices_SelectedIndexChanged(object sender, SelectionChangedEventArgs e)
        {
            InvokeGuiThread(() =>
            {
                var selected = comboBox_Devices.SelectedIndex;
                if (selected < 0 || selected > _deviceList.Count - 1) return;
                textBox_Host.Text = _deviceList[selected].Host;
                textBox_Port.Text = _deviceList[selected].Port.ToString();
            });
        }

        private void InvokeGuiThread(Action action)
        {
            Dispatcher.BeginInvoke(action);
        }
    }
}
		
Code 1 - Discover Onvif IP cameras on your network 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

gui of an application for discovering onvif ip cameras on local network in C#
Figure 1 - The graphical user interface of your application

After the successful implementation of the functions and the GUI elements, the application will work properly. Pressing the connect button will load in the image of the IP camera device connected to your PC into the panel that you can see on the picture.

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="OnvifIPCameraManager01Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Discover Onvif IP cameras" Height="206" Width="283" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
    <Grid>
        <GroupBox Header="Discover" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="120" Width="196">
            <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="186" Margin="0,0,-2,-2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Button Content="Discover" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Click="button_Discover_Click"/>
                <ComboBox x:Name="comboBox_Devices" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Center" Width="120" SelectionChanged="comboBox_Devices_SelectedIndexChanged"/>
                <Label Content="Address" HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Center"/>
                <Label Content="Port" HorizontalAlignment="Left" Grid.Row="3" VerticalAlignment="Center"/>
                <TextBox x:Name="textBox_Host" HorizontalAlignment="Right" Height="22" Grid.Row="2" TextWrapping="Wrap" VerticalAlignment="Center" Width="110" IsEnabled="False"/>
                <TextBox x:Name="textBox_Port" HorizontalAlignment="Right" Height="22" Grid.Row="3" TextWrapping="Wrap" VerticalAlignment="Center" Width="110" IsEnabled="False"/>
            </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 OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.

More information