Ozeki Camera Recorder Stat

How to change the resolution of the video stream in C#

In this guide you will find detailed information on how to change the video stream resolution of your IP camera, Web Camera or video file. 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 change the video resolution from an IP camera, Web Camera or video file using C#?

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

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


Method:

  • SetResolution() : by calling this method, you can set two integer value which represent the width and height property of the video stream.

How to change the resolutoin of the video stream in C#

MainForm.cs

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

namespace Camera_Viewer_Connect_USB_WF
{
    public partial class MainForm : Form
    {
        private OzekiCamera _camera;
        private DrawingImageProvider _imageProvider;
        private MediaConnector _mediaConnector;
        private CameraURLBuilderWF _myCameraUrlBuilder;
        private VideoResizer _resizeHandler;


        public MainForm()
        {
            InitializeComponent();
            _imageProvider = new DrawingImageProvider();
            _mediaConnector = new MediaConnector();
            _resizeHandler = new VideoResizer();
            
            _myCameraUrlBuilder = new CameraURLBuilderWF();


            videoViewerWF1.SetImageProvider(_imageProvider);
        }

        private void btn_Connect_Click(object sender, EventArgs e)
        {
            if (_camera != null)
            {
                videoViewerWF1.Stop();
                _camera.Stop();
                _mediaConnector.Disconnect(_camera.VideoChannel, _resizeHandler);
                _mediaConnector.Disconnect(_resizeHandler, _imageProvider);
                _camera.Dispose();
            }
            btn_Connect.Enabled = false;

            _camera = new OzekiCamera(_myCameraUrlBuilder.CameraURL);

            _camera.CameraStateChanged += _webCamera_CameraStateChanged;

            _mediaConnector.Connect(_camera.VideoChannel, _resizeHandler);
            _mediaConnector.Connect(_resizeHandler, _imageProvider);

            _camera.Start();

            videoViewerWF1.Start();
        }

        void _webCamera_CameraStateChanged(object sender, CameraStateEventArgs e)
        {
            InvokeGuiThread(() =>
            {
                switch (e.State)
                {
                    case CameraState.Streaming:
                        btn_Disconnect.Enabled = true;
                        break;
                    case CameraState.Disconnected:
                        btn_Disconnect.Enabled = false;
                        btn_Connect.Enabled = true;
                        break;
                }
            });
        }
        
        private void btn_Disconnect_Click(object sender, EventArgs e)
        {
            videoViewerWF1.Stop();
            _camera.Stop();
            _mediaConnector.Disconnect(_camera.VideoChannel, _imageProvider);
            
        }

        private void btn_Compose_Click(object sender, EventArgs e)
        {
            var result = _myCameraUrlBuilder.ShowDialog();

            if(result != DialogResult.OK)
                return;

            cameraUrlBox.Text = _myCameraUrlBuilder.CameraURL;

            btn_Connect.Enabled = true;
        }

        private void btn_resize_Click(object sender, EventArgs e)
        {
            try
            {

                if (!String.IsNullOrEmpty(resizeWidthBox.Text) && (!String.IsNullOrEmpty(resizeHeightBox.Text)))
                    _resizeHandler.SetResolution(Int32.Parse(this.resizeWidthBox.Text), Int32.Parse(this.resizeHeightBox.Text));
                else
                    MessageBox.Show("The height or width field is blank!");

            }
            catch (FormatException ex)
            {
                MessageBox.Show("The height or width value is not valid!");
            }
        }

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


    }

Code 1 - Change the resolution of the video stream in C#

Application window

How to change the resolutoin of the video stream in C# How to change the resolutoin of the video stream in C#
Figure 1 - The application window

MainForm.Designer.cs

namespace Camera_Viewer_Connect_USB_WF
{
    partial class MainForm
    {
        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.components = new System.ComponentModel.Container();
            this.btn_Compose = new System.Windows.Forms.Button();
            this.videoViewerWF1 = new Ozeki.Media.VideoViewerWF();
            this.connectGroupBox = new System.Windows.Forms.GroupBox();
            this.btn_Disconnect = new System.Windows.Forms.Button();
            this.btn_Connect = new System.Windows.Forms.Button();
            this.cameraUrlBox = new System.Windows.Forms.TextBox();
            this.urlLabel = new System.Windows.Forms.Label();
            this.bnt_resize = new System.Windows.Forms.Button();
            this.resizeGroupBox = new System.Windows.Forms.GroupBox();
            this.widtLabel = new System.Windows.Forms.Label();
            this.resizeHeightBox = new System.Windows.Forms.TextBox();
            this.resizeWidthBox = new System.Windows.Forms.TextBox();
            this.heightLabel = new System.Windows.Forms.Label();
            this.connectGroupBox.SuspendLayout();
            this.resizeGroupBox.SuspendLayout();
            this.SuspendLayout();
            // 
            // btn_Compose
            // 
            this.btn_Compose.Location = new System.Drawing.Point(238, 15);
            this.btn_Compose.Name = "btn_Compose";
            this.btn_Compose.Size = new System.Drawing.Size(75, 23);
            this.btn_Compose.TabIndex = 1;
            this.btn_Compose.Text = "Compose";
            this.btn_Compose.UseVisualStyleBackColor = true;
            this.btn_Compose.Click += new System.EventHandler(this.btn_Compose_Click);
            // 
            // videoViewerWF1
            // 
            this.videoViewerWF1.BackColor = System.Drawing.Color.Black;
            this.videoViewerWF1.FlipMode = Ozeki.Media.FlipMode.None;
            this.videoViewerWF1.FrameStretch = Ozeki.Media.FrameStretch.Uniform;
            this.videoViewerWF1.FullScreenEnabled = true;
            this.videoViewerWF1.Location = new System.Drawing.Point(12, 100);
            this.videoViewerWF1.Name = "videoViewerWF1";
            this.videoViewerWF1.RotateAngle = 0;
            this.videoViewerWF1.Size = new System.Drawing.Size(320, 240);
            this.videoViewerWF1.TabIndex = 2;
            this.videoViewerWF1.Text = "videoViewerWF1";
            // 
            // connectGroupBox
            // 
            this.connectGroupBox.Controls.Add(this.btn_Disconnect);
            this.connectGroupBox.Controls.Add(this.btn_Connect);
            this.connectGroupBox.Controls.Add(this.cameraUrlBox);
            this.connectGroupBox.Controls.Add(this.urlLabel);
            this.connectGroupBox.Controls.Add(this.btn_Compose);
            this.connectGroupBox.Location = new System.Drawing.Point(12, 12);
            this.connectGroupBox.Name = "connectGroupBox";
            this.connectGroupBox.Size = new System.Drawing.Size(320, 82);
            this.connectGroupBox.TabIndex = 3;
            this.connectGroupBox.TabStop = false;
            this.connectGroupBox.Text = "Connect";
            // 
            // btn_Disconnect
            // 
            this.btn_Disconnect.Enabled = false;
            this.btn_Disconnect.Location = new System.Drawing.Point(157, 43);
            this.btn_Disconnect.Name = "btn_Disconnect";
            this.btn_Disconnect.Size = new System.Drawing.Size(75, 23);
            this.btn_Disconnect.TabIndex = 5;
            this.btn_Disconnect.Text = "Disconnect";
            this.btn_Disconnect.UseVisualStyleBackColor = true;
            this.btn_Disconnect.Click += new System.EventHandler(this.btn_Disconnect_Click);
            // 
            // btn_Connect
            // 
            this.btn_Connect.Enabled = false;
            this.btn_Connect.Location = new System.Drawing.Point(73, 43);
            this.btn_Connect.Name = "btn_Connect";
            this.btn_Connect.Size = new System.Drawing.Size(75, 23);
            this.btn_Connect.TabIndex = 4;
            this.btn_Connect.Text = "Connect";
            this.btn_Connect.UseVisualStyleBackColor = true;
            this.btn_Connect.Click += new System.EventHandler(this.btn_Connect_Click);
            // 
            // cameraUrlBox
            // 
            this.cameraUrlBox.Location = new System.Drawing.Point(73, 17);
            this.cameraUrlBox.Name = "cameraUrlBox";
            this.cameraUrlBox.Size = new System.Drawing.Size(159, 20);
            this.cameraUrlBox.TabIndex = 3;
            // 
            // urlLabel
            // 
            this.urlLabel.AutoSize = true;
            this.urlLabel.Location = new System.Drawing.Point(5, 20);
            this.urlLabel.Name = "urlLabel";
            this.urlLabel.Size = new System.Drawing.Size(71, 13);
            this.urlLabel.TabIndex = 2;
            this.urlLabel.Text = "Camera URL:";
            // 
            // bnt_resize
            // 
            this.bnt_resize.Location = new System.Drawing.Point(8, 33);
            this.bnt_resize.Name = "bnt_resize";
            this.bnt_resize.Size = new System.Drawing.Size(75, 23);
            this.bnt_resize.TabIndex = 4;
            this.bnt_resize.Text = "Resize";
            this.bnt_resize.UseVisualStyleBackColor = true;
            this.bnt_resize.Click += new System.EventHandler(this.btn_resize_Click);
            // 
            // resizeGroupBox
            // 
            this.resizeGroupBox.Controls.Add(this.heightLabel);
            this.resizeGroupBox.Controls.Add(this.widtLabel);
            this.resizeGroupBox.Controls.Add(this.resizeHeightBox);
            this.resizeGroupBox.Controls.Add(this.resizeWidthBox);
            this.resizeGroupBox.Controls.Add(this.bnt_resize);
            this.resizeGroupBox.Location = new System.Drawing.Point(12, 346);
            this.resizeGroupBox.Name = "resizeGroupBox";
            this.resizeGroupBox.Size = new System.Drawing.Size(320, 65);
            this.resizeGroupBox.TabIndex = 5;
            this.resizeGroupBox.TabStop = false;
            this.resizeGroupBox.Text = "Resize";
            // 
            // widtLabel
            // 
            this.widtLabel.AutoSize = true;
            this.widtLabel.Location = new System.Drawing.Point(136, 20);
            this.widtLabel.Name = "widtLabel";
            this.widtLabel.Size = new System.Drawing.Size(35, 13);
            this.widtLabel.TabIndex = 7;
            this.widtLabel.Text = "Width";
            // 
            // resize_height_box
            // 
            this.resizeHeightBox.Location = new System.Drawing.Point(206, 35);
            this.resizeHeightBox.Name = "resizeHeightBox";
            this.resizeHeightBox.Size = new System.Drawing.Size(64, 20);
            this.resizeHeightBox.TabIndex = 6;
            // 
            // resize_width_box
            // 
            this.resizeWidthBox.Location = new System.Drawing.Point(121, 36);
            this.resizeWidthBox.Name = "resizeWidthBox";
            this.resizeWidthBox.Size = new System.Drawing.Size(64, 20);
            this.resizeWidthBox.TabIndex = 5;
            // 
            // heightLabel
            // 
            this.heightLabel.AutoSize = true;
            this.heightLabel.Location = new System.Drawing.Point(219, 19);
            this.heightLabel.Name = "heightLabel";
            this.heightLabel.Size = new System.Drawing.Size(38, 13);
            this.heightLabel.TabIndex = 8;
            this.heightLabel.Text = "Height";
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(348, 414);
            this.Controls.Add(this.resizeGroupBox);
            this.Controls.Add(this.connectGroupBox);
            this.Controls.Add(this.videoViewerWF1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "USB Camera\'s Live Image";
            this.connectGroupBox.ResumeLayout(false);
            this.connectGroupBox.PerformLayout();
            this.resizeGroupBox.ResumeLayout(false);
            this.resizeGroupBox.PerformLayout();
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.Button btn_Compose;
        private Ozeki.Media.VideoViewerWF videoViewerWF1;
        private System.Windows.Forms.GroupBox connectGroupBox;
        private System.Windows.Forms.TextBox cameraUrlBox;
        private System.Windows.Forms.Label urlLabel;
        private System.Windows.Forms.Button btn_Disconnect;
        private System.Windows.Forms.Button btn_Connect;
        private System.Windows.Forms.Button bnt_resize;
        private System.Windows.Forms.GroupBox resizeGroupBox;
        private System.Windows.Forms.Label widtLabel;
        private System.Windows.Forms.TextBox resizeHeightBox;
        private System.Windows.Forms.TextBox resizeWidthBox;
        private System.Windows.Forms.Label heightLabel;
    }
}

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 to the references of the solution.
    • Please import the missing classes.
  3. Which OS are supported? Does it work with Windows 10?

    Yes,the SDK works with Windows 10. The following Operating Systems are supported:.

    • Microsoft Windows XP
    • Microsoft Windows Vista
    • Microsoft Windows 7
    • Microsoft Windows 8
    • Microsoft Windows 10
    • Microsoft Windows Server 2003
    • Microsoft Windows Server 2008