Ozeki Camera Recorder Stat

How to implement zoom function in C#

This tutorial will show how to use zoom function with your Ozeki Camera SDK in C#. Zooming is a convinient way of magnifiing a portion of the displayed image without moving the camera closer to the given area. To start, OZEKI Camera SDK has to be installed and a reference to OzekiSDK.dll has to be added to your Visual Studio project.

Important: you should study this article in order to find out how to set up 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:\Users\User\Documents\Ozeki\Ozeki SDK\Examples\Camera\05_Advanced\07_Zoom

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

Corporate use of zoom function

In graphical user interfaces zoom means to make the picture appear larger. Zoom in if you would like to grow an image, and zoom out to shrink it to its original size. After zooming in, you may decide which part of the picture you are interested in. Use the control keys to go up and down or any direction (e.g. left, up and left, right and down, etc.) to adjust the display to your needs. You can zoom out to get the whole picture.

For example, you would like to make sure that all the people staying in the sea are safe. You recognize that there are some swimmers far from the beach. By using zoom you can get a closer view to see that none of them are screaming for help.

This function is really popular because it helps you to focus on certain parts of the captured image without moving the camera.

Implementation of zoom function in C#

Namespace: Ozeki.Media

Property:

  • IsRunning: If zoom function works the value is true and false if it is not.
  • IsDefault: The value is automatically true when the original picture is seen and false if the zoom is used.
  • Factor: It represents the measure of zoom.

Methods

  • Start: Start initiates the zoom function.
  • Stop: Terminates the zoom function.
  • Default: Resets the camera to the original size.
  • In: This method grows the selected image by one unit.
  • Out: This method shrinks your picture by one unit.
  • ContinuousMove: Used for recording continuous motion. It has two parameters:
    1. Zoom Direction enum: Defines the direction.
    2. Type Int value: Defines the measure of excursion during a period in direction given before.
  • AbsoluteMove: Similar to ContinuousMove but AbsoluteMove jumps determined pixels so the result will not be continuous motion. It has two parameters:
    1. Zoom Direction enum: Defines the direction.
    2. Type Int value: Describes measure of excursion in pixels in direction given before.
  • StopMovement: By using this method ContinuousMove stops.

C# code example for zooming

MainForm.cs

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


namespace Zoom_WF2
{
    public partial class MainForm : Form
    {
        private OzekiCamera _camera;
        private DrawingImageProvider _Provider;
        private MediaConnector _connector;
        private CameraURLBuilderWF _myCameraUrlBuilder;
        private Zoom zoom;

        public MainForm()
        {
            InitializeComponent();
            _connector = new MediaConnector();
            _Provider = new DrawingImageProvider();
            videoViewerWF1.SetImageProvider(_Provider);
            _myCameraUrlBuilder = new CameraURLBuilderWF();
            zoom = new Zoom();
        }

        private void btn_compose_Click(object sender, EventArgs e)
        {
            var result = _myCameraUrlBuilder.ShowDialog();
            if (result != DialogResult.OK) return;
            textbox_camurl.Text = _myCameraUrlBuilder.CameraURL;
            btn_con.Enabled = true;
        }

        private void btn_con_Click(object sender, EventArgs e)
        {
            if (_camera != null)
            {
                _camera.CameraStateChanged -= _camera_CameraStateChanged;
                _camera.Disconnect();
                _connector.Disconnect(_camera.VideoChannel, _Provider);
                _camera.Dispose();
                _camera = null;
            }

            btn_con.Enabled = false;
            _camera = new OzekiCamera(_myCameraUrlBuilder.CameraURL);
            _camera.CameraStateChanged += _camera_CameraStateChanged;
            _connector.Connect(_camera.VideoChannel, zoom);
            _connector.Connect(zoom, _Provider);
            _camera.Start();
            videoViewerWF1.Start();
            zoom.Start();
        }

        private void btn_dcon_Click(object sender, EventArgs e)
        {
            if (_camera == null) return;
            _camera.Disconnect();
            _connector.Disconnect(_camera.VideoChannel, _Provider);
            _camera = null;
        }

        void _camera_CameraStateChanged(object sender, CameraStateEventArgs e)
        {
            InvokeThread(() =>
            {
                if (e.State == CameraState.Streaming)
                    Streaming();
                if (e.State == CameraState.Disconnected)
                    Disconnect();
            });
        }

        private void Disconnect()
        {
            btn_con.Enabled = true;
            btn_dcon.Enabled = false;
        }

        private void Streaming()
        {
            btn_dcon.Enabled = true;
        }

        void InvokeThread(Action action)
        {
            BeginInvoke(action);
        }

        private void btn_zoomIn_Click(object sender, EventArgs e)
        {
            zoom.In();
        }

        private void btn_zoomOut_Click(object sender, EventArgs e)
        {
            zoom.Out();
        }

        private void btn_zoomDefault_Click(object sender, EventArgs e)
        {
            zoom.Default();
        } 

        private void btn_MouseUp(object sender, MouseEventArgs e)
        {
            zoom.StopMovement();
        }

        private void btn_MouseDown(object sender, MouseEventArgs e)
        {
            var button = sender as Button;
            switch(button.Name)
            {
                case "btn_uleft":
                    zoom.ContinouesMove(CameraMovement.UpLeft, 5);
                    break;
                case "btn_left":
                    zoom.ContinouesMove(CameraMovement.Left, 5);
                    break;
                case "btn_bleft":
                    zoom.ContinouesMove(CameraMovement.BottomLeft, 5);
                    break;
                case "btn_bottom":
                    zoom.ContinouesMove(CameraMovement.Bottom, 5);
                    break;
                case "btn_bright":
                    zoom.ContinouesMove(CameraMovement.BottomRight, 5);
                    break;
                case "btn_right":
                    zoom.ContinouesMove(CameraMovement.Right, 5);
                    break;
                case "btn_uright":
                    zoom.ContinouesMove(CameraMovement.UpRight, 5);
                    break;
                case "btn_up":
                    zoom.ContinouesMove(CameraMovement.Up, 5);
                    break;
            }
        }

        private void btn_up_MouseDown(object sender, MouseEventArgs e)
        {
            zoom.ContinouesMove(CameraMovement.Up, 5);
        }

        private void btn_uleft_MouseDown(object sender, MouseEventArgs e)
        {
            zoom.ContinouesMove(CameraMovement.UpLeft, 5);
        }

        private void btn_left_MouseDown(object sender, MouseEventArgs e)
        {
            zoom.ContinouesMove(CameraMovement.Left, 5);
        }

        private void btn_bleft_MouseDown(object sender, MouseEventArgs e)
        {
            zoom.ContinouesMove(CameraMovement.BottomLeft, 5);
        }

        private void btn_bottom_MouseDown(object sender, MouseEventArgs e)
        {
            zoom.ContinouesMove(CameraMovement.Bottom, 5);
        }

        private void btn_bright_MouseDown(object sender, MouseEventArgs e)
        {
            zoom.ContinouesMove(CameraMovement.BottomRight, 5);
        }

        private void btn_right_MouseDown(object sender, MouseEventArgs e)
        {
            zoom.ContinouesMove(CameraMovement.Right, 5);
        }

        private void btn_uright_MouseDown(object sender, MouseEventArgs e)
        {
            zoom.ContinouesMove(CameraMovement.UpRight, 5);
        }
    }
}

Code 1 - Zooming example 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
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.

MainForm.Designer.cs

namespace Zoom_WF2
{
    partial class MainForm
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.lbl_camurl = new System.Windows.Forms.Label();
            this.textbox_camurl = new System.Windows.Forms.TextBox();
            this.btn_compose = new System.Windows.Forms.Button();
            this.btn_con = new System.Windows.Forms.Button();
            this.btn_dcon = new System.Windows.Forms.Button();
            this.videoViewerWF1 = new Ozeki.Media.VideoViewerWF();
            this.btn_zoomplus = new System.Windows.Forms.Button();
            this.btn_zoomdefault = new System.Windows.Forms.Button();
            this.btn_zoommin = new System.Windows.Forms.Button();
            this.btn_uleft = new System.Windows.Forms.Button();
            this.btn_up = new System.Windows.Forms.Button();
            this.btn_left = new System.Windows.Forms.Button();
            this.btn_uright = new System.Windows.Forms.Button();
            this.btn_right = new System.Windows.Forms.Button();
            this.btn_bleft = new System.Windows.Forms.Button();
            this.btn_bottom = new System.Windows.Forms.Button();
            this.btn_bright = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();

            this.lbl_camurl.AutoSize = true;
            this.lbl_camurl.Location = new System.Drawing.Point(12, 16);
            this.lbl_camurl.Name = "lbl_camurl";
            this.lbl_camurl.Size = new System.Drawing.Size(68, 13);
            this.lbl_camurl.TabIndex = 0;
            this.lbl_camurl.Text = "Camera URL";

            this.textbox_camurl.Location = new System.Drawing.Point(97, 14);
            this.textbox_camurl.Name = "textbox_camurl";
            this.textbox_camurl.Size = new System.Drawing.Size(221, 20);
            this.textbox_camurl.TabIndex = 1;

            this.btn_compose.Location = new System.Drawing.Point(340, 10);
            this.btn_compose.Name = "btn_compose";
            this.btn_compose.Size = new System.Drawing.Size(80, 26);
            this.btn_compose.TabIndex = 2;
            this.btn_compose.Text = "Compose";
            this.btn_compose.UseVisualStyleBackColor = true;
            this.btn_compose.Click += new System.EventHandler(this.btn_compose_Click);

            this.btn_con.Enabled = false;
            this.btn_con.Location = new System.Drawing.Point(97, 42);
            this.btn_con.Name = "btn_con";
            this.btn_con.Size = new System.Drawing.Size(73, 26);
            this.btn_con.TabIndex = 4;
            this.btn_con.Text = "Connect";
            this.btn_con.UseVisualStyleBackColor = true;
            this.btn_con.Click += new System.EventHandler(this.btn_con_Click);

            this.btn_dcon.AutoSize = true;
            this.btn_dcon.Enabled = false;
            this.btn_dcon.Location = new System.Drawing.Point(245, 42);
            this.btn_dcon.Name = "btn_dcon";
            this.btn_dcon.Size = new System.Drawing.Size(73, 26);
            this.btn_dcon.TabIndex = 5;
            this.btn_dcon.Text = "Disconnect";
            this.btn_dcon.UseVisualStyleBackColor = true;
            this.btn_dcon.Click += new System.EventHandler(this.btn_dcon_Click);

            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, 87);
            this.videoViewerWF1.Name = "videoViewerWF1";
            this.videoViewerWF1.RotateAngle = 0;
            this.videoViewerWF1.Size = new System.Drawing.Size(408, 306);
            this.videoViewerWF1.TabIndex = 6;
            this.videoViewerWF1.Text = "videoViewerWF1";

            this.btn_zoomplus.Location = new System.Drawing.Point(8, 26);
            this.btn_zoomplus.Name = "btn_zoomplus";
            this.btn_zoomplus.Size = new System.Drawing.Size(73, 44);
            this.btn_zoomplus.TabIndex = 9;
            this.btn_zoomplus.Text = "zoom +";
            this.btn_zoomplus.UseVisualStyleBackColor = true;
            this.btn_zoomplus.Click += new System.EventHandler(this.btn_zoomIn_Click);

            this.btn_zoomdefault.Location = new System.Drawing.Point(105, 169);
            this.btn_zoomdefault.Name = "btn_zoomdefault";
            this.btn_zoomdefault.Size = new System.Drawing.Size(75, 44);
            this.btn_zoomdefault.TabIndex = 10;
            this.btn_zoomdefault.Text = "Default";
            this.btn_zoomdefault.UseVisualStyleBackColor = true;
            this.btn_zoomdefault.Click += new System.EventHandler(this.btn_zoomDefault_Click);

            this.btn_zoommin.Location = new System.Drawing.Point(204, 26);
            this.btn_zoommin.Name = "btn_zoommin";
            this.btn_zoommin.Size = new System.Drawing.Size(75, 44);
            this.btn_zoommin.TabIndex = 11;
            this.btn_zoommin.Text = "zoom -";
            this.btn_zoommin.UseVisualStyleBackColor = true;
            this.btn_zoommin.Click += new System.EventHandler(this.btn_zoomOut_Click);

            this.btn_uleft.Location = new System.Drawing.Point(8, 96);
            this.btn_uleft.Name = "btn_uleft";
            this.btn_uleft.Size = new System.Drawing.Size(75, 44);
            this.btn_uleft.TabIndex = 13;
            this.btn_uleft.Text = "UpLeft";
            this.btn_uleft.UseVisualStyleBackColor = true;
            this.btn_uleft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btn_MouseDown);
            this.btn_uleft.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btn_MouseUp);

            this.btn_up.Location = new System.Drawing.Point(107, 96);
            this.btn_up.Name = "btn_up";
            this.btn_up.Size = new System.Drawing.Size(73, 44);
            this.btn_up.TabIndex = 14;
            this.btn_up.Text = "Up";
            this.btn_up.UseVisualStyleBackColor = true;
            this.btn_up.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btn_MouseDown);
            this.btn_up.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btn_MouseUp);
 
            this.btn_left.Location = new System.Drawing.Point(8, 169);
            this.btn_left.Name = "btn_left";
            this.btn_left.Size = new System.Drawing.Size(75, 44);
            this.btn_left.TabIndex = 15;
            this.btn_left.Text = "Left";
            this.btn_left.UseVisualStyleBackColor = true;
            this.btn_left.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btn_MouseDown);
            this.btn_left.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btn_MouseUp);

            this.btn_uright.Location = new System.Drawing.Point(204, 96);
            this.btn_uright.Name = "btn_uright";
            this.btn_uright.Size = new System.Drawing.Size(75, 44);
            this.btn_uright.TabIndex = 16;
            this.btn_uright.Text = "UpRight";
            this.btn_uright.UseVisualStyleBackColor = true;
            this.btn_uright.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btn_MouseDown);
            this.btn_uright.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btn_MouseUp);

            this.btn_right.Location = new System.Drawing.Point(204, 169);
            this.btn_right.Name = "btn_right";
            this.btn_right.Size = new System.Drawing.Size(75, 44);
            this.btn_right.TabIndex = 18;
            this.btn_right.Text = "Right";
            this.btn_right.UseVisualStyleBackColor = true;
            this.btn_right.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btn_MouseDown);
            this.btn_right.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btn_MouseUp);

            this.btn_bleft.Location = new System.Drawing.Point(8, 242);
            this.btn_bleft.Name = "btn_bleft";
            this.btn_bleft.Size = new System.Drawing.Size(75, 44);
            this.btn_bleft.TabIndex = 19;
            this.btn_bleft.Text = "BottomLeft";
            this.btn_bleft.UseVisualStyleBackColor = true;
            this.btn_bleft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btn_MouseDown);
            this.btn_bleft.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btn_MouseUp);

            this.btn_bottom.Location = new System.Drawing.Point(107, 242);
            this.btn_bottom.Name = "btn_bottom";
            this.btn_bottom.Size = new System.Drawing.Size(75, 44);
            this.btn_bottom.TabIndex = 20;
            this.btn_bottom.Text = "Bottom";
            this.btn_bottom.UseVisualStyleBackColor = true;
            this.btn_bottom.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btn_MouseDown);
            this.btn_bottom.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btn_MouseUp);
   
            this.btn_bright.Location = new System.Drawing.Point(204, 242);
            this.btn_bright.Name = "btn_bright";
            this.btn_bright.Size = new System.Drawing.Size(75, 44);
            this.btn_bright.TabIndex = 21;
            this.btn_bright.Text = "BottomRight";
            this.btn_bright.UseVisualStyleBackColor = true;
            this.btn_bright.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btn_MouseDown);
            this.btn_bright.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btn_MouseUp);

            this.groupBox1.Controls.Add(this.btn_bleft);
            this.groupBox1.Controls.Add(this.btn_bottom);
            this.groupBox1.Controls.Add(this.btn_bright);
            this.groupBox1.Controls.Add(this.btn_right);
            this.groupBox1.Controls.Add(this.btn_uright);
            this.groupBox1.Controls.Add(this.btn_left);
            this.groupBox1.Controls.Add(this.btn_up);
            this.groupBox1.Controls.Add(this.btn_uleft);
            this.groupBox1.Controls.Add(this.btn_zoommin);
            this.groupBox1.Controls.Add(this.btn_zoomdefault);
            this.groupBox1.Controls.Add(this.btn_zoomplus);
            this.groupBox1.Location = new System.Drawing.Point(431, 87);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(294, 306);
            this.groupBox1.TabIndex = 24;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Navigation";

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(737, 410);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.videoViewerWF1);
            this.Controls.Add(this.btn_dcon);
            this.Controls.Add(this.btn_con);
            this.Controls.Add(this.btn_compose);
            this.Controls.Add(this.textbox_camurl);
            this.Controls.Add(this.lbl_camurl);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "Navigation";
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lbl_camurl;
        private System.Windows.Forms.TextBox textbox_camurl;
        private System.Windows.Forms.Button btn_compose;
        private System.Windows.Forms.Button btn_con;
        private System.Windows.Forms.Button btn_dcon;
        private Ozeki.Media.VideoViewerWF videoViewerWF1;
        private System.Windows.Forms.Button btn_zoomplus;
        private System.Windows.Forms.Button btn_zoomdefault;
        private System.Windows.Forms.Button btn_zoommin;
        private System.Windows.Forms.Button btn_uleft;
        private System.Windows.Forms.Button btn_up;
        private System.Windows.Forms.Button btn_left;
        private System.Windows.Forms.Button btn_uright;
        private System.Windows.Forms.Button btn_right;
        private System.Windows.Forms.Button btn_bleft;
        private System.Windows.Forms.Button btn_bottom;
        private System.Windows.Forms.Button btn_bright;
        private System.Windows.Forms.GroupBox groupBox1;
    }
}


Code 2 - GUI example in C#

Conclusion

By reading through this lecture you can find examples and implementations for zooming in C# using the Ozeki Camera SDK software. After examining this guide you will be fully familiar with the subject of selecting an area of interest on captured video frames with a bitmap image.

Related pages

FAQ

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

  1. What are the minimum and maximum aperture sizes?

    The minimum aperture size is 1x1 and the maximum aperture size is the maximum size of the picture.

  2. What is the origin image?

    Any image which can be found in your image collection before any transformations are applied is an origin image.

  3. 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.