How to set the camera time in C#

This example demonstrates a simple method for how you can set the camera time on your IP camera 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.

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: 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_Camera_Time_WF\Configure_Camera_Time_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Configure_Camera_Time_WPF\Configure_Camera_Time_WPF.sln

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

How to set the camera time using C#?

The additional methods of this example are the following:

When you click on the 'Set date and time' button the camera will set the date and time using a CameraDateTimeSetter object. The name of it has been configured in the button's click event. In the constructor of the CameraDateTimeSetter object three attributes will be configured:

TimeZoneInfo = (TimeZoneInfo)comboBox_Zone.SelectedItem,
UTCDate = new IPCameraDate(datetime.Year, datetime.Month, datetime.Day),
UTCTime = new IPCameraTime(datetime.Hour, datetime.Minute, datetime.Second).

Then you should call the _camera.SetAttributes(IPCameraAttributes.DateAndTime, config) method which is going to setup the camera DateAndTime attributes with the right configuration.

Setting Onvif camera time 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 ConfigureOnvifCameraRemotely02
{
    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();
            comboBox_Zone.DataSource = TimeZoneInfo.GetSystemTimeZones();
        }

        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_SetDateTime_Click(object sender, EventArgs e)
        {
            CameraDateTimeSetter config;

            if (radioButton_UTC.Checked)
            {
                var datetime = DateTime.UtcNow;
                config = new CameraDateTimeSetter(true)
                {
                    TimeZoneInfo = (TimeZoneInfo)comboBox_Zone.SelectedItem,
                    UTCDate = new IPCameraDate(datetime.Year, datetime.Month, datetime.Day),
                    UTCTime = new IPCameraTime(datetime.Hour, datetime.Minute, datetime.Second)
                };
            }
            else
            {
                var date = dateTimePicker_Date.Value;
                var time = dateTimePicker_Time.Value;
                config = new CameraDateTimeSetter(true)
                {
                    TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("UTC"), // UTC + 0
                    UTCDate = new IPCameraDate(date.Year, date.Month, date.Day),
                    UTCTime = new IPCameraTime(time.Hour, time.Minute, time.Second)
                };
            }
            _camera.DateAndTime.SetAttributes(config);
            _camera.DateAndTime.RefreshProperties();
        }
    }
}
		
Code 1 - Setting Onvif camera 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

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. Besides this you can click the 'Set date and time' button which will set the date and time on the camera.

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

using System.Windows.Forms;

namespace ConfigureOnvifCameraRemotely02
{
    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.dateTimePicker_Time = new System.Windows.Forms.DateTimePicker();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.comboBox_Zone = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.radioButton_Custom = new System.Windows.Forms.RadioButton();
            this.radioButton_UTC = new System.Windows.Forms.RadioButton();
            this.dateTimePicker_Date = new System.Windows.Forms.DateTimePicker();
            this.button_SetDateTime = 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, 60);
            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, 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);
            // 
            // CameraBox
            // 
            this.CameraBox.Location = new System.Drawing.Point(10, 80);
            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 ";
            // 
            // dateTimePicker_Time
            // 
            this.dateTimePicker_Time.Format = System.Windows.Forms.DateTimePickerFormat.Time;
            this.dateTimePicker_Time.Location = new System.Drawing.Point(10, 115);
            this.dateTimePicker_Time.MaxDate = new System.DateTime(2100, 12, 31, 0, 0, 0, 0);
            this.dateTimePicker_Time.Name = "dateTimePicker_Time";
            this.dateTimePicker_Time.ShowUpDown = true;
            this.dateTimePicker_Time.Size = new System.Drawing.Size(310, 20);
            this.dateTimePicker_Time.TabIndex = 0;
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.comboBox_Zone);
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Controls.Add(this.radioButton_Custom);
            this.groupBox2.Controls.Add(this.radioButton_UTC);
            this.groupBox2.Controls.Add(this.dateTimePicker_Date);
            this.groupBox2.Controls.Add(this.dateTimePicker_Time);
            this.groupBox2.Controls.Add(this.button_SetDateTime);
            this.groupBox2.Location = new System.Drawing.Point(10, 325);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(325, 175);
            this.groupBox2.TabIndex = 4;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Set camera time";
            // 
            // comboBox_Zone
            // 
            this.comboBox_Zone.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox_Zone.FormattingEnabled = true;
            this.comboBox_Zone.Location = new System.Drawing.Point(45, 15);
            this.comboBox_Zone.Name = "comboBox_Zone";
            this.comboBox_Zone.Size = new System.Drawing.Size(275, 21);
            this.comboBox_Zone.TabIndex = 5;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(7, 18);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(32, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "Zone";
            // 
            // radioButton_Custom
            // 
            this.radioButton_Custom.AutoSize = true;
            this.radioButton_Custom.Location = new System.Drawing.Point(10, 65);
            this.radioButton_Custom.Name = "radioButton_Custom";
            this.radioButton_Custom.Size = new System.Drawing.Size(110, 17);
            this.radioButton_Custom.TabIndex = 3;
            this.radioButton_Custom.Text = "Set UTC manually";
            this.radioButton_Custom.UseVisualStyleBackColor = true;
            // 
            // radioButton_UTC
            // 
            this.radioButton_UTC.AutoSize = true;
            this.radioButton_UTC.Checked = true;
            this.radioButton_UTC.Location = new System.Drawing.Point(10, 40);
            this.radioButton_UTC.Name = "radioButton_UTC";
            this.radioButton_UTC.Size = new System.Drawing.Size(69, 17);
            this.radioButton_UTC.TabIndex = 2;
            this.radioButton_UTC.TabStop = true;
            this.radioButton_UTC.Text = "Use UTC";
            this.radioButton_UTC.UseVisualStyleBackColor = true;
            // 
            // dateTimePicker_Date
            // 
            this.dateTimePicker_Date.Location = new System.Drawing.Point(10, 90);
            this.dateTimePicker_Date.MaxDate = new System.DateTime(2100, 12, 31, 0, 0, 0, 0);
            this.dateTimePicker_Date.Name = "dateTimePicker_Date";
            this.dateTimePicker_Date.Size = new System.Drawing.Size(310, 20);
            this.dateTimePicker_Date.TabIndex = 1;
            // 
            // button_SetDateTime
            // 
            this.button_SetDateTime.Location = new System.Drawing.Point(90, 140);
            this.button_SetDateTime.Name = "button_SetDateTime";
            this.button_SetDateTime.Size = new System.Drawing.Size(140, 25);
            this.button_SetDateTime.TabIndex = 0;
            this.button_SetDateTime.Text = "Set date and time";
            this.button_SetDateTime.UseVisualStyleBackColor = true;
            this.button_SetDateTime.Click += new System.EventHandler(this.button_SetDateTime_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(344, 509);
            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 Time";
            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_SetDateTime;
        private System.Windows.Forms.DateTimePicker dateTimePicker_Time;
        private ComboBox comboBox_Zone;
        private Label label2;
        private RadioButton radioButton_Custom;
        private RadioButton radioButton_UTC;
        private DateTimePicker dateTimePicker_Date;
    }
}
		
Code 2 - GUI example in C#

WPF version

MainWindow.xaml.cs

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

namespace ConfigureOnvifCameraRemotely02Wpf
{
    /// 
    /// 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();
            videoViewer.SetImageProvider(_drawingImageProvider);
            comboBox_Zone.ItemsSource = TimeZoneInfo.GetSystemTimeZones();
        }

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

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

        private void button_SetDateTime_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CameraDateTimeSetter config;

                if (radioButton_UTC.IsChecked == true)
                {
                    var datetime = DateTime.UtcNow;
                    config = new CameraDateTimeSetter(true)
                    {
                        TimeZoneInfo = (TimeZoneInfo)comboBox_Zone.SelectedItem,
                        UTCDate = new IPCameraDate(datetime.Year, datetime.Month, datetime.Day),
                        UTCTime = new IPCameraTime(datetime.Hour, datetime.Minute, datetime.Second)
                    };
                }
                else
                {
                    var date = dateTimePicker_Date.SelectedDate.Value;
                    var split = textBox_Time.Text.Split(':');
                    var time = new DateTime(date.Year, date.Month, date.Day, int.Parse(split[0]), int.Parse(split[1]),
                        int.Parse(split[2]));
                    config = new CameraDateTimeSetter(true)
                    {
                        TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("UTC"), // UTC + 0
                        UTCDate = new IPCameraDate(date.Year, date.Month, date.Day),
                        UTCTime = new IPCameraTime(time.Hour, time.Minute, time.Second)
                    };
                }
                _camera.DateAndTime.SetAttributes(config);
                _camera.DateAndTime.RefreshProperties();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
    }
}
		
Code 1 - Setting Onvif camera time 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. Besides this you can click the 'Set date and time' button which will set the date and time on the camera.

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="ConfigureOnvifCameraRemotely02Wpf.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 Time" Height="510" Width="340" 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="Set camera time" HorizontalAlignment="Left" Margin="10,268,0,0" VerticalAlignment="Top" Height="193" Width="308">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.8*"/>
               	<RowDefinition Height="0.5*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Label Grid.Column="0" Content="Zone" VerticalAlignment="Center"/>
                <ComboBox x:Name="comboBox_Zone" Grid.Column="1" Height="30"/>
            </Grid>
            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <RadioButton x:Name="radioButton_UTC" GroupName="TimeGroup" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Content="Use UTC" IsChecked="True"/>
                <RadioButton GroupName="TimeGroup" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1" Content="Set UTC manually"/>
            </Grid>
            <Grid Grid.Row="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="105"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Label Grid.Column="0" Content="Date"/>
                    <DatePicker x:Name="dateTimePicker_Date" Grid.Column="1" SelectedDateFormat="Long"/>
                </Grid>
                <Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="105"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Label Grid.Column="0" Content="Time (HH:MM:SS)"/>
                    <TextBox x:Name="textBox_Time" Grid.Column="1"/>
                </Grid>
            </Grid>
            <Button Grid.Row="3" Height="25" Width="60" HorizontalAlignment="Right" Content="Set" Click="button_SetDateTime_Click"/>
        </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