ByteScout Screen Capturing SDK - C# - Adding Text And Image Overlays - ByteScout
Announcement
Our ByteScout SDK products are sunsetting as we focus on expanding new solutions.
Learn More Open modal
Close modal
Announcement Important Update
ByteScout SDK Sunsetting Notice
Our ByteScout SDK products are sunsetting as we focus on our new & improved solutions. Thank you for being part of our journey, and we look forward to supporting you in this next chapter!

ByteScout Screen Capturing SDK – C# – Adding Text And Image Overlays

  • Home
  • /
  • Articles
  • /
  • ByteScout Screen Capturing SDK – C# – Adding Text And Image Overlays

ByteScout Screen Capturing SDK – C# – Adding Text And Image Overlays

ctrlImageOverlay.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BytescoutScreenCapturingLib;

namespace ScreenCapturingClient
{
    public partial class ctrlImageOverlay : UserControl
    {
        ImageOverlay _imageOverlay = null;
        ICapturer _capturer = null;
        int _index = -1;
        bool _isLoaded = false;

        public ctrlImageOverlay()
        {
            InitializeComponent();
        }

        public ctrlImageOverlay(ImageOverlay imageOverlay, int index, ICapturer capturer)
        {
            InitializeComponent();

            _imageOverlay = imageOverlay;
            _index = index;
            _capturer = capturer;

            nmLeft.Value = _imageOverlay.Left;
            nmTop.Value = _imageOverlay.Top;
            txtFileName.Text = _imageOverlay.FileName;
            chkVisible.Checked = _imageOverlay.Visible;

            txtFileName.Enabled = false;
            btnBrowseFile.Enabled = false;

            _isLoaded = true;
        }

        private void btnBrowseFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Bitmap Files (*.bmp)|*.bmp|Png Files (*.png)|*.png";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = dlg.FileName;
            }
        }

        private void nmLeft_ValueChanged(object sender, EventArgs e)
        {
            if (_imageOverlay != null && _capturer != null && _index != -1 && _isLoaded)
            {
                _capturer.UpdateOverlayPosition(_index, (int)nmLeft.Value, (int)nmTop.Value);
                _imageOverlay.Left = (int)nmLeft.Value;
            }
        }

        private void nmTop_ValueChanged(object sender, EventArgs e)
        {
            if (_imageOverlay != null && _capturer != null && _index != -1 && _isLoaded)
            {
                _capturer.UpdateOverlayPosition(_index, (int)nmLeft.Value, (int)nmTop.Value);
                _imageOverlay.Top = (int)nmTop.Value;
            }
        }

        public ImageOverlay GetImageOverlay()
        {
            if (_imageOverlay == null)
            {
                _imageOverlay = new ImageOverlay();
                _imageOverlay.Left = (int)nmLeft.Value;
                _imageOverlay.Top = (int)nmTop.Value;
                _imageOverlay.Visible = chkVisible.Checked;
                _imageOverlay.FileName = txtFileName.Text;
            }

            return _imageOverlay;
        }

        private void chkVisible_CheckedChanged(object sender, EventArgs e)
        {
            if (_imageOverlay != null && _capturer != null && _index != -1 && _isLoaded)
            {
                _capturer.UpdateOverlayVisibility(_index, chkVisible.Checked);
                _imageOverlay.Visible = chkVisible.Checked;
            }
        }
    }
}

ctrlTextOverlay.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BytescoutScreenCapturingLib;

namespace ScreenCapturingClient
{
    public partial class ctrlTextOverlay : UserControl
    {
        TextOverlay _textOverlay = null;
        ICapturer _capturer = null;
        int _index = -1;
        bool _isLoaded = false;

        public ctrlTextOverlay()
        {
            InitializeComponent();
            pnlBackColor.BackColor = Color.Transparent;

        }
        public ctrlTextOverlay(TextOverlay textOverlay, int index, ICapturer capturer)
        {
            InitializeComponent();

            _textOverlay = textOverlay;
            _index = index;
            _capturer = capturer;

            nmLeft.Value = _textOverlay.Left;
            nmTop.Value = _textOverlay.Top;
            txtText.Text = _textOverlay.Text;
            txtFont.Text = _textOverlay.Font.Name;
            txtFont.Font = _textOverlay.Font;
            pnlColor.BackColor = _textOverlay.Color;
            chkVisible.Checked = _textOverlay.Visible;
            btnUpdate.Visible = true;

            chkBackColor.Checked = _textOverlay.BackColor != Color.Transparent;
            btnBackColor.Enabled = _textOverlay.BackColor != Color.Transparent;
            pnlBackColor.BackColor = _textOverlay.BackColor;

            txtFont.Enabled = false;
            btnFont.Enabled = false;
            pnlColor.Enabled = false;
            btnColor.Enabled = false;
            chkBackColor.Enabled = false;
            btnBackColor.Enabled = false;
            pnlBackColor.Enabled = false;

            _isLoaded = true;
        }

        private void btnFont_Click(object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog();
            dlg.Font = txtFont.Font;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtFont.Font = dlg.Font;
                txtFont.Text = dlg.Font.Name;

                if (_textOverlay != null)
                {
                    _textOverlay.Font = dlg.Font;
                }
            }
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();
            dlg.AllowFullOpen = true;
            dlg.AnyColor = true;
            dlg.FullOpen = true;
            dlg.CustomColors = new int[] { Color.Transparent.ToArgb() };
           
            dlg.Color = pnlColor.BackColor;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                pnlColor.BackColor = dlg.Color;

                if (_textOverlay != null)
                {
                    _textOverlay.Color = dlg.Color;
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (_textOverlay != null && _capturer != null && _index != -1)
            {
                _capturer.UpdateTextOverlay(_index, txtText.Text);
                _textOverlay.Text = txtText.Text;
            }
        }

        private void nmLeft_ValueChanged(object sender, EventArgs e)
        {
            if (_textOverlay != null && _capturer != null && _index != -1 && _isLoaded)
            {
                _capturer.UpdateOverlayPosition(_index, (int)nmLeft.Value, (int)nmTop.Value);
                _textOverlay.Left = (int)nmLeft.Value; ;
            }
        }

        private void nmTop_ValueChanged(object sender, EventArgs e)
        {
            if (_textOverlay != null && _capturer != null && _index != -1 && _isLoaded)
            {
                _capturer.UpdateOverlayPosition(_index, (int)nmLeft.Value, (int)nmTop.Value);
                _textOverlay.Top = (int)nmTop.Value;
            }
        }

        private void chkVisible_CheckedChanged(object sender, EventArgs e)
        {
            if (_textOverlay != null && _capturer != null && _index != -1 && _isLoaded)
            {
                _capturer.UpdateOverlayVisibility(_index, chkVisible.Checked);
                _textOverlay.Visible = chkVisible.Checked;
            }
        }

        public TextOverlay GetTextOverlay()
        {
            if (_textOverlay == null)
            {
                _textOverlay = new TextOverlay();

                _textOverlay.Left = (int)nmLeft.Value;
                _textOverlay.Top = (int)nmTop.Value;
                _textOverlay.Text = txtText.Text;
                _textOverlay.Font = txtFont.Font;
                _textOverlay.Color = pnlColor.BackColor;
                _textOverlay.Visible = chkVisible.Checked;
                _textOverlay.BackColor = pnlBackColor.BackColor;
            }

            return _textOverlay;
        }

        private void chkBackColor_CheckedChanged(object sender, EventArgs e)
        {
            btnBackColor.Enabled = chkBackColor.Checked;
            pnlBackColor.Enabled = chkBackColor.Checked;
            if (!chkBackColor.Checked)
            {
                pnlBackColor.BackColor = Color.Transparent;
            }
            if (_textOverlay != null)
            {
                _textOverlay.BackColor = pnlBackColor.BackColor;
            }
        }

        private void btnBackColor_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();
            dlg.AllowFullOpen = true;
            dlg.AnyColor = true;
            dlg.FullOpen = true;

            dlg.Color = pnlBackColor.BackColor;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                pnlBackColor.BackColor = dlg.Color;

                if (_textOverlay != null)
                {
                    _textOverlay.BackColor = dlg.Color;
                }
            }
        }
       
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BytescoutScreenCapturingLib;

namespace ScreenCapturingClient
{
    public partial class Form1 : Form
    {
        ICapturer _capturer;
        int _previewHdc;
        Graphics _previewGraphics;

        public Form1()
        {
            InitializeComponent();
        }

        private void InitCapturer()
        {
            _capturer = new CapturerClass();
            _capturer.RegistrationName = "demo";
            _capturer.RegistrationKey = "demo";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                InitCapturer();

                cmbWebCam.Items.Clear();
                for (int i = 0; i < _capturer.WebCamCount; i++)
                {
                    cmbWebCam.Items.Add(_capturer.GetWebCamName(i));
                }
                if(cmbWebCam.Items.Count > 0)
                    cmbWebCam.SelectedIndex = 0;

                cmbVideoCodec.Items.Clear();
                for (int i = 0; i < _capturer.VideoCodecsCount; i++)
                {
                    cmbVideoCodec.Items.Add(_capturer.GetVideoCodecName(i));
                }
                if(cmbVideoCodec.Items.Count > 0)
                    cmbVideoCodec.SelectedIndex = 0;

                cmbAudioDevices.Items.Clear();
                for (int i = 0; i < _capturer.AudioDeviceCount; i++)
                {
                    cmbAudioDevices.Items.Add(_capturer.GetAudioDeviceName(i));
                }
                if(cmbAudioDevices.Items.Count > 0)
                    cmbAudioDevices.SelectedIndex = 0;

                cmbAudioCodecs.Items.Clear();
                for (int i = 0; i < _capturer.AudioCodecsCount; i++)
                {
                    cmbAudioCodecs.Items.Add(_capturer.GetAudioCodecName(i));
                }
                if(cmbAudioCodecs.Items.Count > 0)
                    cmbAudioCodecs.SelectedIndex = 0;

                cmbCaptureAreaType.Items.Clear();
                cmbCaptureAreaType.DataSource = Enum.GetValues(typeof(CaptureAreaType));
                cmbCaptureAreaType.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void chkEnableAudio_CheckedChanged(object sender, EventArgs e)
        {
            cmbAudioDevices.Enabled = chkEnableAudio.Checked;
            cmbAudioCodecs.Enabled = chkEnableAudio.Checked;
        }

        private void chkEnableWebcam_CheckedChanged(object sender, EventArgs e)
        {
            _capturer.AddWebCamVideo = chkEnableWebcam.Checked;

            cmbWebCam.Enabled = chkEnableWebcam.Checked;
            nmWebCamLeft.Enabled = chkEnableWebcam.Checked;
            nmWebCamTop.Enabled = chkEnableWebcam.Checked;
            nmWebCamWidth.Enabled = chkEnableWebcam.Checked;
            nmWebCamHeight.Enabled = chkEnableWebcam.Checked;
        }

        private void btnOutputFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtOutputFile.Text = dlg.FileName;
            }
        }

        private void btnLogFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtLogFile.Text = dlg.FileName;
            }
        }

        private void btnRecord_Click(object sender, EventArgs e)
        {
            try
            {
                if (_capturer.IsRunning)
                {
                    _capturer.Stop();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(_capturer);
                    _capturer = null;
                    InitCapturer();

                    lstOverlays.Items.Clear();
                    btnRecord.Text = "Record";
                    grpProperties.Enabled = true;
                    lblStatus.Text = "Stopped";
                }
                else
                {
                    _capturer.AudioEnabled = chkEnableAudio.Checked;
                    if (chkEnableAudio.Checked)
                    {
                        _capturer.CurrentAudioDeviceName = cmbAudioDevices.SelectedItem.ToString();
                        //_capturer.CurrentAudioCodecName = cmbAudioCodecs.SelectedItem.ToString();
                    }

                    _capturer.AddWebCamVideo = chkEnableWebcam.Checked;
                    if (chkEnableWebcam.Checked)
                    {
                        _capturer.SetWebCamVideoRectangle((int)nmWebCamLeft.Value, (int)nmWebCamTop.Value, (int)nmWebCamWidth.Value, (int)nmWebCamHeight.Value);
                        _capturer.CurrentWebCamName = cmbWebCam.SelectedItem.ToString();
                    }

                    //_capturer.CurrentVideoCodecName = cmbVideoCodec.SelectedItem.ToString();

                    if (!string.IsNullOrEmpty(txtLogFile.Text))
                    {
                        _capturer.SetLogFile(txtLogFile.Text);
                    }

                    _capturer.OutputFileName = txtOutputFile.Text;

                    _capturer.CapturingType = (CaptureAreaType)cmbCaptureAreaType.SelectedItem;

                    _previewGraphics = pnlPreview.CreateGraphics();
                    _previewHdc = _previewGraphics.GetHdc().ToInt32();
                    _capturer.SetFullPreviewContext(_previewHdc, 0, 0, pnlPreview.Width, pnlPreview.Height, 5);

                    _capturer.CaptureRectLeft = (int)nmCaptureLeft.Value;
                    _capturer.CaptureRectTop = (int)nmCaptureTop.Value;
                    _capturer.CaptureRectWidth = (int)nmCaptureWidth.Value;
                    _capturer.CaptureRectHeight = (int)nmCaptureHeight.Value;

                    _capturer.OutputWidth = (int)nmOutputWidth.Value;
                    _capturer.OutputHeight = (int)nmOutputHeight.Value;


                    foreach (ListViewItem item in lstOverlays.Items)
                    {
                        Overlay overlay = item.Tag as Overlay;
                        if (overlay is TextOverlay)
                        {
                            TextOverlay textOverlay = overlay as TextOverlay;

                            int a = (int)textOverlay.BackColor.A;
                            //_capturer.AddTextOverlay(textOverlay.Left, textOverlay.Top, textOverlay.Text, textOverlay.Font.Name,
                            //                        (int)textOverlay.Font.Size,textOverlay.Font.Bold, textOverlay.Font.Italic,
                            //                        textOverlay.Font.Underline, (uint)ColorTranslator.ToOle(textOverlay.Color));
                            _capturer.AddTextOverlayWithBackground2(textOverlay.Left, textOverlay.Top, textOverlay.Text, textOverlay.Font.Name,
                                                    (int)textOverlay.Font.Size, textOverlay.Font.Bold, textOverlay.Font.Italic,
                                                    textOverlay.Font.Underline,
                                                    textOverlay.Color.R, textOverlay.Color.G, textOverlay.Color.B, textOverlay.Color.A,
                                                    textOverlay.BackColor.R, textOverlay.BackColor.G, textOverlay.BackColor.B, textOverlay.BackColor.A);
                        }
                        else if (overlay is ImageOverlay)
                        {
                            ImageOverlay imageOverlay = overlay as ImageOverlay;
                            _capturer.AddImageOverlay(imageOverlay.Left, imageOverlay.Top, imageOverlay.FileName);
                        }
                    }


                    _capturer.Run();

	//' IMPORTANT: if you want to check for some code if need to stop the recording then make sure you are 
	//' using Thread.Sleep(1) inside the checking loop, so you have the loop like
	//' Do {
	//' Thread.Sleep(1) 
	//' }
	//' While(StopButtonNotClicked);


                    btnRecord.Text = "Stop";
                    grpProperties.Enabled = false;
                    lblStatus.Text = "Running";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAddTextOverlay_Click(object sender, EventArgs e)
        {
            try
            {
                frmNewOverlay frm = new frmNewOverlay(true);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    TextOverlay textOverlay = frm.GetOverlay() as TextOverlay;
                    if (_capturer.IsRunning)
                    {
                        //_capturer.AddTextOverlay(overlay.Left, overlay.Top, overlay.Text, overlay.Font.Name, (int)overlay.Font.Size,
                        //                        overlay.Font.Bold, overlay.Font.Italic, overlay.Font.Underline, (uint)ColorTranslator.ToOle(overlay.Color));
                        _capturer.AddTextOverlayWithBackground2(textOverlay.Left, textOverlay.Top, textOverlay.Text, textOverlay.Font.Name,
                                                   (int)textOverlay.Font.Size, textOverlay.Font.Bold, textOverlay.Font.Italic,
                                                   textOverlay.Font.Underline,
                                                   textOverlay.Color.R, textOverlay.Color.G, textOverlay.Color.B, textOverlay.Color.A,
                                                   textOverlay.BackColor.R, textOverlay.BackColor.G, textOverlay.BackColor.B, textOverlay.BackColor.A);
                    }

                    ListViewItem item = new ListViewItem();
                    item.Text = textOverlay.ToString();
                    item.Tag = textOverlay;
                    lstOverlays.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAddImageOverlay_Click(object sender, EventArgs e)
        {
            try
            {
                frmNewOverlay frm = new frmNewOverlay(false);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    ImageOverlay overlay = frm.GetOverlay() as ImageOverlay;

                    if (_capturer.IsRunning)
                    {
                        _capturer.AddImageOverlay(overlay.Left, overlay.Top, overlay.FileName);
                    }

                    ListViewItem item = new ListViewItem();
                    item.Text = overlay.ToString();
                    item.Tag = overlay;
                    lstOverlays.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnRemoveOverlay_Click(object sender, EventArgs e)
        {
            pnlCurrentOverlay.Controls.Clear();
            if (lstOverlays.SelectedItems.Count > 0)
            {
                if (_capturer.IsRunning)
                {
                    _capturer.RemoveOverlay(lstOverlays.Items.IndexOf(lstOverlays.SelectedItems[0]));
                }
                lstOverlays.Items.Remove(lstOverlays.SelectedItems[0]);

            }
        }

        private void lstOverlays_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlCurrentOverlay.Controls.Clear();
            if(lstOverlays.SelectedItems.Count > 0)
            {
                Overlay overlay = lstOverlays.SelectedItems[0].Tag as Overlay;
                if (overlay is TextOverlay)
                {
                    ctrlTextOverlay ctrl = new ctrlTextOverlay(overlay as TextOverlay, lstOverlays.Items.IndexOf(lstOverlays.SelectedItems[0]), _capturer);
                    ctrl.Dock = DockStyle.Fill;

                    pnlCurrentOverlay.Controls.Add(ctrl);
                }
                else if (overlay is ImageOverlay)
                {
                    ctrlImageOverlay ctrl = new ctrlImageOverlay(overlay as ImageOverlay, lstOverlays.Items.IndexOf(lstOverlays.SelectedItems[0]), _capturer);
                    ctrl.Dock = DockStyle.Fill;

                    pnlCurrentOverlay.Controls.Add(ctrl);
                }
            }
        }

        
    }
}

frmNewOverlay.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ScreenCapturingClient
{
    public partial class frmNewOverlay : Form
    {
        public frmNewOverlay(bool textOverlay)
        {
            InitializeComponent();

            UserControl ctrl = null;

            if (textOverlay)
            {
                ctrl = new ctrlTextOverlay();
            }
            else
            {
                ctrl = new ctrlImageOverlay();
            }

            ctrl.Dock = DockStyle.Fill;
            panel1.Controls.Add(ctrl);
        }

        public Overlay GetOverlay()
        {
            if (panel1.Controls.Count > 0)
            {
                if (panel1.Controls[0] is ctrlTextOverlay)
                {
                    return ((ctrlTextOverlay)panel1.Controls[0]).GetTextOverlay();
                }
                else if (panel1.Controls[0] is ctrlImageOverlay)
                {
                    return ((ctrlImageOverlay)panel1.Controls[0]).GetImageOverlay();
                }
            }

            return null;
        }
    }
}

Overlay.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ScreenCapturingClient
{
    public class Overlay
    {
        int _left = 0;
        int _top = 0;
        bool _visible = true;

        public int Left { get{return _left;}set{_left = value;}}
        public int Top { get { return _top; } set { _top = value; } }
        public bool Visible { get { return _visible; } set { _visible = value; } }
    }

    public class TextOverlay : Overlay
    {
        string _text = string.Empty;
        Font _font = new Font("Arial", 10f);
        Color _color = Color.Red;
        Color _backColor = Color.Transparent;

        public string Text { get { return _text; } set { _text = value; } }
        public Font Font { get { return _font; } set { _font = value; } }
        public Color Color { get { return _color; } set { _color = value; } }
        public Color BackColor { get { return _backColor; } set { _backColor = value; } }

        public override string ToString()
        {
            return string.Format("Text Overlay. Location: {0}, Text: {1}, Font: {2}", new Point(Left, Top), Text, Font);
        }
    }

    public class ImageOverlay : Overlay
    {
        string _fileName = string.Empty;

        public string FileName { get { return _fileName; } set { _fileName = value; } }

        public override string ToString()
        {
            return string.Format("Image Overlay. Location: {0}, File Name: {1}", new Point(Left, Top), FileName);
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ScreenCapturingClient
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Tutorials:

prev
next