A
A
Alexander Prokopenko2022-01-22 16:21:02
C++ / C#
Alexander Prokopenko, 2022-01-22 16:21:02

How to make a screenshot of a screen area in C#?

I want to take a screenshot of an area of ​​the screen in C#, but I can't.

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

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

        private bool isLMB = false;

        private Point point1; 
        private Point point2;

        private int width;
        private int height;

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {         
            isLMB = true;
            point1 = new Point(Cursor.Position.X, Cursor.Position.Y);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isLMB)
            {
                point2 = new Point(Cursor.Position.X, Cursor.Position.Y);

                width = point2.X - point1.X;
                height = point2.Y - point1.Y;

                pictureBox1.Size = new Size(width, height);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Default;
            isLMB = false;
            pictureBox1.BackColor = Color.Blue;
            pictureBox1.Location = point1;            
            this.Opacity = .85;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            this.menuStrip1.Visible = true;
        }

        private void makeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.Cross;
            this.WindowState = FormWindowState.Maximized;
            this.Opacity = .5;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.menuStrip1.Visible = false;
            pictureBox1.Location = new Point(0, 0);
            pictureBox1.Width = 10000;
            pictureBox1.Height = 10000;
            pictureBox1.BackColor = Color.White;
        }

        private void Screenshot(string CapturedFilePath)
        {
            Bitmap bitmap = new Bitmap
            (width, height);

            Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
            graphics.CopyFromScreen(0, 0, 0,0, bitmap.Size);

            bitmap.Save(CapturedFilePath, ImageFormat.Jpeg);
        }
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "JPEG(*.JPEG)|*.jpeg";
            saveFileDialog1.FileName = DateTime.Now.ToString("s_m_dd_MM_yyyy");

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.WindowState = FormWindowState.Minimized;

                System.Threading.Thread.Sleep(500);
                Screenshot(saveFileDialog1.FileName);
            }
        }
    }
}


Here's what I got (I know that the code may not be the best quality, but this is my 2nd application in C #), but it only takes a screen of the upper left corner.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HemulGM, 2022-01-22
@AlexanderProkopenko

A screenshot of a part of the screen is done like this:
1. A full screenshot of the desktop is
taken 2. A window is created on top of all windows, which displays the resulting image in full size (fullscreen, without frames)
3. The user selects an area
4. The selected area is cut out and saved
You can check it by running in the background of the video and pressing Win + Shift + S (on win10). The picture will freeze.
Of course, you can do it differently by simply displaying a fullscreen window, which will darken the desktop a bit and allow you to select an area, but the moment may be missed (in the same video, for example)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question