How to stream the screen in C#?

This guide describes how you can capture your screen real time by using Ozeki Camera SDK. To use this feature, Ozeki Camera SDK has to be installed and a reference to OzekiSDK.dll has to be added to your Visual Studio project.

screen capture
Figure 1 - Desktop streaming lets you to monitor the desktop of your PC

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:

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

How you can use screen capture?

Desktop streaming or screen capturing offers the opportunity to record the desktop of a used PC. This way it is possible to record the desktop and trace all changes made on the followed PC.

This feature is very useful for security companies, public Internet access spots and workplaces with highly sensitive data.

It can also be used for education due the teacher can revise the work of the student by using Ozeki Camera SDK.

Properties

ScreenCapture uses the ScreenCaptureFPS and DesiredResolution properties to calibrate the quality of the stream. It is also possible to focus to a given area of the screen by using the Rectangle property. Here you can find a detailed explanation of all the used properties.

  • DesiredResolution: This property specifies the resolution of the video stream. By setting this value lower it is possible to use ScreenCapture with networks having lower bandwidth.
  • DesktopResolution: This property specifies the resolution of the desktop. It can not be modified by the user.
  • Mode: This property specifies how to stream the screen from three choices, which decides that the screen is shown without mouse, only mouse, or both of them can be seen.
  • Rectangle: By using this property it is possible to focus to a defined area of the screen and stream only that. This property is composed of four separate values called X1, Y1, Width and Height in this tutorial. X1 and Y1 specify the coordinates of the bottom left corner of the rectangle desired to be captured. Width and Height are the width and the height of the rectangle which borders the captured are. E.g. if X1 and Y1 are 0 and Width and Height are the Width and the Height of the desktop the entire screen will be displayed.
  • ScreenCaptureFPS: This is an enum type property which lets you to adjust the frame per second of the stream. It has a maximum value of 30. You can set its value to 1, 5, 10, 15, 20, 25 and 30 by default.

Methods

  • Start

    This methods starts the desktop capturing.

  • Stop

    It is possible to stop the capturing by using this method.

  • Screens

    This static method returns all screen's properties which connected to the PC.

  • SaveAllScreen

    This static method merges all screen's view connected to the PC, and saves a picture this way. The queue of the merge method is decided by the identifier of the screens.

C# code example for desktop streaming

Windows Form  

Windows forms version

MainForm.cs

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

namespace ScreenCapture_WF
{
    public partial class MainForm : Form
    {
        private MediaConnector _connector;
        private DrawingImageProvider _provider;
        private ScreenCapture _capture;

        public MainForm()
        {
            InitializeComponent();
            
            _capture = new ScreenCapture();
           
            _provider = new DrawingImageProvider();

            videoViewerWF1.SetImageProvider(_provider);

            _connector = new MediaConnector();
            _connector.Connect(_capture, _provider);
            
            _capture.Mode = ScreenCaptureMode.Both;

            _capture.Start();
        
            videoViewerWF1.Start();

            _cbFPS.DataSource = Enum.GetValues(typeof (ScreenCaptureFPS));
            _cbFPS.SelectedIndex = _cbFPS.Items.Count - 1;

            _cbCaptureMode.DataSource = 
            Enum.GetValues(typeof (ScreenCaptureMode));
            _cbCaptureMode.SelectedIndex = _cbCaptureMode.Items.Count - 1;

            var def = _capture.DesktopResolution;
            _tbWidth.Text = def.Height.ToString(CultureInfo.InvariantCulture);
            _tbHeight.Text = def.Width.ToString(CultureInfo.InvariantCulture);
        }

        private void save_Butt_Click(object sender, EventArgs e)
        {
            try
            {
                var bmp = ScreenCapture.SaveAllScreens();
                var path = "c:\\myBitmap.bmp";
                bmp.Save(path);
                MessageBox.Show("Screen saved to: " + path);
            }
            catch (Exception)
            {
                MessageBox.Show("There was a problem saving the file." +
                    "Check the file permissions.");
            }
        }

        private void _btStart_Click(object sender, EventArgs e)
        {
            _capture.Start(); 
        }

        private void _btStop_Click(object sender, EventArgs e)
        {
            _capture.Stop();
        }

        private void _cbFPS_SelectedIndexChanged(object sender, EventArgs e)
        {
            var combo = sender as ComboBox;
            _capture.CaptureFPS = (ScreenCaptureFPS)combo.SelectedItem;
        }

        private void setResolution_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(_tbWidth.Text)
            && !String.IsNullOrEmpty(_tbHeight.Text))
            {
                var sWidth = _tbWidth.Text;
                var sHeight = _tbHeight.Text;
                int width;
                int height;
                if (int.TryParse(sWidth, out width) 
                && int.TryParse(sHeight, out height))
                {
                    if (width > 0 && height > 0)
                        _capture.DesiredResolution = 
                        new Resolution(width, height);
                }
            }
        }

        private void _cbCaptureMode_SelectedIndexChanged
        (object sender, EventArgs e)
        {
            var combo = sender as ComboBox;
            _capture.Mode = (ScreenCaptureMode)combo.SelectedItem;
        }
    }
}

Code 1 - Edge detection example code in C#

GUI

After you have downloaded the Ozeki Camera SDK software you can find the GUI code in the Example folder. Please note that this function essentialy displays a desktop and contanins no controllers.

MainForm.Designer.cs

using System;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace ScreenCapture_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.save_Butt = new System.Windows.Forms.Button();
            this.videoViewerWF1 = new Ozeki.Media.VideoViewerWF();
            this._btStart = new System.Windows.Forms.Button();
            this._btStop = new System.Windows.Forms.Button();
            this._cbFPS = new System.Windows.Forms.ComboBox();
            this._tbWidth = new System.Windows.Forms.TextBox();
            this._tbHeight = new System.Windows.Forms.TextBox();
            this._lbX = new System.Windows.Forms.Label();
            this._btSetResolution = new System.Windows.Forms.Button();
            this._lbCaptureMode = new System.Windows.Forms.Label();
            this._cbCaptureMode = new System.Windows.Forms.ComboBox();
            this._lbFPS = new System.Windows.Forms.Label();
            this.SuspendLayout();
          
            this.save_Butt.Location = new System.Drawing.Point(12, 12);
            this.save_Butt.Name = "save_Butt";
            this.save_Butt.Size = new System.Drawing.Size(110, 23);
            this.save_Butt.TabIndex = 1;
            this.save_Butt.Text = "Save screens";
            this.save_Butt.UseVisualStyleBackColor = true;
            this.save_Butt.Click += 
            new System.EventHandler(this.save_Butt_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, 41);
            this.videoViewerWF1.Name = "videoViewerWF1";
            this.videoViewerWF1.RotateAngle = 0;
            this.videoViewerWF1.Size = new System.Drawing.Size(995, 535);
            this.videoViewerWF1.TabIndex = 0;
            this.videoViewerWF1.Text = "videoViewerWF1";

            this._btStart.Location = new System.Drawing.Point(158, 12);
            this._btStart.Name = "_btStart";
            this._btStart.Size = new System.Drawing.Size(75, 23);
            this._btStart.TabIndex = 2;
            this._btStart.Text = "Start";
            this._btStart.UseVisualStyleBackColor = true;
            this._btStart.Click += new System.EventHandler(this._btStart_Click);

            this._btStop.Location = new System.Drawing.Point(239, 12);
            this._btStop.Name = "_btStop";
            this._btStop.Size = new System.Drawing.Size(75, 23);
            this._btStop.TabIndex = 3;
            this._btStop.Text = "Stop";
            this._btStop.UseVisualStyleBackColor = true;
            this._btStop.Click += new System.EventHandler(this._btStop_Click);

            this._cbFPS.FormattingEnabled = true;
            this._cbFPS.Location = new System.Drawing.Point(593, 11);
            this._cbFPS.Name = "_cbFPS";
            this._cbFPS.Size = new System.Drawing.Size(121, 21);
            this._cbFPS.TabIndex = 4;
            this._cbFPS.DropDownStyle = ComboBoxStyle.DropDownList;
            this._cbFPS.SelectedIndexChanged += 
            new System.EventHandler(this._cbFPS_SelectedIndexChanged);

            this._tbWidth.Location = new System.Drawing.Point(754, 12);
            this._tbWidth.Name = "_tbWidth";
            this._tbWidth.Size = new System.Drawing.Size(66, 20);
            this._tbWidth.TabIndex = 5;

            this._tbHeight.Location = new System.Drawing.Point(846, 12);
            this._tbHeight.Name = "_tbHeight";
            this._tbHeight.Size = new System.Drawing.Size(66, 20);
            this._tbHeight.TabIndex = 6;

            this._lbX.AutoSize = true;
            this._lbX.Location = new System.Drawing.Point(826, 15);
            this._lbX.Name = "_lbX";
            this._lbX.Size = new System.Drawing.Size(14, 13);
            this._lbX.TabIndex = 7;
            this._lbX.Text = "X";

            this._btSetResolution.Location = new System.Drawing.Point(918, 9);
            this._btSetResolution.Name = "_btSetResolution";
            this._btSetResolution.Size = new System.Drawing.Size(89, 23);
            this._btSetResolution.TabIndex = 8;
            this._btSetResolution.Text = "Set Resolution";
            this._btSetResolution.UseVisualStyleBackColor = true;
            this._btSetResolution.Click += 
            new System.EventHandler(this.setResolution_Click);

            this._lbCaptureMode.AutoSize = true;
            this._lbCaptureMode.Location = new System.Drawing.Point(335, 14);
            this._lbCaptureMode.Name = "_lbCaptureMode";
            this._lbCaptureMode.Size = new System.Drawing.Size(77, 13);
            this._lbCaptureMode.TabIndex = 9;
            this._lbCaptureMode.Text = "Capture Mode:";

            this._cbCaptureMode.FormattingEnabled = true;
            this._cbCaptureMode.Location = new System.Drawing.Point(418, 11);
            this._cbCaptureMode.Name = "_cbCaptureMode";
            this._cbCaptureMode.Size = new System.Drawing.Size(121, 21);
            this._cbCaptureMode.TabIndex = 10;
            this._cbCaptureMode.DropDownStyle = ComboBoxStyle.DropDownList;
            this._cbCaptureMode.SelectedIndexChanged += 
            new System.EventHandler(this._cbCaptureMode_SelectedIndexChanged);

            this._lbFPS.AutoSize = true;
            this._lbFPS.Location = new System.Drawing.Point(557, 14);
            this._lbFPS.Name = "_lbFPS";
            this._lbFPS.Size = new System.Drawing.Size(30, 13);
            this._lbFPS.TabIndex = 11;
            this._lbFPS.Text = "FPS:";

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1019, 588);
            this.Controls.Add(this._lbFPS);
            this.Controls.Add(this._cbCaptureMode);
            this.Controls.Add(this._lbCaptureMode);
            this.Controls.Add(this._btSetResolution);
            this.Controls.Add(this._lbX);
            this.Controls.Add(this._tbHeight);
            this.Controls.Add(this._tbWidth);
            this.Controls.Add(this._cbFPS);
            this.Controls.Add(this._btStop);
            this.Controls.Add(this._btStart);
            this.Controls.Add(this.save_Butt);
            this.Controls.Add(this.videoViewerWF1);
            this.Name = "MainForm";
            this.Text = "ScreenCapture";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private Ozeki.Media.VideoViewerWF videoViewerWF1;
        private System.Windows.Forms.Button save_Butt;
        private System.Windows.Forms.Button _btStart;
        private System.Windows.Forms.Button _btStop;
        private System.Windows.Forms.ComboBox _cbFPS;
        private TextBox _tbWidth;
        private TextBox _tbHeight;
        private Label _lbX;
        private Button _btSetResolution;
        private Label _lbCaptureMode;
        private ComboBox _cbCaptureMode;
        private Label _lbFPS;
    }
}
	
Code 2 - Edge detection example code in C#

Conclusion

After learning the examples and information of this webpage you will be fully familiar with the concept and usage of desktop streaming. By using our example code and the functions discussed above you can install desktop screening to your C# camera application using the Ozeki Camera SDK.

Related pages

FAQ

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

  1. What kind of developer environment is needed?

    • Microsoft Visual Studio
    • Microsoft .Net Framework 4.0
  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. How can I increase the quality of the stream?

    Please increase the value of the DesiredResolution property.

  4. What can I do if the stream is very slow or lagging?

    Please decrease the value of the DesiredResolution property and increase the value of ScreenCaptureFPS to 30.

More information