Ozeki Camera Recorder Stat

How to setup the Brightness / Saturation / Contrast values of the camera image in C#

In this guide you can find detailed information on how to set preferences like brightness, contrast and saturation of your IP camera. To implement this example, you need to have OZEKI Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project.

How to set the preferences (brightness, contrast, saturation) 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_Brightness_WF\Camera_Viewer_Brightness_WF.sln

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

To find more about the previously used functions included here as well, please, visit this tutorial

The additional statements and methods of this example are the following:

TrackBarBrightness_Scroll(),
TrackBarContrast_Scroll(),
TrackBarSaturation_Scroll()
These three new methods provide the additional functionality of this example. These methods are listening to their own scroll bar GUI elements and whenever the scroll bars are being modified the appropiate property of the camera image is also changing.

_camera.CameraStateChanged += Camera_CameraStateChanged;
InvokeGUI()
These methods and event serve the functionality to load in and display on the GUI elements the IP camera's current preferences as soon as the streaming process has been started. This is a really short and elegant solution for C# cross-thread operations. The next example will demonstrate this technique as well.

Implement image settings of an IP camera in C#

Form1.cs

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

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

        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(10, 20);
            _videoViewerWf.Name = "_videoViewerWf";
        }

        // Connecting the 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 values of the scroll bars
        void Camera_CameraStateChanged(object sender, CameraStateEventArgs e)
        {
            
                switch (e.State)
                {
                    case CameraState.Streaming:
                        InvokeGUI(() =>
                        {
                            if (_camera.ImagingSettings != null)
                            {
                                if(_camera.ImagingSettings.BrightnessInterval != null)
                                {
                                    TrackBarBrightness.Minimum = (int)_camera.ImagingSettings.BrightnessInterval.Min;
                                    TrackBarBrightness.Maximum = (int)_camera.ImagingSettings.BrightnessInterval.Max;
                                    TrackBarBrightness.Value = (int)_camera.ImagingSettings.Brightness;
                                }

                                if (_camera.ImagingSettings.ContrastInterval != null)
                                {
                                    TrackBarContrast.Minimum = (int)_camera.ImagingSettings.ContrastInterval.Min;
                                    TrackBarContrast.Maximum = (int)_camera.ImagingSettings.ContrastInterval.Max;
                                    TrackBarContrast.Value = (int)_camera.ImagingSettings.Contrast;
                                }

                                if (_camera.ImagingSettings.ColorSaturationInterval != null)
                                {
                                    TrackBarSaturation.Minimum = (int)_camera.ImagingSettings.ColorSaturationInterval.Min;
                                    TrackBarSaturation.Maximum = (int)_camera.ImagingSettings.ColorSaturationInterval.Max;
                                    TrackBarSaturation.Value = (int)_camera.ImagingSettings.ColorSaturation;
                                }

                                BrightnessLabel.Text = _camera.ImagingSettings.Brightness.ToString(CultureInfo.InvariantCulture);
                                ContrastLabel.Text = _camera.ImagingSettings.Contrast.ToString(CultureInfo.InvariantCulture);
                                SaturationLabel.Text = _camera.ImagingSettings.ColorSaturation.ToString(CultureInfo.InvariantCulture);
                            }
                        });

                        break;
                }
            
        }

        // Setting the camera's image
        private void TrackBarBrightness_Scroll(object sender, EventArgs e)
        {
            if (_camera != null)
            {
                _camera.ImagingSettings.SetAttributes(new CameraImaging { Brightness = TrackBarBrightness.Value });
                _camera.ImagingSettings.RefreshProperties();
                BrightnessLabel.Text = _camera.ImagingSettings.Brightness.ToString(CultureInfo.InvariantCulture);
            }
        }

        private void TrackBarContrast_Scroll(object sender, EventArgs e)
        {
            if (_camera != null)
            {
            _camera.ImagingSettings.SetAttributes(new CameraImaging { Contrast = TrackBarContrast.Value });
            _camera.ImagingSettings.RefreshProperties();
            ContrastLabel.Text = _camera.ImagingSettings.Contrast.ToString(CultureInfo.InvariantCulture);
        }}

        private void TrackBarSaturation_Scroll(object sender, EventArgs e)
        {
            if (_camera != null)
            {
            _camera.ImagingSettings.SetAttributes(new CameraImaging { ColorSaturation = TrackBarSaturation.Value });
            _camera.ImagingSettings.RefreshProperties();
            SaturationLabel.Text = _camera.ImagingSettings.ColorSaturation.ToString(CultureInfo.InvariantCulture);
        }}

        void InvokeGUI(Action a)
        {
            BeginInvoke(a);
        }
    }
}
	

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 the application for adjusting brightness, saturation and contrast in C#
Figure 1 - The graphical user interface of your application

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

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

        #region Windows Form Designer generated code

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

        #endregion

        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.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.SaturationLabel = new System.Windows.Forms.Label();
            this.ContrastLabel = new System.Windows.Forms.Label();
            this.BrightnessLabel = new System.Windows.Forms.Label();
            this.TrackBarSaturation = new System.Windows.Forms.TrackBar();
            this.TrackBarContrast = new System.Windows.Forms.TrackBar();
            this.TrackBarBrightness = new System.Windows.Forms.TrackBar();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBarSaturation)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBarContrast)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBarBrightness)).BeginInit();
            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(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, 85);
            this.CameraBox.Name = "CameraBox";
            this.CameraBox.Size = new System.Drawing.Size(290, 210);
            this.CameraBox.TabIndex = 3;
            this.CameraBox.TabStop = false;
            this.CameraBox.Text = "Live camera ";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(10, 35);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(56, 13);
            this.label1.TabIndex = 4;
            this.label1.Text = "Brightness";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(10, 85);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(46, 13);
            this.label2.TabIndex = 5;
            this.label2.Text = "Contrast";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(10, 135);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(55, 13);
            this.label3.TabIndex = 6;
            this.label3.Text = "Saturation";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.SaturationLabel);
            this.groupBox2.Controls.Add(this.ContrastLabel);
            this.groupBox2.Controls.Add(this.BrightnessLabel);
            this.groupBox2.Controls.Add(this.TrackBarSaturation);
            this.groupBox2.Controls.Add(this.TrackBarContrast);
            this.groupBox2.Controls.Add(this.TrackBarBrightness);
            this.groupBox2.Controls.Add(this.label1);
            this.groupBox2.Controls.Add(this.label3);
            this.groupBox2.Controls.Add(this.label2);
            this.groupBox2.Location = new System.Drawing.Point(10, 300);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(290, 195);
            this.groupBox2.TabIndex = 7;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Image adjustment";
            // 
            // SaturationLabel
            // 
            this.SaturationLabel.AutoSize = true;
            this.SaturationLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.SaturationLabel.Location = new System.Drawing.Point(255, 135);
            this.SaturationLabel.Name = "SaturationLabel";
            this.SaturationLabel.Size = new System.Drawing.Size(2, 15);
            this.SaturationLabel.TabIndex = 12;
            // 
            // ContrastLabel
            // 
            this.ContrastLabel.AutoSize = true;
            this.ContrastLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.ContrastLabel.Location = new System.Drawing.Point(255, 85);
            this.ContrastLabel.Name = "ContrastLabel";
            this.ContrastLabel.Size = new System.Drawing.Size(2, 15);
            this.ContrastLabel.TabIndex = 11;
            // 
            // BrightnessLabel
            // 
            this.BrightnessLabel.AutoSize = true;
            this.BrightnessLabel.BackColor = System.Drawing.Color.White;
            this.BrightnessLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.BrightnessLabel.Location = new System.Drawing.Point(255, 35);
            this.BrightnessLabel.Name = "BrightnessLabel";
            this.BrightnessLabel.Size = new System.Drawing.Size(2, 15);
            this.BrightnessLabel.TabIndex = 10;
            // 
            // TrackBarSaturation
            // 
            this.TrackBarSaturation.Location = new System.Drawing.Point(100, 130);
            this.TrackBarSaturation.Maximum = 1;
            this.TrackBarSaturation.Name = "TrackBarSaturation";
            this.TrackBarSaturation.Size = new System.Drawing.Size(150, 45);
            this.TrackBarSaturation.TabIndex = 9;
            this.TrackBarSaturation.Scroll += new System.EventHandler(this.TrackBarSaturation_Scroll);
            // 
            // TrackBarContrast
            // 
            this.TrackBarContrast.Location = new System.Drawing.Point(100, 80);
            this.TrackBarContrast.Maximum = 1;
            this.TrackBarContrast.Name = "TrackBarContrast";
            this.TrackBarContrast.Size = new System.Drawing.Size(150, 45);
            this.TrackBarContrast.TabIndex = 8;
            this.TrackBarContrast.Scroll += new System.EventHandler(this.TrackBarContrast_Scroll);
            // 
            // TrackBarBrightness
            // 
            this.TrackBarBrightness.Location = new System.Drawing.Point(100, 30);
            this.TrackBarBrightness.Maximum = 1;
            this.TrackBarBrightness.Name = "TrackBarBrightness";
            this.TrackBarBrightness.Size = new System.Drawing.Size(150, 45);
            this.TrackBarBrightness.TabIndex = 7;
            this.TrackBarBrightness.Scroll += new System.EventHandler(this.TrackBarBrightness_Scroll);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(309, 504);
            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 = "IP Camera\'s Image Setting";
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBarSaturation)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBarContrast)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.TrackBarBrightness)).EndInit();
            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.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.TrackBar TrackBarSaturation;
        private System.Windows.Forms.TrackBar TrackBarContrast;
        private System.Windows.Forms.TrackBar TrackBarBrightness;
        private System.Windows.Forms.Label SaturationLabel;
        private System.Windows.Forms.Label BrightnessLabel;
        private System.Windows.Forms.Label ContrastLabel;
    }
}
	

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 OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.
  3. Why does the brightness/contrast/saturation change when I did not order it?

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

  4. I can see the image but all the numbers are on 0 and I can move the scrollbars but nothing happens.

    In this case your camera does not support the modification of the brightness, contrast or saturation.