I wrote a screenshot tool

Time:2020-9-6

It’s very simple, that is, first take a full screen shot, and then crop it as needed.

Therefore, to get the size of the desktop, the code is as follows:

public class PrimaryScreen
    {
        #region Win32 API
        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr ptr);
        [DllImport("gdi32.dll")]
        static extern int GetDeviceCaps(
        IntPtr hdc, // handle to DC
        int nIndex // index of capability
        );
        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
        static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
        #endregion
        #Region devicecaps constant
        const int HORZRES = 8;
        const int VERTRES = 10;
        const int LOGPIXELSX = 88;
        const int LOGPIXELSY = 90;
        const int DESKTOPVERTRES = 117;
        const int DESKTOPHORZRES = 118;
        #endregion
        #Region property
        /// 
        ///Gets the current physical size of the screen resolution
        /// 
        public static Size WorkingArea
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                Size size = new Size();
                size.Width = GetDeviceCaps(hdc, HORZRES);
                size.Height = GetDeviceCaps(hdc, VERTRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return size;
            }
        }
        /// 
        ///Current system DPI_ The X size is usually 96
        /// 
        public static int DpiX
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
                ReleaseDC(IntPtr.Zero, hdc);
                return DpiX;
            }
        }
        /// 
        ///Current system DPI_ The Y size is generally 96
        /// 
        public static int DpiY
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
                ReleaseDC(IntPtr.Zero, hdc);
                return DpiX;
            }
        }
        /// 
        ///Gets the real set desktop resolution size
        /// 
        public static Size DESKTOP
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                Size size = new Size();
                size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
                size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return size;
            }
        }

        /// 
        ///Gets the width scaling percentage
        /// 
        public static float ScaleX
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
                int d = GetDeviceCaps(hdc, HORZRES);
                float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return ScaleX;
            }
        }
        /// 
        ///Get height scaling percentage
        /// 
        public static float ScaleY
        {
            get
            {
                IntPtr hdc = GetDC(IntPtr.Zero);
                float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
                ReleaseDC(IntPtr.Zero, hdc);
                return ScaleY;
            }
        }
        #endregion
    }

usePrimaryScreen.DESKTOPYou can get the size of the desktop resolution. With this size, you can start the full screen capture. The code is as follows:

public class ImageHelper
    {
        /// 
        ///Capture full screen
        /// 
        /// 
        public static Bitmap GetScreen()
        {
            Size ScreenSize = PrimaryScreen.DESKTOP;
            Bitmap bmp = new Bitmap(ScreenSize.Width, ScreenSize.Height);
            using (Graphics g = Graphics.FromImage(bmp))
                g.CopyFromScreen(0, 0, 0, 0, new Size(ScreenSize.Width, ScreenSize.Height));
            return bmp;
        }

        /// 
        ///Image shading
        /// 
        ///Original image
        ///Brightness [- 255, 255]
        public static void Lighten(Bitmap b, int degree)
        {
            if (b == null)
            {
                //return null;
                return;
            }

            if (degree < -255) degree = -255;
            if (degree > 255) degree = 255;

            try
            {

                int width = b.Width;
                int height = b.Height;

                int pix = 0;

                BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                unsafe
                {
                    byte* p = (byte*)data.Scan0;
                    int offset = data.Stride - width * 3;
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            //Handles the brightness of pixels at a specified location
                            for (int i = 0; i < 3; i++)
                            {
                                pix = p[i] + degree;

                                if (degree < 0) p[i] = (byte)Math.Max(0, pix);
                                if (degree > 0) p[i] = (byte)Math.Min(255, pix);

                            } // i
                            p += 3;
                        } // x
                        p += offset;
                    } // y
                }

                b.UnlockBits(data);

                //return b;
            }
            catch
            {
                //return null;
            }

        } // end of Lighten
    }

Call ImageHelper.GetScreen () to get a full screen shot

Then, in order to realize the region screenshot, we need to put the full screen screenshot into a window for clipping. The code of the clipping window is shown as follows:

private void button1_Click(object sender, EventArgs e)
{
    this.Opacity  =0; // hide yourself first
    Bitmap bitmap =  ImageHelper.GetScreen (); // capture full screen
    Getscreenform frm = new getscreenform (bitmap); // prepare area screenshot
    frm.ShowDialog (); // the area screenshot interface will pop up
    this.Opacity  =1; // show yourself
}

 

There are a lot of codes for region screenshots, which are nothing more than the relevant processing of pressing, moving and releasing the mouse to cut the full screen screenshot. The code is as follows:

public partial class GetScreenForm : Form
    {
        /// 
        ///Bright image
        /// 
        public Bitmap bitmap { get; set; }

        /// 
        ///Dark graph
        /// 
        public Bitmap bitmap2 { get; set; }

        /// 
        ///The width of the screen
        /// 
        public int W { get; set; }
        /// 
        ///The height of the screen
        /// 
        public int H { get; set; }
        /// 
        ///Width for high DPI
        /// 
        public int W2 { get; set; }
        /// 
        ///High for high DPI
        /// 
        public int H2 { get; set; }

        Graphics g;
        Bitmap cache;
        Graphics gMain;

        /// 
        ///Construction method
        /// 
        public GetScreenForm(Bitmap bitmap)
        {
            //Bright image
            this.bitmap = bitmap;
            this.W = bitmap.Width;
            this.H = bitmap.Height;

            //Dark graph
            this.bitmap2 = new Bitmap(bitmap.Width, bitmap.Height);
            using (Graphics g = Graphics.FromImage(bitmap2))
                g.DrawImage(bitmap, 0, 0);
            ImageHelper.Lighten(bitmap2, -100);
            //The width and height for high DPI are obtained
            W2 = (int)(bitmap2.Width * PrimaryScreen.ScaleX);
            H2 = (int)(bitmap2.Height * PrimaryScreen.ScaleY);
            //Initialization
            InitializeComponent();
            this.Width = (int)(this.W / PrimaryScreen.ScaleX);
            this.Height = (int)(this.H / PrimaryScreen.ScaleY);
            //Drawing related
            cache = new Bitmap(this.W, this.H);
            gMain = this.CreateGraphics();
            g = Graphics.FromImage(cache);
        }

        /// 
        ///Double click to close
        /// 
        protected override void OnDoubleClick(EventArgs e)
        {
            //Get screenshots 
            if (SX > int.MinValue && SY > int.MinValue)
            {
                //Get area
                int x1 = SX, x2 = SX + SW;
                if (x1 > x2) { x2 = x1 + x2; x1 = x2 - x1; x2 = x2 - x1; };
                int y1 = SY, y2 = SY + SH;
                if (y1 > y2) { y2 = y1 + y2; y1 = y2 - y1; y2 = y2 - y1; };
                //Screenshot
                Bitmap bmp = new Bitmap(x2 - x1, y2 - y1);
                Graphics g6 = Graphics.FromImage(bmp);
                g6.DrawImage(bitmap,
                    new Rectangle(0, 0, bmp.Width, bmp.Height),
                    new Rectangle((int)(x1 * PrimaryScreen.ScaleX), (int)(y1 * PrimaryScreen.ScaleY), (int)((x2 - x1) * PrimaryScreen.ScaleX), (int)((y2 - y1) * PrimaryScreen.ScaleY)),
                    GraphicsUnit.Pixel);
                bmp.Save("x.jpg", ImageFormat.Jpeg);
            }
            this.Close();
        }

        private void GetScreenForm_Load(object sender, EventArgs e)
        {

        }

        protected override void OnShown(EventArgs e)
        {
            DrawForm();
        }

        void DrawForm()
        {
            //Draw a dark picture
            g.DrawImage(bitmap2,
                New rectangle (0, 0, W, H), // target
                New rectangle (0, 0, W2, H2), // source
                GraphicsUnit.Pixel);

            //Draw a bright picture
            if (SX > int.MinValue && SY > int.MinValue)
            {
                g.DrawImage(bitmap,
                    new Rectangle(SX, SY, SW, SH),
                    new Rectangle((int)(SX * PrimaryScreen.ScaleX), (int)(SY * PrimaryScreen.ScaleY), (int)(SW * PrimaryScreen.ScaleX), (int)(SH * PrimaryScreen.ScaleY)),
                    GraphicsUnit.Pixel);
                //New rectangle (SX, sy, SW, SH), // target
                //New rectangle (SX, sy, SW, SH), // source
                //GraphicsUnit.Pixel);
            }

            //Flip
            gMain.DrawImage(cache, 0, 0);
        }

        /// 
        ///Selected area
        /// 
        public int SX { get; set; } = int.MinValue;
        public int SY { get; set; } = int.MinValue;
        public int SW { get; set; }
        public int SH { get; set; }

        /// 
        ///Work type 0 not working 1 frame 2 move frame
        /// 
        public int WorkType { get; set; }

        /// 
        ///Starting point of movement
        /// 
        public int MoveX { get; set; }
        public int MoveY { get; set; }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            //Judge whether it is in the box or not
            bool inside = false;
            if (SX > int.MinValue && SY > int.MinValue)
            {
                int x1 = SX, x2 = SX + SW;
                if (x1 > x2) { x2 = x1 + x2; x1 = x2 - x1; x2 = x2 - x1; };
                int y1 = SY, y2 = SY + SH;
                if (y1 > y2) { y2 = y1 + y2; y1 = y2 - y1; y2 = y2 - y1; };
                if (e.X > x1 && e.X < x2
                    && e.Y > y1 && e.Y < y2)
                {
                    inside = true;
                }
            }
            if (inside)
            {
                //In the box, move the frame
                this.MoveX = e.X;
                this.MoveY = e.Y;
                this.WorkType = 2;
                DrawForm();
            }
            else
            {
                //Outside the box, redraw the frame
                this.SX = e.X;
                this.SY = e.Y;
                this.SW = 0;
                this.SH = 0;
                this.WorkType = 1;
                DrawForm();
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.WorkType == 1)
                {
                    //Picture frame
                    this.SW = e.X - this.SX;
                    this.SH = e.Y - this.SY;
                }
                else
                {
                    //Move frame
                    this.SX += e.X - this.MoveX;
                    this.SY += e.Y - this.MoveY;
                    this.MoveX = e.X;
                    this.MoveY = e.Y;
                }
                DrawForm();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (this.WorkType == 1)
            {
                this.SW = e.X - this.SX;
                this.SH = e.Y - this.SY;
            }
            this.WorkType = 0;
            DrawForm();
        }
    }

 

Provide source code for everyone to play, click here to download the source code.