Ozeki Camera Recorder Stat

How to setup the white balance in C#

In the following guide you will find detailed information on how to adjust the white balance preferences of your IP camera device. 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 adjust the white balance preferences of an IP camera device using C#?

Windows Form WPF  

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 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: http://www.camera-sdk.com/p_13-download-camera-onvif.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Camera_Viewer_White_Balance_WF\Camera_Viewer_White_Balance_WF.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:

You can discover the features within the methods that belong to the trackbar GUI elements (TrackBar_CbGain() && TrackBar_CrGain()). In the body of these methods you can see the statements that can be used to set the possible properties of the white balance feature manually: in order to do this you need a newCameraImaging object that is going to be set to the camera with the new values by SetAttributes() method. Finally, you have to refresh the camera's image settings by the RefreshProperties() method. This statement is in the Camera_CameraStateChanged method: we set the value of the variable as soon as the streaming has begun. The invoked statements can be used to set the correct values of the GUI elements like the initial values of the trackbars and the values inside the labels each belonging to it's associated trackbar.

Implement image settings of an IP camera in C#

Form1.cs

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

namespace VideoCameraViewer11
{
    public partial class Form1 : Form
    {
        private IIPCamera _camera;
        private DrawingImageProvider _imageProvider;
        private MediaConnector _connector;
        private VideoViewerWF _videoViewerWf;

        public Form1()
        {
            InitializeComponent();
            _imageProvider = new DrawingImageProvider();
            _connector = new MediaConnector();
            _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(14, 19);
            _videoViewerWf.Name = "_videoViewerWf";
        }

        // Connecting camera's video channel to the image provider and starting it
        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();
        }

        //Setting the initial value of the NumericUpDown GUI object
        private void Camera_CameraStateChanged(object sender, CameraStateEventArgs e)
	        {
	            switch (e.State)
	            {
	                case CameraState.Streaming:
	                    _camera.ImagingSettings.WhiteBalance.ManualMode = true;
	
	                    InvokeGui(() =>
	                    {
	                        Label_CbGain.Text = "CbGain: " + _camera.ImagingSettings.WhiteBalance.CbGain;
	                        Label_CrGain.Text = "CrGain: " + _camera.ImagingSettings.WhiteBalance.CrGain;
                            if (_camera.ImagingSettings.WhiteBalanceYbGainInterval != null)
                            {
                                TrackBar_CbGain.Minimum = (int)_camera.ImagingSettings.WhiteBalanceYbGainInterval.Min;
                                TrackBar_CbGain.Maximum = (int)_camera.ImagingSettings.WhiteBalanceYbGainInterval.Max;
                                TrackBar_CbGain.Value = (int)_camera.ImagingSettings.WhiteBalance.CbGain;
                            }
                            if (_camera.ImagingSettings.WhiteBalanceYrGainInterval != null)
                            {
                                TrackBar_CrGain.Minimum = (int)_camera.ImagingSettings.WhiteBalanceYrGainInterval.Min;
                                TrackBar_CrGain.Maximum = (int)_camera.ImagingSettings.WhiteBalanceYrGainInterval.Max;
                                TrackBar_CrGain.Value = (int)_camera.ImagingSettings.WhiteBalance.CrGain;
                            }
	                    });
	                    break;
	            }
	        }

        private void TrackBarCbGain_Scroll(object sender, EventArgs e)
        {
            if (_camera != null)
            {
                var imagingSettings = new CameraImaging { WhiteBalance = { CbGain = TrackBar_CbGain.Value } };
                _camera.ImagingSettings.SetAttributes(imagingSettings);
                _camera.ImagingSettings.RefreshProperties();
                InvokeGui(() => Label_CbGain.Text = "CbGain: " + TrackBar_CbGain.Value);
            }
        }

        private void TrackBarCrGain_Scroll(object sender, EventArgs e)
	        {
             if (_camera != null) { 
	            var imagingSettings = new CameraImaging {WhiteBalance = {CrGain = TrackBar_CrGain.Value}};
	            _camera.ImagingSettings.SetAttributes(imagingSettings);
	            _camera.ImagingSettings.RefreshProperties();
	            InvokeGui(() =>  Label_CrGain.Text = "CbGain: " + TrackBar_CrGain.Value );
	        }}

        private void InvokeGui(Action action)
        {
            BeginInvoke(action);
        }
    }
}
	

Code 1 - Implement image settings of an IP camera 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

The Graphical User Interface of an application for adjusting white balance in C#
Figure 1 - The graphical user interface of your application

Please note that not all of the IP camera devices support the adjustment of white balance features.

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 VideoCameraViewer11
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>

        #endregion

        private void InitializeComponent()
        {
            this.GroupBox_Connect = new System.Windows.Forms.GroupBox();
            this.button_Connect = new System.Windows.Forms.Button();
            this.CameraBox = new System.Windows.Forms.GroupBox();
            this.GroupBox_Image = new System.Windows.Forms.GroupBox();
            this.Label_CrGain = new System.Windows.Forms.Label();
            this.Label_CbGain = new System.Windows.Forms.Label();
            this.TrackBar_CrGain = new System.Windows.Forms.TrackBar();
            this.TrackBar_CbGain = new System.Windows.Forms.TrackBar();
            this.GroupBox_Connect.SuspendLayout();
            this.GroupBox_Image.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBar_CrGain)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBar_CbGain)).BeginInit();
            this.SuspendLayout();
            // 
            // GroupBox_Connect
            // 
            this.GroupBox_Connect.Controls.Add(this.button_Connect);
            this.GroupBox_Connect.Location = new System.Drawing.Point(10, 10);
            this.GroupBox_Connect.Name = "GroupBox_Connect";
            this.GroupBox_Connect.Size = new System.Drawing.Size(100, 60);
            this.GroupBox_Connect.TabIndex = 0;
            this.GroupBox_Connect.TabStop = false;
            this.GroupBox_Connect.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(79, 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, 85);
            this.CameraBox.Name = "CameraBox";
            this.CameraBox.Size = new System.Drawing.Size(285, 210);
            this.CameraBox.TabIndex = 3;
            this.CameraBox.TabStop = false;
            this.CameraBox.Text = "Live camera ";
            // 
            // GroupBox_Image
            // 
            this.GroupBox_Image.Controls.Add(this.Label_CrGain);
            this.GroupBox_Image.Controls.Add(this.Label_CbGain);
            this.GroupBox_Image.Controls.Add(this.TrackBar_CrGain);
            this.GroupBox_Image.Controls.Add(this.TrackBar_CbGain);
            this.GroupBox_Image.Location = new System.Drawing.Point(10, 300);
            this.GroupBox_Image.Name = "GroupBox_Image";
            this.GroupBox_Image.Size = new System.Drawing.Size(285, 105);
            this.GroupBox_Image.TabIndex = 7;
            this.GroupBox_Image.TabStop = false;
            this.GroupBox_Image.Text = "Image adjustment";
            // 
            // Label_CrGain
            // 
            this.Label_CrGain.AutoSize = true;
            this.Label_CrGain.Location = new System.Drawing.Point(165, 35);
            this.Label_CrGain.Name = "Label_CrGain";
            this.Label_CrGain.Size = new System.Drawing.Size(39, 13);
            this.Label_CrGain.TabIndex = 3;
            this.Label_CrGain.Text = "CrGain";
            // 
            // Label_CbGain
            // 
            this.Label_CbGain.AutoSize = true;
            this.Label_CbGain.Location = new System.Drawing.Point(20, 35);
            this.Label_CbGain.Name = "Label_CbGain";
            this.Label_CbGain.Size = new System.Drawing.Size(42, 13);
            this.Label_CbGain.TabIndex = 2;
            this.Label_CbGain.Text = "CbGain";
            // 
            // TrackBar_CrGain
            // 
            this.TrackBar_CrGain.Location = new System.Drawing.Point(150, 55);
            this.TrackBar_CrGain.Maximum = 700;
            this.TrackBar_CrGain.Minimum = 400;
            this.TrackBar_CrGain.Name = "TrackBar_CrGain";
            this.TrackBar_CrGain.Size = new System.Drawing.Size(130, 45);
            this.TrackBar_CrGain.TabIndex = 1;
            this.TrackBar_CrGain.Value = 400;
            this.TrackBar_CrGain.Scroll += new System.EventHandler(this.TrackBarCrGain_Scroll);
            // 
            // TrackBar_CbGain
            // 
            this.TrackBar_CbGain.Location = new System.Drawing.Point(5, 55);
            this.TrackBar_CbGain.Maximum = 700;
            this.TrackBar_CbGain.Minimum = 400;
            this.TrackBar_CbGain.Name = "TrackBar_CbGain";
            this.TrackBar_CbGain.Size = new System.Drawing.Size(130, 45);
            this.TrackBar_CbGain.TabIndex = 0;
            this.TrackBar_CbGain.Value = 400;
            this.TrackBar_CbGain.Scroll += new System.EventHandler(this.TrackBarCbGain_Scroll);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(304, 419);
            this.Controls.Add(this.GroupBox_Image);
            this.Controls.Add(this.CameraBox);
            this.Controls.Add(this.GroupBox_Connect);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "IP Camera White Balance Adjustment";
            this.GroupBox_Connect.ResumeLayout(false);
            this.GroupBox_Image.ResumeLayout(false);
            this.GroupBox_Image.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBar_CrGain)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBar_CbGain)).EndInit();
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.GroupBox GroupBox_Connect;
        private System.Windows.Forms.Button button_Connect;
        private System.Windows.Forms.GroupBox CameraBox;
        private System.Windows.Forms.GroupBox GroupBox_Image;
        private System.Windows.Forms.TrackBar TrackBar_CrGain;
        private System.Windows.Forms.TrackBar TrackBar_CbGain;
        private System.Windows.Forms.Label Label_CrGain;
        private System.Windows.Forms.Label Label_CbGain;
    }
}
	

Code 2 - GUI example in C#

DISCLAIMER: Please note that the following features will only work if your IP camera supports the given function. You should check the user manual of your IP camera to make sure it supports the feature that you wish to implement in C#.

Related Pages

FAQ

Below you can find the answers for the most frequently asked questions related to this topic:

  1. How can I get the URL of the camera?

    You can get the URL from the producer of the camera. (In the 10th tutorial you can find information on how to create an own IP camera discoverer program.)

  2. I have not managed to build the solution. How to solve it?

    • Please set the Target framework property of the project to .NET 4.0.
    • You should add the OzekiSDK.dll and the System.Drawing.dll to the references of the solution.
    • Please import the missing classes.
  3. I cannot set the white balance. The scrollbars move but nothing happens. Why?

    Some cameras do not support the modification of the white balance.

  4. Why does the white balance change, when I did not order it?

    It is possible that someone else is also using the camera.