Use C # to implement full-screen screenshots

Source: Internet
Author: User

Today, a colleague wants to write a full screen code. Of course, the first step is to obtain the bitmap of the entire screen. Remember that Win32 API functions such as CreateDC and BitBlt can be used. So I checked it online and used these functions on the screen. However, winform can forget all the APIS, so you have to find a method without Win32 APIs. Based on the implementation on the Internet and some of your own designs, the Implementation ideas are as follows: 1. at the beginning, create a bitmap of the same size as the screen, and then use Graphics. copyFromScreen () copies the screen bitmap to this bitmap. This is a key step, so that all operations can be performed on the bitmap, without the actual screen.
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. next, to make it easier, there is an important design implementation method: replacing the existing real screen with a full screen form, in this way, all operations in the process can be implemented on that form (this form is set to borderless, and the height and width are equal to the screen size ), in addition, in order to display the masking effect (only the selected part of the screen content can be displayed normally, but the part is actually covered with a translucent layer), a layer of translucent bitmap is added. The Code is as follows:

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 above code is easy to understand. Here is a technique called GraphicsPath, which automatically forms a hollow area. The above implementation is easy to expand: multi-region and multi-Referee are easy to implement.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.