This article shares the specific code of the C# screenshot tool small project for your reference. The specific content is as follows
1. Cause
The screenshots I have been using are screenshots of QQ, so I want to implement a simple screenshot for convenience.
2. Ideas
Let’s talk about the implementation process.
1. The main form has a screenshot button on it. Click to enter the screenshot form.
2. In the screenshot form, the background is set to the screenshot picture with the full screen, without borders, the form is maximized. At this time, what you see is a screen picture, which is actually a form, and then we will take pictures in this form. In fact, it is mainly the use of Graphics on the artboard. After the clipping is completed, the picture will be saved to the clipboard.
3. Code
Hotkey registration class
using System; using ; using ; namespace test { /// <summary> /// Hotkey class /// </summary> public class HotKey { /// <summary> /// If the function executes successfully, the return value is not 0. If the execution fails, the return value is 0 /// </summary> /// <returns></returns> [DllImport("", SetLastError = true)] public static extern bool RegisterHotKey( IntPtr hWnd, // The handle of the window. When the hotkey is pressed, the WM_HOTKEY message will be generated. This information will send the handle of the window. int id, // Defining the hotkey ID is the function of uniquely identifying the hotkey uint fsModifiers, // Hotkeys will only take effect when Alt, Ctrl, Shift, Windows and other keys are pressed, that is, WM_HOTKEY information will be generated Keys vk // Virtual key, that is, Alt+Ctrl+X is pressed, X represents the virtual key ); [DllImport("", SetLastError = true)] public static extern bool UnregisterHotKey( IntPtr hWnd, // Window handle int id // ID to cancel the hotkey ); } }
Main form
using System; using ; using ; using ; using ; using ; namespace test { public partial class Form1 : Form { [Flags] public enum KeyModifiers { //Define the hotkey value string (the hotkey value is specified by the system and cannot be changed) None = 0, Alt = 1, Ctrl = 2, Shift = 4, WindowsKey = 8 } public Form1() { InitializeComponent(); } //When form loading - Register shortcut key private void Form1_Load(object sender, EventArgs e) { uint ctrlHotKey = (uint)( | ); // Register hotkey is Alt+Ctrl+A, "100" is the unique identification hotkey (Handle,100,ctrlHotKey,); } //Screenshot Button private void button1_Click(object sender, EventArgs e) { if ( != ) { = ; (200); } int swidth = ; int sheight = ; Bitmap btm = new Bitmap(swidth,sheight); //The empty picture is the same size as the screen Graphics g = (btm); //Empty picture board (new Point(0,0),new Point(0,0),new Size(swidth,sheight)); //Copy the screen content to an empty picture Cutter cutter = new Cutter(btm); //Send screenshot = ; //Screenshot full screen, no border = btm; //New form screenshots are used as background (); } private void tiaoZ(object sender, ElapsedEventArgs e) { } //Form Close-Cancel Hotkey private void Form1_FormClosing(object sender, FormClosingEventArgs e) { (Handle,100); } //The event executed by pressing the shortcut key private void GlobalKeyProcess() { = ; (200); (); } //Rewrite. Monitor system messages and call corresponding methods protected override void WndProc(ref Message m) { const int WM_HOTKEY = 0x0312; //If the value is 0x0312 (I don't know why it is 0x0312, then it means that the user has pressed the hotkey switch () { case WM_HOTKEY: if (().Equals("100")) { GlobalKeyProcess(); } //todo other hotkeys break; } // Pass system messages from WndProc of the parent class (ref m); } } }
Screenshot Form-Core
using System; using ; using ; using ; using ; using ; using ; using ; using ; namespace test { public partial class Cutter : Form { Bitmap screenBtmp = null; //Screenshot of computer screen public Cutter(Bitmap btm) { InitializeComponent(); screenBtmp = btm; } //Right-click to exit private void Cutter_MouseClick(object sender, MouseEventArgs e) { if ( == ) { = ; (); } } bool CatchStart = false; //Free screenshot starts Point downPoint; //Initial point //Press the left mouse button - Start free screenshot private void Cutter_MouseDown(object sender, MouseEventArgs e) { if ( == ) { if (!CatchStart) { CatchStart = true; downPoint = new Point(,); //Initial point } } } Rectangle catchRec;//Storage intercept range //Mouse Move - Draw free screenshot path private void Cutter_MouseMove(object sender, MouseEventArgs e) { //Path drawing, core if (CatchStart) { // //Second buffer //Do not draw the mouse movement path directly on the control background artboard, which will cause many paths to be drawn because the path drawn before is still there // Instead, every time the mouse is moved in memory, a new BImtap is created like the screenshot, and the mouse movement path is drawn in this Bitmap // Then draw this new Bitmap on the form background drawing board, so that it will not cause many paths to be drawn, because every time a brand new Bitmao is drawn //But if you do this, because the mouse moves a lot, creating a large number of Bitmaps in memory will cause serious memory consumption, so after each movement is drawn, //Dispose() artboard, brush, and Bitmap resources need to be released. // Bitmap copyBtmp = (Bitmap)(); //Create a new one, draw the path on it //The upper left corner Point firstP = new Point(,); // Create a new drawing board, brush Graphics g = (copyBtmp); Pen p = new Pen(,1); // Calculate the path range int width = ( - ); int height = ( - ); if ( < ) { = ; } if ( < ) { = ; } //Draw the path catchRec = new Rectangle(firstP,new Size(width,height)); //Print the path on a new BItmap and release it later (p, catchRec); (); (); //Form background drawing board Graphics gf = (); //Draw the new picture on the drawing board of the form -- Free screenshot - The path is actually a picture of the same size as the screen, but there is a red path on it (copyBtmp,new Point(0,0)); (); //Release the memory Bimtap (); } } bool catchFinished = false; //Free screenshot end sign //The left mouse button pops up - End free screenshot private void Cutter_MouseUp(object sender, MouseEventArgs e) { if ( == ) { if (CatchStart) { CatchStart = false; catchFinished = true; } } } //Double-click on the left mouse button to save the freely intercepted pictures private void Cutter_MouseDoubleClick(object sender, MouseEventArgs e) { if(( == ) && catchFinished){ //Create an empty graph of the range size intercepted by the user Bitmap catchBtmp = new Bitmap(,); Graphics g = (catchBtmp); //Search on the original screenshot ScreenBitmap User selects the area of range size Draw to the empty picture above //After drawing, this empty picture is the picture we want to capture //Parameter 1 Original image // Parameter 2 Range area drawn on empty diagram //Parameter 3 The intercept range of the original image //Parameter 4 Unit of measurement (screenBtmp,new Rectangle(0,0,,),catchRec,); //Save freely captured images to the clipboard (); (catchBtmp); (); catchFinished = false; = screenBtmp; (); = ; (); } } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.