How to save / restore IP camera settings in C#
This example demonstrates how to save and restore the settings of an IP camera. 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.
How to save/restore an Onvif camera's settings 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 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\CConfigure_Save_Settings_WF\Configure_Save_Settings_WF.sln |
WPF version: | C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\Configure_Save_Settings_WPF\Configure_Save_Settings_WPF.sln |
To compile this example you will need Microsoft Visual Studio installed on your computer.
The additional statements and methods of this example are the following:
private Dictionary<string, List<IPCameraBackupFile>> _backupFilesDictionary;
This line represents a collection of keys and values. The keys will be the name of the backup file and values will be a list which contains IPCameraBackupFile objects.
When you click on the Save button, the program saves the name of the backup into the _backupFilesDictionary object's key attribute what you can provide in the textbox and the _camera object's GetSystemBackup() method returns a List<IPCameraBackupFiles> list. This list will be the _backupFilesDictionary object's value attribute. With this algorithm you can save many backup files with different names:
var name = textBox_Save.Text;
var backupFiles = _camera.GetSystemBackup();
_backupFilesDictionary[name] = backupFiles;
If you want to restore a backup file, you should give the name of the saved backup file and if the _backupFilesDictionary contains the given key than it will be restored by the RestoreSystem() method:
if (!_backupFilesDictionary.ContainsKey(textBox_Restore.Text)) return;
var backUpFile = _backupFilesDictionary[textBox_Restore.Text];
_camera.RestoreSystem(backUpFile);
Note: Some cameras may not support the backup and restore function. In addition, the exact format of the backup configuration files is outside the scope of Onvif specification.
Save/Restore camera settings example in C#
Windows Form | WPF |
Windows forms version
Form1.cs
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using Ozeki.Media; using Ozeki.Camera; namespace ConfigureOnvifCameraRemotely03 { public partial class Form1 : Form { private IIPCamera _camera; private DrawingImageProvider _imageProvider; private MediaConnector _connector; private VideoViewerWF _videoViewerWf; private List<IPCameraBackupFile> _backupFiles; private Dictionary<string, List<IPCameraBackupFile>> _backupFilesDictionary; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { _connector = new MediaConnector(); _imageProvider = new DrawingImageProvider(); _backupFilesDictionary = new Dictionary<string, List<IPCameraBackupFile>>(); _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(35, 25); _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_Save_Click(object sender, EventArgs e) { var name = textBox_Save.Text; _backupFiles = _camera.GetSystemBackup(); _backupFilesDictionary[name] = _backupFiles; textBox_Status.Text = "Camera backup file has been saved as " + name; } private void button_Restore_Click(object sender, EventArgs e) { if (!_backupFilesDictionary.ContainsKey(textBox_Restore.Text)) return; var backUpFile = _backupFilesDictionary[textBox_Restore.Text]; _camera.RestoreSystem(backUpFile); textBox_Status.Text = "Camera backup file has been restored from " + textBox_Restore.Text; } } }
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
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 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 ConfigureOnvifCameraRemotely03 { 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.textBox_Status = new System.Windows.Forms.TextBox(); this.textBox_Restore = new System.Windows.Forms.TextBox(); this.textBox_Save = new System.Windows.Forms.TextBox(); this.button_Restore = new System.Windows.Forms.Button(); this.button_Save = 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(110, 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(15, 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, 130); this.CameraBox.Name = "CameraBox"; this.CameraBox.Size = new System.Drawing.Size(335, 230); this.CameraBox.TabIndex = 3; this.CameraBox.TabStop = false; this.CameraBox.Text = "Live camera "; // // groupBox2 // this.groupBox2.Controls.Add(this.textBox_Status); this.groupBox2.Controls.Add(this.textBox_Restore); this.groupBox2.Controls.Add(this.textBox_Save); this.groupBox2.Controls.Add(this.button_Restore); this.groupBox2.Controls.Add(this.button_Save); this.groupBox2.Location = new System.Drawing.Point(130, 10); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(215, 120); this.groupBox2.TabIndex = 4; this.groupBox2.TabStop = false; this.groupBox2.Text = "Save/Restore camera settings"; // // textBox_Status // this.textBox_Status.Location = new System.Drawing.Point(7, 70); this.textBox_Status.Multiline = true; this.textBox_Status.Name = "textBox_Status"; this.textBox_Status.ReadOnly = true; this.textBox_Status.Size = new System.Drawing.Size(200, 40); this.textBox_Status.TabIndex = 4; // // textBox_Restore // this.textBox_Restore.Location = new System.Drawing.Point(87, 42); this.textBox_Restore.Name = "textBox_Restore"; this.textBox_Restore.Size = new System.Drawing.Size(120, 20); this.textBox_Restore.TabIndex = 3; this.textBox_Restore.Text = "backup"; // // textBox_Save // this.textBox_Save.Location = new System.Drawing.Point(87, 17); this.textBox_Save.Name = "textBox_Save"; this.textBox_Save.Size = new System.Drawing.Size(120, 20); this.textBox_Save.TabIndex = 2; this.textBox_Save.Text = "backup"; // // button_Restore // this.button_Restore.Location = new System.Drawing.Point(5, 40); this.button_Restore.Name = "button_Restore"; this.button_Restore.Size = new System.Drawing.Size(75, 23); this.button_Restore.TabIndex = 1; this.button_Restore.Text = "Restore"; this.button_Restore.UseVisualStyleBackColor = true; this.button_Restore.Click += new System.EventHandler(this.button_Restore_Click); // // button_Save // this.button_Save.Location = new System.Drawing.Point(5, 15); this.button_Save.Name = "button_Save"; this.button_Save.Size = new System.Drawing.Size(75, 23); this.button_Save.TabIndex = 0; this.button_Save.Text = "Save"; this.button_Save.UseVisualStyleBackColor = true; this.button_Save.Click += new System.EventHandler(this.button_Save_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(354, 374); 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 Save/Restore Settings"; this.Load += new System.EventHandler(this.Form1_Load); 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_Restore; private System.Windows.Forms.Button button_Save; private System.Windows.Forms.TextBox textBox_Restore; private System.Windows.Forms.TextBox textBox_Save; private System.Windows.Forms.TextBox textBox_Status; } }
WPF version
MainWindow.xaml.cs
using System.Collections.Generic; using System.Windows; using Ozeki.Media; using Ozeki.Camera; namespace ConfigureOnvifCameraRemotely03Wpf { ////// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private IIPCamera _camera; private DrawingImageProvider _drawingImageProvider; private MediaConnector _connector; private List_backupFiles; private Dictionary > _backupFilesDictionary; public MainWindow() { InitializeComponent(); _drawingImageProvider = new DrawingImageProvider(); _connector = new MediaConnector(); _backupFilesDictionary = new Dictionary >(); } 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_Save_Click(object sender, RoutedEventArgs e) { var name = textBox_Save.Text; _backupFiles = _camera.GetSystemBackup(); _backupFilesDictionary[name] = _backupFiles; textBox_Status.Text = "Camera backup file has been saved as " + name; } private void button_Restore_Click(object sender, RoutedEventArgs e) { if (!_backupFilesDictionary.ContainsKey(textBox_Restore.Text)) return; var backUpFile = _backupFilesDictionary[textBox_Restore.Text]; _camera.RestoreSystem(backUpFile); textBox_Status.Text = "Camera backup file has been restored from " + textBox_Restore.Text; } } }
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
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="ConfigureOnvifCameraRemotely03Wpf.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 Save/Restore Settings" Height="424" Width="340" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen"> <Grid> <GroupBox Header="Live camera" HorizontalAlignment="Left" Margin="10,149,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="Save/Restore camera settings" HorizontalAlignment="Left" Margin="101,12,0,0" VerticalAlignment="Top" Height="132" Width="217"> <Grid HorizontalAlignment="Left" Height="107" VerticalAlignment="Top" Width="207" Margin="0,0,-2,0"> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <Button Content="Save" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Click="button_Save_Click"/> <TextBox x:Name="textBox_Save" HorizontalAlignment="Right" Height="20" TextWrapping="Wrap" Text="backup" VerticalAlignment="Center" Width="100"/> <Button Content="Restore" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Grid.Row="1" Click="button_Restore_Click"/> <TextBox x:Name="textBox_Restore" HorizontalAlignment="Right" Height="20" TextWrapping="Wrap" Text="backup" VerticalAlignment="Center" Width="100" Grid.Row="1"/> <TextBox x:Name="textBox_Status" HorizontalAlignment="Left" Height="36" Grid.Row="2" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="207" IsEnabled="False"/> </Grid> </GroupBox> </Grid> </Window>
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:
-
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.)
-
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.
-
How can I test the saving and restoring?
With this program you can connect to the camera and save the settings, then open a previous application, where you can move the camera or modify the brigthness of it. Now go back and restore the settings using this application. You will see that the camera went back to the position and the brightness of the saved state.
More information
- How to get detailed camera information and capabilities in C#
- How to set the camera time in C#
- How to save or restore camera settings in C#
- How to reset camera settings in C#
- How to reboot the camera in C#
- C# camera username and password
- How to configure the camera network settings (IP, subnet, gateway, dns) in C#
- How to turn device visibility on or off for Onvif discovery in C#
- How to set or query the camera name and location in C#
- How to use certificates to authenticate the camera in C#