Solve C full screen screenshots of the implementation method

  • 2020-05-12 03:04:36
  • OfStack

Today a colleague wanted to write a full screen shot of the code. Of course, the first step is to be able to get a bitmap of the entire screen, remember Win32 API CreateDC, BitBlt and other functions can be used. So I looked it up on the Internet, and sure enough, the screenshots used these functions. But winform can already forget all of API, so look for an implementation without Win32 API. Integrated online implementation, as well as their own 1 some design, implementation ideas are as follows:
1. At the beginning of the screenshot, create a bitmap similar to the screen size, and then copy the screen bitmap to the bitmap using Graphics.CopyFromScreen (). This is a key step so that everything can be done on the bitmap without the actual screen.

Code
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp)) {
    g.CopyFromScreen(0, 0, 0, 0, new Size(width, height));
}

2. The next in order to facilitate in the above screenshot, there is a very important design implementation: use full-screen form instead of the existing real screen, so that you can put the screenshots all operations in the form of the process of implementation, the form is set without borders, height to width is equal to the screen size), in addition to show masking effect (only normal shows part of the selection screen content, and it's part one like a layer of translucent cover), add 1 layer of translucent bitmaps. The specific code is as follows:

Code
public partial class FullScreenForm : Form {
    private Rectangle rectSelected = Rectangle.Empty;
    private bool isClipping = false;
    private Bitmap screen;
    private Bitmap coverLayer = null;
    private Color coverColor;
    private Brush rectBrush = null;
    private Bitmap resultBmp = null;
    public FullScreenForm(Bitmap screen) {
        InitializeComponent();
        int width = Screen.PrimaryScreen.Bounds.Width;
        int height = Screen.PrimaryScreen.Bounds.Height;
        coverLayer = new Bitmap(width, height);
        coverColor = Color.FromArgb(50, 200, 0, 0);
        rectBrush = new SolidBrush(coverColor);
        using (Graphics g = Graphics.FromImage(coverLayer)) {
            g.Clear(coverColor);
        }
        this.Bounds = new Rectangle(0, 0, width, height);
        this.screen = screen;
        this.DoubleBuffered = true;
    }
    protected override void OnMouseDown(MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            isClipping = true;
            rectSelected.Location = e.Location;
        }
        else if (e.Button == MouseButtons.Right) {
            this.DialogResult = DialogResult.OK;
        }
    }
    protected override void OnMouseMove(MouseEventArgs e) {
        if (e.Button == MouseButtons.Left && isClipping) {
            rectSelected.Width = e.X - rectSelected.X;
            rectSelected.Height = e.Y - rectSelected.Y;

            this.Invalidate();
        }
    }
    protected override void OnMouseUp(MouseEventArgs e) {
        if (e.Button == MouseButtons.Left && isClipping) {
            rectSelected.Width = e.X - rectSelected.X;
            rectSelected.Height = e.Y - rectSelected.Y;
            this.Invalidate();
            resultBmp = new Bitmap(rectSelected.Width, rectSelected.Height);
            using (Graphics g = Graphics.FromImage(resultBmp)) {
                g.DrawImage(screen,new Rectangle(0, 0, rectSelected.Width, rectSelected.Height), rectSelected, GraphicsUnit.Pixel);
            }
            this.DialogResult = DialogResult.OK;
        }
    }
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.DrawImage(screen, 0, 0);
        g.DrawImage(coverLayer, 0, 0);
        PaintRectangle();
    }
    protected override void OnPaintBackground(PaintEventArgs e) {

    }
    protected override void OnKeyDown(KeyEventArgs e) {
        if (e.KeyCode == Keys.Escape) {
            this.DialogResult = DialogResult.Cancel;
        }
    }
    private void PaintRectangle() {
        using (Graphics g = Graphics.FromImage(coverLayer)) {
            g.Clear(coverColor);
            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(this.Bounds);
            path.AddRectangle(rectSelected);
            g.FillPath(rectBrush, path);
            g.DrawRectangle(Pens.Blue, rectSelected);
        }
    }
    public Bitmap ResultBitmap {
        get { return resultBmp; }
    }
}

The code above is easy to read, but one trick is GraphicsPath, which automatically forms a hollow area. The above implementation is easy to extend: multi-area screenshots, multi-referee screenshots, and so on are easy to implement.

Related articles: