How to configure the IP camera network settings in C#
In this guide you can find information on how to configure the camera's network settings (IP, subnet, gateway, dns). 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 configure the camera's network 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\Configure_Network_Settings_WF\Configure_Network_Settings_WF.sln |
WPF version: | C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\Configure_Network_Settings_WPF\Configure_Network_Settings_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:
First of all, when you connected a camera, you should clear the previous camera's network information by calling the ClearGUI() method:
textBox_IP.Text = String.Empty;
textBox_Host.Text = String.Empty;
textBox_Netmask.Text = String.Empty;
textBox_Gateway.Text = String.Empty;
textBox_DNS.Text = String.Empty;
textBox_NTP_IP.Text = String.Empty;
textBox_HTTP.Text = String.Empty;
textBox_HTTPS.Text = String.Empty;
textBox_RTSP.Text = String.Empty;
comboBox_HTTP.Items.Clear();
comboBox_HTTPS.Items.Clear();
comboBox_RTSP.Items.Clear();
After this, you can query the connected camera's current network settings in GetNetworkSettings() method by querying the _camera object NetworkManager.DefaultConfig properties (IPAddress, HostName, Netmask, DefaultGateway, DNS, NTP):
_camera.NetworkManager.DefaultConfig.IPAddress;
_camera.NetworkManager.DefaultConfig.HostName;
_camera.NetworkManager.DefaultConfig.Netmask;
_camera.NetworkManager.DefaultConfig.DefaultGateway;
_camera.NetworkManager.DefaultConfig.DNS;
_camera.NetworkManager.DefaultConfig.NTP.IPAddress;
And you can query HTTP, HTTPS, RTSP ports if your camera supports the different port settings:
_camera.NetworkManager.HttpPort.Port
_camera.NetworkManager.HttpsPort.Port
_camera.NetworkManager.RtspPort.Port
In the GUI you can choose between using DHCP and Manual settings by selecting the proper radio button. If you want to use DHCP settings, you have to check the Use DHCP radio button and click on the Apply button to set UseDHCP property to true value:
_camera.NetworkManager.DefaultConfig.UseDHCP = true;
It is also important to apply the configuration settings by calling your camera object's ApplyConfig() method:
_camera.NetworkManager.ApplyConfig();
If you choose the Manual settings radio button, you can set the above network properties by filling out the textboxes of the GUI and clicking to the Apply button.
Configure the camera's network settings 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 ConfigureOnvifCameraRemotely07 { public partial class Form1 : Form { private IIPCamera _camera; private DrawingImageProvider _imageProvider; private MediaConnector _connector; private VideoViewerWF _videoViewerWf; private List<int> _ports; public Form1() { InitializeComponent(); _ports = new List<int>(); _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"); _camera.CameraStateChanged += _camera_CameraStateChanged; _connector.Connect(_camera.VideoChannel, _imageProvider); _videoViewerWf.SetImageProvider(_imageProvider); _videoViewerWf.Start(); _camera.Start(); } private void _camera_CameraStateChanged(object sender, CameraStateEventArgs e) { if (e.State == CameraState.Streaming) { InvokeGuiThread(() => groupBox_Network.Enabled = true); ClearGUI(); GetNetworkSettings(); if (!_camera.NetworkManager.DefaultConfig.UseDHCP) InvokeGuiThread(() => radioButton_Manual.Checked = true); } } private void ClearGUI() { InvokeGuiThread(() => { textBox_IP.Text = String.Empty; textBox_Host.Text = String.Empty; textBox_Netmask.Text = String.Empty; textBox_Gateway.Text = String.Empty; textBox_DNS.Text = String.Empty; textBox_NTP_IP.Text = String.Empty; textBox_HTTP.Text = String.Empty; textBox_HTTPS.Text = String.Empty; textBox_RTSP.Text = String.Empty; comboBox_HTTP.Items.Clear(); comboBox_HTTPS.Items.Clear(); comboBox_RTSP.Items.Clear(); }); } private void GetNetworkSettings() { InvokeGuiThread(() => { textBox_IP.Text = _camera.NetworkManager.DefaultConfig.IPAddress; textBox_Host.Text = _camera.NetworkManager.DefaultConfig.HostName; textBox_Netmask.Text = _camera.NetworkManager.DefaultConfig.Netmask; textBox_Gateway.Text = _camera.NetworkManager.DefaultConfig.DefaultGateway; textBox_DNS.Text = _camera.NetworkManager.DefaultConfig.DNS; textBox_NTP_IP.Text = _camera.NetworkManager.DefaultConfig.NTP.IPAddress; if (_camera.NetworkManager.HttpPort == null) textBox_HTTP.Enabled = false; else { foreach (var ports in _camera.NetworkManager.HttpPort.Port) { comboBox_HTTP.Items.Add(ports); } } if (_camera.NetworkManager.HttpsPort == null) textBox_HTTPS.Enabled = false; else { foreach (var ports in _camera.NetworkManager.HttpsPort.Port) { comboBox_HTTPS.Items.Add(ports); } } if (_camera.NetworkManager.RtspPort == null) textBox_RTSP.Enabled = false; else { foreach (var ports in _camera.NetworkManager.RtspPort.Port) { comboBox_RTSP.Items.Add(ports); } } }); } private void button_Apply_Click(object sender, EventArgs e) { if (radioButton_DHCP.Checked) { _camera.NetworkManager.DefaultConfig.UseDHCP = true; } if (radioButton_Manual.Checked) { _camera.NetworkManager.DefaultConfig.UseDHCP = false; _camera.NetworkManager.DefaultConfig.IPAddress = textBox_IP.Text; _camera.NetworkManager.DefaultConfig.HostName = textBox_Host.Text; _camera.NetworkManager.DefaultConfig.Netmask = textBox_Netmask.Text; _camera.NetworkManager.DefaultConfig.DefaultGateway = textBox_Gateway.Text; _camera.NetworkManager.DefaultConfig.DNS = textBox_DNS.Text; if (_camera.NetworkManager.HttpPort != null) { _ports.Clear(); foreach (var item in comboBox_HTTP.Items) { var ports = Int32.Parse(item.ToString()); _ports.Add(ports); } _camera.NetworkManager.HttpPort = new CameraPort(CameraProtocolType.HTTP, true, _ports.ToArray()); } if (_camera.NetworkManager.HttpsPort != null) { _ports.Clear(); foreach (var item in comboBox_HTTPS.Items) { var ports = Int32.Parse(item.ToString()); _ports.Add(ports); } _camera.NetworkManager.HttpsPort = new CameraPort(CameraProtocolType.HTTPS, true, _ports.ToArray()); } if (_camera.NetworkManager.RtspPort != null) { _ports.Clear(); foreach (var item in comboBox_RTSP.Items) { var ports = Int32.Parse(item.ToString()); _ports.Add(ports); } _camera.NetworkManager.RtspPort = new CameraPort(CameraProtocolType.RTSP, true, _ports.ToArray()); } } _camera.NetworkManager.ApplyConfig(); } private void button_HTTP_Add_Click(object sender, EventArgs e) { if (!comboBox_HTTP.Items.Contains(textBox_HTTP.Text)) comboBox_HTTP.Items.Add(textBox_HTTP.Text); } private void button_HTTPS_Add_Click(object sender, EventArgs e) { if (!comboBox_HTTPS.Items.Contains(textBox_HTTPS.Text)) comboBox_HTTPS.Items.Add(textBox_HTTPS.Text); } private void button_RTSP_Add_Click(object sender, EventArgs e) { if (!comboBox_RTSP.Items.Contains(textBox_RTSP.Text)) comboBox_RTSP.Items.Add(textBox_RTSP.Text); } private void button_HTTP_Delete_Click(object sender, EventArgs e) { for (int i = 0; i < comboBox_HTTP.Items.Count; i++) { if (comboBox_HTTP.Items[i].ToString() == textBox_HTTP.Text) comboBox_HTTP.Items.Remove(comboBox_HTTP.Items[i]); } } private void button_HTTPS_Delete_Click(object sender, EventArgs e) { for (int i = 0; i < comboBox_HTTPS.Items.Count; i++) { if (comboBox_HTTPS.Items[i].ToString() == textBox_HTTPS.Text) comboBox_HTTPS.Items.Remove(comboBox_HTTPS.Items[i]); } } private void button_RTSP_Delete_Click(object sender, EventArgs e) { for (int i = 0; i < comboBox_RTSP.Items.Count; i++) { if (comboBox_RTSP.Items[i].ToString() == textBox_RTSP.Text) comboBox_RTSP.Items.Remove(comboBox_RTSP.Items[i]); } } private void radioButton_Manual_CheckedChanged(object sender, EventArgs e) { groupBox_Basic.Enabled = true; groupBox_Ports.Enabled = true; } private void radioButton_DHCP_CheckedChanged(object sender, EventArgs e) { groupBox_Basic.Enabled = false; groupBox_Ports.Enabled = false; } private void InvokeGuiThread(Action action) { BeginInvoke(action); } } }
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. The network settings are not supported in all IPcamera devices.
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
using System.Windows.Forms; namespace ConfigureOnvifCameraRemotely07 { 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.groupBox_Network = new System.Windows.Forms.GroupBox(); this.groupBox_Basic = new System.Windows.Forms.GroupBox(); this.label1 = new System.Windows.Forms.Label(); this.textBox_IP = new System.Windows.Forms.TextBox(); this.textBox_NTP_IP = new System.Windows.Forms.TextBox(); this.textBox_Netmask = new System.Windows.Forms.TextBox(); this.label10 = new System.Windows.Forms.Label(); this.textBox_Gateway = new System.Windows.Forms.TextBox(); this.textBox_DNS = new System.Windows.Forms.TextBox(); this.label6 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox_Host = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.groupBox_Ports = new System.Windows.Forms.GroupBox(); this.button_RTSP_Delete = new System.Windows.Forms.Button(); this.button_HTTPS_Delete = new System.Windows.Forms.Button(); this.button_HTTP_Delete = new System.Windows.Forms.Button(); this.textBox_RTSP = new System.Windows.Forms.TextBox(); this.textBox_HTTPS = new System.Windows.Forms.TextBox(); this.textBox_HTTP = new System.Windows.Forms.TextBox(); this.button_RTSP_Add = new System.Windows.Forms.Button(); this.button_HTTPS_Add = new System.Windows.Forms.Button(); this.button_HTTP_Add = new System.Windows.Forms.Button(); this.comboBox_RTSP = new System.Windows.Forms.ComboBox(); this.comboBox_HTTPS = new System.Windows.Forms.ComboBox(); this.comboBox_HTTP = new System.Windows.Forms.ComboBox(); this.label8 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.label9 = new System.Windows.Forms.Label(); this.button_Apply = new System.Windows.Forms.Button(); this.radioButton_Manual = new System.Windows.Forms.RadioButton(); this.radioButton_DHCP = new System.Windows.Forms.RadioButton(); this.groupBox1.SuspendLayout(); this.groupBox_Network.SuspendLayout(); this.groupBox_Basic.SuspendLayout(); this.groupBox_Ports.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(321, 230); this.CameraBox.TabIndex = 3; this.CameraBox.TabStop = false; this.CameraBox.Text = "Live camera "; // // groupBox_Network // this.groupBox_Network.Controls.Add(this.groupBox_Basic); this.groupBox_Network.Controls.Add(this.groupBox_Ports); this.groupBox_Network.Controls.Add(this.button_Apply); this.groupBox_Network.Controls.Add(this.radioButton_Manual); this.groupBox_Network.Controls.Add(this.radioButton_DHCP); this.groupBox_Network.Enabled = false; this.groupBox_Network.Location = new System.Drawing.Point(12, 316); this.groupBox_Network.Name = "groupBox_Network"; this.groupBox_Network.Size = new System.Drawing.Size(319, 415); this.groupBox_Network.TabIndex = 4; this.groupBox_Network.TabStop = false; this.groupBox_Network.Text = "Network settings"; // // groupBox_Basic // this.groupBox_Basic.Controls.Add(this.label1); this.groupBox_Basic.Controls.Add(this.textBox_IP); this.groupBox_Basic.Controls.Add(this.textBox_NTP_IP); this.groupBox_Basic.Controls.Add(this.textBox_Netmask); this.groupBox_Basic.Controls.Add(this.label10); this.groupBox_Basic.Controls.Add(this.textBox_Gateway); this.groupBox_Basic.Controls.Add(this.textBox_DNS); this.groupBox_Basic.Controls.Add(this.label6); this.groupBox_Basic.Controls.Add(this.label2); this.groupBox_Basic.Controls.Add(this.textBox_Host); this.groupBox_Basic.Controls.Add(this.label3); this.groupBox_Basic.Controls.Add(this.label4); this.groupBox_Basic.Enabled = false; this.groupBox_Basic.Location = new System.Drawing.Point(18, 80); this.groupBox_Basic.Name = "groupBox_Basic"; this.groupBox_Basic.Size = new System.Drawing.Size(286, 178); this.groupBox_Basic.TabIndex = 28; this.groupBox_Basic.TabStop = false; this.groupBox_Basic.Text = "Basic settings"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(7, 19); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(60, 13); this.label1.TabIndex = 7; this.label1.Text = "IP address:"; // // textBox_IP // this.textBox_IP.Location = new System.Drawing.Point(85, 16); this.textBox_IP.Name = "textBox_IP"; this.textBox_IP.Size = new System.Drawing.Size(193, 20); this.textBox_IP.TabIndex = 2; // // textBox_NTP_IP // this.textBox_NTP_IP.Location = new System.Drawing.Point(85, 148); this.textBox_NTP_IP.Name = "textBox_NTP_IP"; this.textBox_NTP_IP.ReadOnly = true; this.textBox_NTP_IP.Size = new System.Drawing.Size(193, 20); this.textBox_NTP_IP.TabIndex = 26; // // textBox_Netmask // this.textBox_Netmask.Location = new System.Drawing.Point(84, 68); this.textBox_Netmask.Name = "textBox_Netmask"; this.textBox_Netmask.Size = new System.Drawing.Size(193, 20); this.textBox_Netmask.TabIndex = 3; // // label10 // this.label10.AutoSize = true; this.label10.Location = new System.Drawing.Point(6, 151); this.label10.Name = "label10"; this.label10.Size = new System.Drawing.Size(64, 13); this.label10.TabIndex = 24; this.label10.Text = "NTP server:"; // // textBox_Gateway // this.textBox_Gateway.Location = new System.Drawing.Point(84, 96); this.textBox_Gateway.Name = "textBox_Gateway"; this.textBox_Gateway.Size = new System.Drawing.Size(193, 20); this.textBox_Gateway.TabIndex = 4; // // textBox_DNS // this.textBox_DNS.Location = new System.Drawing.Point(85, 123); this.textBox_DNS.Name = "textBox_DNS"; this.textBox_DNS.Size = new System.Drawing.Size(193, 20); this.textBox_DNS.TabIndex = 5; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(6, 45); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(61, 13); this.label6.TabIndex = 13; this.label6.Text = "Host name:"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(6, 71); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(72, 13); this.label2.TabIndex = 8; this.label2.Text = "Subnet mask:"; // // textBox_Host // this.textBox_Host.Location = new System.Drawing.Point(85, 42); this.textBox_Host.Name = "textBox_Host"; this.textBox_Host.Size = new System.Drawing.Size(193, 20); this.textBox_Host.TabIndex = 12; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(6, 99); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(52, 13); this.label3.TabIndex = 9; this.label3.Text = "Gateway:"; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(6, 125); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(33, 13); this.label4.TabIndex = 10; this.label4.Text = "DNS:"; // // groupBox_Ports // this.groupBox_Ports.Controls.Add(this.button_RTSP_Delete); this.groupBox_Ports.Controls.Add(this.button_HTTPS_Delete); this.groupBox_Ports.Controls.Add(this.button_HTTP_Delete); this.groupBox_Ports.Controls.Add(this.textBox_RTSP); this.groupBox_Ports.Controls.Add(this.textBox_HTTPS); this.groupBox_Ports.Controls.Add(this.textBox_HTTP); this.groupBox_Ports.Controls.Add(this.button_RTSP_Add); this.groupBox_Ports.Controls.Add(this.button_HTTPS_Add); this.groupBox_Ports.Controls.Add(this.button_HTTP_Add); this.groupBox_Ports.Controls.Add(this.comboBox_RTSP); this.groupBox_Ports.Controls.Add(this.comboBox_HTTPS); this.groupBox_Ports.Controls.Add(this.comboBox_HTTP); this.groupBox_Ports.Controls.Add(this.label8); this.groupBox_Ports.Controls.Add(this.label7); this.groupBox_Ports.Controls.Add(this.label9); this.groupBox_Ports.Enabled = false; this.groupBox_Ports.Location = new System.Drawing.Point(18, 264); this.groupBox_Ports.Name = "groupBox_Ports"; this.groupBox_Ports.Size = new System.Drawing.Size(286, 108); this.groupBox_Ports.TabIndex = 27; this.groupBox_Ports.TabStop = false; this.groupBox_Ports.Text = "Port settings"; // // button_RTSP_Delete // this.button_RTSP_Delete.Location = new System.Drawing.Point(232, 74); this.button_RTSP_Delete.Name = "button_RTSP_Delete"; this.button_RTSP_Delete.Size = new System.Drawing.Size(46, 23); this.button_RTSP_Delete.TabIndex = 31; this.button_RTSP_Delete.Text = "Delete"; this.button_RTSP_Delete.UseVisualStyleBackColor = true; this.button_RTSP_Delete.Click += new System.EventHandler(this.button_RTSP_Delete_Click); // // button_HTTPS_Delete // this.button_HTTPS_Delete.Location = new System.Drawing.Point(232, 44); this.button_HTTPS_Delete.Name = "button_HTTPS_Delete"; this.button_HTTPS_Delete.Size = new System.Drawing.Size(46, 23); this.button_HTTPS_Delete.TabIndex = 30; this.button_HTTPS_Delete.Text = "Delete"; this.button_HTTPS_Delete.UseVisualStyleBackColor = true; this.button_HTTPS_Delete.Click += new System.EventHandler(this.button_HTTPS_Delete_Click); // // button_HTTP_Delete // this.button_HTTP_Delete.Location = new System.Drawing.Point(232, 14); this.button_HTTP_Delete.Name = "button_HTTP_Delete"; this.button_HTTP_Delete.Size = new System.Drawing.Size(46, 23); this.button_HTTP_Delete.TabIndex = 29; this.button_HTTP_Delete.Text = "Delete"; this.button_HTTP_Delete.UseVisualStyleBackColor = true; this.button_HTTP_Delete.Click += new System.EventHandler(this.button_HTTP_Delete_Click); // // textBox_RTSP // this.textBox_RTSP.Location = new System.Drawing.Point(151, 76); this.textBox_RTSP.Name = "textBox_RTSP"; this.textBox_RTSP.Size = new System.Drawing.Size(30, 20); this.textBox_RTSP.TabIndex = 28; // // textBox_HTTPS // this.textBox_HTTPS.Location = new System.Drawing.Point(152, 46); this.textBox_HTTPS.Name = "textBox_HTTPS"; this.textBox_HTTPS.Size = new System.Drawing.Size(30, 20); this.textBox_HTTPS.TabIndex = 27; // // textBox_HTTP // this.textBox_HTTP.Location = new System.Drawing.Point(152, 16); this.textBox_HTTP.Name = "textBox_HTTP"; this.textBox_HTTP.Size = new System.Drawing.Size(30, 20); this.textBox_HTTP.TabIndex = 26; // // button_RTSP_Add // this.button_RTSP_Add.Location = new System.Drawing.Point(189, 74); this.button_RTSP_Add.Name = "button_RTSP_Add"; this.button_RTSP_Add.Size = new System.Drawing.Size(37, 23); this.button_RTSP_Add.TabIndex = 25; this.button_RTSP_Add.Text = "Add"; this.button_RTSP_Add.UseVisualStyleBackColor = true; this.button_RTSP_Add.Click += new System.EventHandler(this.button_RTSP_Add_Click); // // button_HTTPS_Add // this.button_HTTPS_Add.Location = new System.Drawing.Point(188, 44); this.button_HTTPS_Add.Name = "button_HTTPS_Add"; this.button_HTTPS_Add.Size = new System.Drawing.Size(38, 23); this.button_HTTPS_Add.TabIndex = 24; this.button_HTTPS_Add.Text = "Add"; this.button_HTTPS_Add.UseVisualStyleBackColor = true; this.button_HTTPS_Add.Click += new System.EventHandler(this.button_HTTPS_Add_Click); // // button_HTTP_Add // this.button_HTTP_Add.Location = new System.Drawing.Point(188, 14); this.button_HTTP_Add.Name = "button_HTTP_Add"; this.button_HTTP_Add.Size = new System.Drawing.Size(38, 23); this.button_HTTP_Add.TabIndex = 23; this.button_HTTP_Add.Text = "Add"; this.button_HTTP_Add.UseVisualStyleBackColor = true; this.button_HTTP_Add.Click += new System.EventHandler(this.button_HTTP_Add_Click); // // comboBox_RTSP // this.comboBox_RTSP.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBox_RTSP.FormattingEnabled = true; this.comboBox_RTSP.Location = new System.Drawing.Point(84, 76); this.comboBox_RTSP.Name = "comboBox_RTSP"; this.comboBox_RTSP.Size = new System.Drawing.Size(61, 21); this.comboBox_RTSP.TabIndex = 22; // // comboBox_HTTPS // this.comboBox_HTTPS.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBox_HTTPS.FormattingEnabled = true; this.comboBox_HTTPS.Location = new System.Drawing.Point(85, 46); this.comboBox_HTTPS.Name = "comboBox_HTTPS"; this.comboBox_HTTPS.Size = new System.Drawing.Size(61, 21); this.comboBox_HTTPS.TabIndex = 21; // // comboBox_HTTP // this.comboBox_HTTP.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBox_HTTP.FormattingEnabled = true; this.comboBox_HTTP.Location = new System.Drawing.Point(84, 16); this.comboBox_HTTP.Name = "comboBox_HTTP"; this.comboBox_HTTP.Size = new System.Drawing.Size(61, 21); this.comboBox_HTTP.TabIndex = 20; // // label8 // this.label8.AutoSize = true; this.label8.Location = new System.Drawing.Point(6, 49); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(72, 13); this.label8.TabIndex = 18; this.label8.Text = "HTTPS ports:"; // // label7 // this.label7.AutoSize = true; this.label7.Location = new System.Drawing.Point(7, 19); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(65, 13); this.label7.TabIndex = 17; this.label7.Text = "HTTP ports:"; // // label9 // this.label9.AutoSize = true; this.label9.Location = new System.Drawing.Point(7, 79); this.label9.Name = "label9"; this.label9.Size = new System.Drawing.Size(65, 13); this.label9.TabIndex = 19; this.label9.Text = "RTSP ports:"; // // button_Apply // this.button_Apply.Location = new System.Drawing.Point(114, 378); this.button_Apply.Name = "button_Apply"; this.button_Apply.Size = new System.Drawing.Size(75, 23); this.button_Apply.TabIndex = 20; this.button_Apply.Text = "Apply"; this.button_Apply.UseVisualStyleBackColor = true; this.button_Apply.Click += new System.EventHandler(this.button_Apply_Click); // // radioButton_Manual // this.radioButton_Manual.AutoSize = true; this.radioButton_Manual.Location = new System.Drawing.Point(7, 44); this.radioButton_Manual.Name = "radioButton_Manual"; this.radioButton_Manual.Size = new System.Drawing.Size(140, 17); this.radioButton_Manual.TabIndex = 1; this.radioButton_Manual.Text = "Manual network settings"; this.radioButton_Manual.UseVisualStyleBackColor = true; this.radioButton_Manual.CheckedChanged += new System.EventHandler(this.radioButton_Manual_CheckedChanged); // // radioButton_DHCP // this.radioButton_DHCP.AutoSize = true; this.radioButton_DHCP.Checked = true; this.radioButton_DHCP.Location = new System.Drawing.Point(7, 20); this.radioButton_DHCP.Name = "radioButton_DHCP"; this.radioButton_DHCP.Size = new System.Drawing.Size(77, 17); this.radioButton_DHCP.TabIndex = 0; this.radioButton_DHCP.TabStop = true; this.radioButton_DHCP.Text = "Use DHCP"; this.radioButton_DHCP.UseVisualStyleBackColor = true; this.radioButton_DHCP.CheckedChanged += new System.EventHandler(this.radioButton_DHCP_CheckedChanged); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(345, 746); this.Controls.Add(this.groupBox_Network); 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 = "Configure the camera\'s network settings"; this.groupBox1.ResumeLayout(false); this.groupBox_Network.ResumeLayout(false); this.groupBox_Network.PerformLayout(); this.groupBox_Basic.ResumeLayout(false); this.groupBox_Basic.PerformLayout(); this.groupBox_Ports.ResumeLayout(false); this.groupBox_Ports.PerformLayout(); this.ResumeLayout(false); } private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button button_Connect; private System.Windows.Forms.GroupBox CameraBox; private GroupBox groupBox_Network; private Label label2; private Label label1; private TextBox textBox_DNS; private TextBox textBox_Gateway; private TextBox textBox_Netmask; private TextBox textBox_IP; private RadioButton radioButton_Manual; private RadioButton radioButton_DHCP; private Label label9; private Label label8; private Label label7; private Label label6; private TextBox textBox_Host; private Label label4; private Label label3; private Button button_Apply; private Label label10; private TextBox textBox_NTP_IP; private GroupBox groupBox_Ports; private GroupBox groupBox_Basic; private TextBox textBox_RTSP; private TextBox textBox_HTTPS; private TextBox textBox_HTTP; private Button button_RTSP_Add; private Button button_HTTPS_Add; private Button button_HTTP_Add; private ComboBox comboBox_RTSP; private ComboBox comboBox_HTTPS; private ComboBox comboBox_HTTP; private Button button_RTSP_Delete; private Button button_HTTPS_Delete; private Button button_HTTP_Delete; } }
WPF version
MainWindow.xaml.cs
using System; using System.Collections.Generic; using System.Windows; using Ozeki.Camera; using Ozeki.Media; namespace ConfigureOnvifCameraRemotely07Wpf { ////// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private IIPCamera _camera; private DrawingImageProvider _drawingImageProvider; private MediaConnector _connector; private List<int> _ports; public MainWindow() { InitializeComponent(); _drawingImageProvider = new DrawingImageProvider(); _connector = new MediaConnector(); _ports = new List<int>(); } private void Connect_Click(object sender, RoutedEventArgs e) { _camera = new IPCamera("192.168.112.109:8080", "user", "qwe123"); _camera.CameraStateChanged += _camera_CameraStateChanged; _connector.Connect(_camera.VideoChannel, _drawingImageProvider); videoViewer.SetImageProvider(_drawingImageProvider); videoViewer.Start(); _camera.Start(); } private void _camera_CameraStateChanged(object sender, CameraStateEventArgs e) { if (e.State == CameraState.Streaming) { InvokeGuiThread(() => groupBox_Network.IsEnabled = true); ClearGUI(); GetNetworkSettings(); if (!_camera.NetworkManager.DefaultConfig.UseDHCP) InvokeGuiThread(() => radioButton_Manual.IsChecked = true); } } private void ClearGUI() { InvokeGuiThread(() => { textBox_IP.Text = String.Empty; textBox_Host.Text = String.Empty; textBox_Netmask.Text = String.Empty; textBox_Gateway.Text = String.Empty; textBox_DNS.Text = String.Empty; textBox_NTP_IP.Text = String.Empty; textBox_HTTP.Text = String.Empty; textBox_HTTPS.Text = String.Empty; textBox_RTSP.Text = String.Empty; comboBox_HTTP.Items.Clear(); comboBox_HTTPS.Items.Clear(); comboBox_RTSP.Items.Clear(); }); } private void GetNetworkSettings() { InvokeGuiThread(() => { textBox_IP.Text = _camera.NetworkManager.DefaultConfig.IPAddress; textBox_Host.Text = _camera.NetworkManager.DefaultConfig.HostName; textBox_Netmask.Text = _camera.NetworkManager.DefaultConfig.Netmask; textBox_Gateway.Text = _camera.NetworkManager.DefaultConfig.DefaultGateway; textBox_DNS.Text = _camera.NetworkManager.DefaultConfig.DNS; textBox_NTP_IP.Text = _camera.NetworkManager.DefaultConfig.NTP.IPAddress; if (_camera.NetworkManager.HttpPort == null) textBox_HTTP.IsEnabled = false; else { foreach (var ports in _camera.NetworkManager.HttpPort.Port) { comboBox_HTTP.Items.Add(ports); } } if (_camera.NetworkManager.HttpsPort == null) textBox_HTTPS.IsEnabled = false; else { foreach (var ports in _camera.NetworkManager.HttpsPort.Port) { comboBox_HTTPS.Items.Add(ports); } } if (_camera.NetworkManager.RtspPort == null) textBox_RTSP.IsEnabled = false; else { foreach (var ports in _camera.NetworkManager.RtspPort.Port) { comboBox_RTSP.Items.Add(ports); } } }); } private void button_Apply_Click(object sender, RoutedEventArgs e) { if (radioButton_DHCP.IsChecked == true) { _camera.NetworkManager.DefaultConfig.UseDHCP = true; } if (radioButton_Manual.IsChecked == true) { _camera.NetworkManager.DefaultConfig.UseDHCP = false; _camera.NetworkManager.DefaultConfig.IPAddress = textBox_IP.Text; _camera.NetworkManager.DefaultConfig.HostName = textBox_Host.Text; _camera.NetworkManager.DefaultConfig.Netmask = textBox_Netmask.Text; _camera.NetworkManager.DefaultConfig.DefaultGateway = textBox_Gateway.Text; _camera.NetworkManager.DefaultConfig.DNS = textBox_DNS.Text; if (_camera.NetworkManager.HttpPort != null) { _ports.Clear(); foreach (var item in comboBox_HTTP.Items) { var ports = Int32.Parse(item.ToString()); _ports.Add(ports); } _camera.NetworkManager.HttpPort = new CameraPort(CameraProtocolType.HTTP, true, _ports.ToArray()); } if (_camera.NetworkManager.HttpsPort != null) { _ports.Clear(); foreach (var item in comboBox_HTTPS.Items) { var ports = Int32.Parse(item.ToString()); _ports.Add(ports); } _camera.NetworkManager.HttpsPort = new CameraPort(CameraProtocolType.HTTPS, true, _ports.ToArray()); } if (_camera.NetworkManager.RtspPort != null) { _ports.Clear(); foreach (var item in comboBox_RTSP.Items) { var ports = Int32.Parse(item.ToString()); _ports.Add(ports); } _camera.NetworkManager.RtspPort = new CameraPort(CameraProtocolType.RTSP, true, _ports.ToArray()); } } _camera.NetworkManager.ApplyConfig(); } private void button_HTTP_Add_Click(object sender, RoutedEventArgs e) { if (!comboBox_HTTP.Items.Contains(textBox_HTTP.Text)) comboBox_HTTP.Items.Add(textBox_HTTP.Text); } private void button_HTTPS_Add_Click(object sender, RoutedEventArgs e) { if (!comboBox_HTTPS.Items.Contains(textBox_HTTPS.Text)) comboBox_HTTPS.Items.Add(textBox_HTTPS.Text); } private void button_RTSP_Add_Click(object sender, RoutedEventArgs e) { if (!comboBox_RTSP.Items.Contains(textBox_RTSP.Text)) comboBox_RTSP.Items.Add(textBox_RTSP.Text); } private void button_HTTP_Delete_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < comboBox_HTTP.Items.Count; i++) { if (comboBox_HTTP.Items[i].ToString() == textBox_HTTP.Text) comboBox_HTTP.Items.Remove(comboBox_HTTP.Items[i]); } } private void button_HTTPS_Delete_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < comboBox_HTTPS.Items.Count; i++) { if (comboBox_HTTPS.Items[i].ToString() == textBox_HTTPS.Text) comboBox_HTTPS.Items.Remove(comboBox_HTTPS.Items[i]); } } private void button_RTSP_Delete_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < comboBox_RTSP.Items.Count; i++) { if (comboBox_RTSP.Items[i].ToString() == textBox_RTSP.Text) comboBox_RTSP.Items.Remove(comboBox_RTSP.Items[i]); } } private void radioButton_Manual_CheckedChanged(object sender, DependencyPropertyChangedEventArgs e) { groupBox_Basic.IsEnabled = true; groupBox_Ports.IsEnabled = true; } private void radioButton_DHCP_CheckedChanged(object sender, DependencyPropertyChangedEventArgs e) { groupBox_Basic.IsEnabled = false; groupBox_Ports.IsEnabled = false; } private void InvokeGuiThread(Action action) { Dispatcher.BeginInvoke(action); } } }
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. The network settings are not supported in all ipcamera devices.
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="ConfigureOnvifCameraRemotely07Wpf.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 Reboot" Height="756" Width="355" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen"> <Grid> <GroupBox Header="Live camera" HorizontalAlignment="Left" Margin="10,51,0,0" VerticalAlignment="Top" Height="226" Width="329"> <Grid HorizontalAlignment="Left" Height="204" VerticalAlignment="Top" Width="296"> <controls:VideoViewerWPF Name="videoViewer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black" Margin="0,0,-20,0"/> </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 x:Name="groupBox_Network" Header="Network settings" HorizontalAlignment="Left" Margin="14,282,0,0" VerticalAlignment="Top" Height="436" Width="325" IsEnabled="False"> <Grid HorizontalAlignment="Left" Height="415" VerticalAlignment="Top" Width="315" Margin="0,0,-2,-1"> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="5*"/> <RowDefinition Height="4*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <RadioButton x:Name="radioButton_DHCP" Content="Use DHCP" HorizontalAlignment="Left" VerticalAlignment="Center" IsChecked="True" IsEnabledChanged="radioButton_DHCP_CheckedChanged"/> <RadioButton x:Name="radioButton_Manual" Content="Manual network settings" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Center" IsEnabledChanged="radioButton_Manual_CheckedChanged"/> <GroupBox x:Name="groupBox_Basic" Header="Basic settings" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="2" VerticalAlignment="Top" Height="173" Width="295" Grid.RowSpan="2"> <Grid HorizontalAlignment="Left" Height="150" VerticalAlignment="Top" Width="285" Margin="0,0,-2,0"> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> <Label Content="IP address" HorizontalAlignment="Left" VerticalAlignment="Center"/> <Label Content="Host name" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1"/> <Label Content="Subnet mask" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="2"/> <Label Content="Gateway" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="3"/> <Label Content="DNS" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="4"/> <Label Content="NTP server" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="5"/> <TextBox x:Name="textBox_IP" Grid.Column="1" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="163"/> <TextBox x:Name="textBox_Host" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="163"/> <TextBox x:Name="textBox_Netmask" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="163"/> <TextBox x:Name="textBox_Gateway" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="163"/> <TextBox x:Name="textBox_DNS" Grid.Column="1" Grid.Row="4" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="163"/> <TextBox x:Name="textBox_NTP_IP" Grid.Column="1" Grid.Row="5" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="163" IsEnabled="False"/> </Grid> </GroupBox> <GroupBox x:Name="groupBox_Ports" Header="Port settings" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="3" VerticalAlignment="Top" Height="127" Width="295"> <Grid HorizontalAlignment="Left" Height="108" VerticalAlignment="Top" Width="285" Margin="0,0,-2,-3"> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="1.7*"/> <ColumnDefinition Width="1.3*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <Label Content="HTTP ports" HorizontalAlignment="Left" VerticalAlignment="Center"/> <Label Content="HTTPS ports" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1"/> <Label Content="RTSP ports" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="2"/> <ComboBox x:Name="comboBox_HTTP" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="56"/> <ComboBox x:Name="comboBox_HTTPS" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="56"/> <ComboBox x:Name="comboBox_RTSP" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="56"/> <TextBox x:Name="textBox_HTTP" Grid.Column="2" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="44"/> <TextBox x:Name="textBox_HTTPS" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="44"/> <TextBox x:Name="textBox_RTSP" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" VerticalAlignment="Center" Width="44"/> <Button Content="Add" Grid.Column="3" HorizontalAlignment="Left" VerticalAlignment="Center" Width="44" Click="button_HTTP_Add_Click"/> <Button Content="Add" Grid.Column="3" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="44" Click="button_HTTPS_Add_Click"/> <Button Content="Add" Grid.Column="3" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="44" Click="button_RTSP_Add_Click"/> <Button Content="Delete" Grid.Column="4" HorizontalAlignment="Left" VerticalAlignment="Center" Width="44" Click="button_HTTP_Delete_Click"/> <Button Content="Delete" Grid.Column="4" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="44" Click="button_HTTPS_Delete_Click"/> <Button Content="Delete" Grid.Column="4" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="44" Click="button_RTSP_Delete_Click"/> </Grid> </GroupBox> <Button Content="Apply" HorizontalAlignment="Center" Grid.Row="4" VerticalAlignment="Center" Width="75" Click="button_Apply_Click"/> </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.
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#