How to set/query the camera name and location in C#

This example demonstrates how to set and query your IP camera name and location in C#. 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 set/query the camera name and location using C#?

To establish the connection properly between your application and an IP camera you should apply the same code snippet 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 set up 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\
Configure_Name_Location_WF\Configure_Name_Location_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Configure_Name_Location_WPF\Configure_Name_Location_WPF.sln

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

In addition to the methods discussed in the previous tutorials the additional methods of this example are the following:

IPCamera, created by using Ozeki Camera SDK is able to get notified when an attribute setting is completed, by subscribing to the AttributeSetCompleted event of the ipcamera object:

_camera.AttributesSetCompleted += _camera_AttributesSetCompleted;

When the event occurs, we are able to refresh this attributes:

_camera.RefreshAttributes(e.UpdatedProperties);

You can set the name and location of the camera by set the DeviceInformation property with new CustomInfo values:

_camera.SetAttributes(IPCameraAttributes.DeviceInformation, new[]   
{ 
    new CustomInfo("name", textBox_Name.Text)
});

_camera.SetAttributes(IPCameraAttributes.DeviceInformation, new[]   
{ 
    new CustomInfo("location", textBox_Location.Text)
});
	

After this, you are able to query the name and location of your camera or any other CustomInfo value by filtering the CustomInfo's properties:

var customInfos = _camera.CameraInfo.CustomInfos;
            foreach (var infos in customInfos)
            {
                if (infos.Name == "name")
                    label_Name.Text = infos.Value;
            }


var customInfos = _camera.CameraInfo.CustomInfos;
            foreach (var infos in customInfos)
            {
                if (infos.Name == "location")
                    label_Location.Text = infos.Value;
            }
	

Set or query the name and location of the camera example 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 ConfigureOnvifCameraRemotely09
{
    public partial class Form1 : Form
    {
        private IIPCamera _camera;
        private DrawingImageProvider _imageProvider;
        private MediaConnector _connector;
        private VideoViewerWF _videoViewerWf;

        public Form1()
        {
            InitializeComponent();
            _connector = new MediaConnector();
            _imageProvider = new DrawingImageProvider();
            _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.FlipMode = FlipMode.None;
            _videoViewerWf.Location = new Point(30, 30);
            _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_SetName_Click(object sender, EventArgs e)
        {
            _camera.CameraInfo.SetAttributes(new[]   
            { 
                new CustomInfo("name", textBox_Name.Text)
            });
            _camera.CameraInfo.RefreshProperties();
        }

        private void button_SetLocation_Click(object sender, EventArgs e)
        {
            _camera.CameraInfo.SetAttributes(new[]   
            { 
                new CustomInfo("location", textBox_Location.Text)
            });
            _camera.CameraInfo.RefreshProperties();
        }

        private void button_GetName(object sender, EventArgs e)
        {
            var customInfos = _camera.CameraInfo.CustomInfos;
            foreach (var infos in customInfos)
            {
                if (infos.Name == "name")
                    label_Name.Text = infos.Value;
            }
        }

        private void button_GetLocation(object sender, EventArgs e)
        {
            var customInfos = _camera.CameraInfo.CustomInfos;
            foreach (var infos in customInfos)
            {
                if (infos.Name == "location")
                    label_Location.Text = infos.Value;
            }
        }
    }
}
		
Code 1 - Set or query the name and location of the camera 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

graphical user interface
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 the image of the IP camera device connected to your PC into the panel you can see on the picture. You can set the value of the textboxes responsible for storing the name and location by clicking to the "Set name" and "Set location" buttons. With the "Get name" and "Get Location" buttons, you are able to query the name and location of your camera into the labels you can see below.

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 ConfigureOnvifCameraRemotely09
{
    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.button_SetName = new System.Windows.Forms.Button();
            this.button_SetLocation = new System.Windows.Forms.Button();
            this.textBox_Location = new System.Windows.Forms.TextBox();
            this.textBox_Name = new System.Windows.Forms.TextBox();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.label_Location = new System.Windows.Forms.Label();
            this.label_Name = new System.Windows.Forms.Label();
            this.button2 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.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(110, 90);
            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(15, 35);
            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, 105);
            this.CameraBox.Name = "CameraBox";
            this.CameraBox.Size = new System.Drawing.Size(325, 230);
            this.CameraBox.TabIndex = 3;
            this.CameraBox.TabStop = false;
            this.CameraBox.Text = "Live camera ";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.button_SetName);
            this.groupBox2.Controls.Add(this.button_SetLocation);
            this.groupBox2.Controls.Add(this.textBox_Location);
            this.groupBox2.Controls.Add(this.textBox_Name);
            this.groupBox2.Location = new System.Drawing.Point(125, 10);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(210, 90);
            this.groupBox2.TabIndex = 4;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Set camera name and location";
            // 
            // button_SetName
            // 
            this.button_SetName.Location = new System.Drawing.Point(10, 20);
            this.button_SetName.Name = "button_SetName";
            this.button_SetName.Size = new System.Drawing.Size(75, 23);
            this.button_SetName.TabIndex = 4;
            this.button_SetName.Text = "Set name";
            this.button_SetName.UseVisualStyleBackColor = true;
            this.button_SetName.Click += new System.EventHandler(this.button_SetName_Click);
            // 
            // button_SetLocation
            // 
            this.button_SetLocation.Location = new System.Drawing.Point(10, 50);
            this.button_SetLocation.Name = "button_SetLocation";
            this.button_SetLocation.Size = new System.Drawing.Size(75, 23);
            this.button_SetLocation.TabIndex = 3;
            this.button_SetLocation.Text = "Set location";
            this.button_SetLocation.UseVisualStyleBackColor = true;
            this.button_SetLocation.Click += new System.EventHandler(this.button_SetLocation_Click);
            // 
            // textBox_Location
            // 
            this.textBox_Location.Location = new System.Drawing.Point(91, 52);
            this.textBox_Location.Name = "textBox_Location";
            this.textBox_Location.Size = new System.Drawing.Size(100, 20);
            this.textBox_Location.TabIndex = 2;
            // 
            // textBox_Name
            // 
            this.textBox_Name.Location = new System.Drawing.Point(91, 22);
            this.textBox_Name.Name = "textBox_Name";
            this.textBox_Name.Size = new System.Drawing.Size(100, 20);
            this.textBox_Name.TabIndex = 1;
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.label_Location);
            this.groupBox3.Controls.Add(this.label_Name);
            this.groupBox3.Controls.Add(this.button2);
            this.groupBox3.Controls.Add(this.button1);
            this.groupBox3.Location = new System.Drawing.Point(10, 345);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(320, 90);
            this.groupBox3.TabIndex = 5;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Get camera name and location";
            // 
            // label_Location
            // 
            this.label_Location.AutoSize = true;
            this.label_Location.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label_Location.Location = new System.Drawing.Point(110, 55);
            this.label_Location.Name = "label_Location";
            this.label_Location.Size = new System.Drawing.Size(2, 15);
            this.label_Location.TabIndex = 3;
            // 
            // label_Name
            // 
            this.label_Name.AutoSize = true;
            this.label_Name.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label_Name.Location = new System.Drawing.Point(110, 25);
            this.label_Name.Name = "label_Name";
            this.label_Name.Size = new System.Drawing.Size(2, 15);
            this.label_Name.TabIndex = 2;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(10, 50);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(85, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "Get Location";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button_GetLocation);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(10, 20);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(85, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "Get name";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button_GetName);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(344, 449);
            this.Controls.Add(this.groupBox3);
            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 = "Configuring Onvif Camera Name and Location";
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.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_SetLocation;
        private System.Windows.Forms.TextBox textBox_Location;
        private System.Windows.Forms.TextBox textBox_Name;
        private System.Windows.Forms.Button button_SetName;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.Label label_Location;
        private System.Windows.Forms.Label label_Name;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;
    }
}
		
Code 2 - GUI example in C#

WPF version

MainWindow.xaml.cs

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

namespace ConfigureOnvifCameraRemotely09Wpf
{
    /// 
    /// 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();
        }

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

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

        private void button_SetName_Click(object sender, RoutedEventArgs e)
        {
            _camera.CameraInfo.SetAttributes(new[]   
        { 
            new CustomInfo("name", textBox_Name.Text)
        });
            _camera.CameraInfo.RefreshProperties();
        }

        private void button_SetLocation_Click(object sender, RoutedEventArgs e)
        {
            _camera.CameraInfo.SetAttributes(new[]   
        { 
            new CustomInfo("location", textBox_Location.Text)
        });
            _camera.CameraInfo.RefreshProperties();
        }

        private void button_GetName(object sender, RoutedEventArgs e)
        {
            var customInfos = _camera.CameraInfo.CustomInfos;
            foreach (var infos in customInfos)
            {
                if (infos.Name == "name")
                    textBox_GetName.Text = infos.Value;
            }
        }

        private void button_GetLocation(object sender, RoutedEventArgs e)
        {
            var customInfos = _camera.CameraInfo.CustomInfos;
            foreach (var infos in customInfos)
            {
                if (infos.Name == "location")
                    textBox_GetLocation.Text = infos.Value;
            }
        }
    }
}
		
Code 1 - Set or query the name and location of the camera 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

graphical user interface
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. You can set the textboxes' values as the name and location by clicking "Set name" and "Set location" buttons. With the "Get name" and "Get Location" buttons, you are able to query the name and location of your camera into the labels you can see below.

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="ConfigureOnvifCameraRemotely09Wpf.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="Configuring Onvif Camera Name and Location" Height="444" Width="340" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
<Grid>
    <GroupBox Header="Live camera" HorizontalAlignment="Left" Margin="16,94,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,24,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.107,-0.364" Click="Connect_Click"/>
    <GroupBox Header="Set camera name and location" HorizontalAlignment="Left" Margin="112,12,0,0" VerticalAlignment="Top" Height="77" Width="212">
        <Grid HorizontalAlignment="Left" Height="57" VerticalAlignment="Top" Width="196">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1.5*"/>
            </Grid.ColumnDefinitions>
            <Button Content="Set name" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70" Click="button_SetName_Click"/>
            <Button Content="Set location" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70" Grid.Row="1" Click="button_SetLocation_Click"/>
            <TextBox x:Name="textBox_Name" Grid.Column="1" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="115"/>
            <TextBox x:Name="textBox_Location" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="115"/>
        </Grid>
    </GroupBox>
    <GroupBox Header="Get camera name and location" HorizontalAlignment="Left" Margin="16,325,0,0" VerticalAlignment="Top" Height="81" Width="212">
        <Grid HorizontalAlignment="Left" Height="59" VerticalAlignment="Top" Width="196">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1.5*"/>
            </Grid.ColumnDefinitions>
            <Button Content="Get name" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70" Click="button_GetName"/>
            <Button Content="Get location" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70" Grid.Row="1" Click="button_GetLocation"/>
            <TextBox x:Name="textBox_GetName" Grid.Column="1" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="115" IsEnabled="False"/>
            <TextBox x:Name="textBox_GetLocation" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="115" 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 System.Drawing.dll and the OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.

More information