How to Capture screenshot and record computer screen in C#

By FoxLearn 7/18/2024 7:39:18 AM   7.98K
You can capture screenshots and record your computer screen in a C# Windows Forms Application using libraries like System.Drawing for screenshots.

How to capture screenshot in C#

Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "CaptureScreenshot" and then click OK

Drag and drop the PictureBox, Button controls from the Visual Studio Toolbox onto your form designer, then design your form as shown below.

take screenshot in c#

Add code to handle your form as below

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

namespace CaptureScreenshot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCapture_Click(object sender, EventArgs e)
        {
            Bitmap bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g = Graphics.FromImage(bm);
            g.CopyFromScreen(0, 0, 0, 0, bm.Size);
            pictureBox.Image = bm;
        }

        //Capture screenshot
        void Capture()
        {
            while (true)
            {
                Bitmap bm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                Graphics g = Graphics.FromImage(bm);
                g.CopyFromScreen(0, 0, 0, 0, bm.Size);
                pictureBox.Image = bm;
                Thread.Sleep(1000);
            }
        }

        private void btnRecord_Click(object sender, EventArgs e)
        {
            //Start new thread to record your screen
            Thread t = new Thread(Capture);
            t.Start();
        }
    }
}

To record the screen, you can create a theard, then call the screenshot method, add a delay each 1 second, then display the screen recording in a PictureBox control.

VIDEO TUTORIAL

Related