C # imitate QQ screenshot function

xiaoxiao2021-03-31  250

The front array changed the code of the section C # screenshot, and it is now useful to use it for everyone.

Primary file

CaptureScreenform.cs

Using system.drawing; using system.collections; using system.windows.form; using system.data; using system.runtime.interopservices;

Namespace CaptureScreen {///

/// CapturescreenForm's summary description. /// public class CaptureScreenForm: System.Windows.Forms.Form {private System.Windows.Forms.PictureBox picbox; private System.Windows.Forms.Button exitBtn; private System.Windows.Forms.Button aboutBtn; private System.Windows.Forms.Button ClearBtn; private System.Windows.Forms.Button saveBtn; private System.Windows.Forms.Button fullScreenBtn; private System.Windows.Forms.Button mouseCaptureBtn;

Public static image image = null; private system.windows.Forms.Panel Panel1;

///

/// The required designer variable. /// private system.componentmodel.container components = null;

Public CapturescreenForm () {// // Windows Form Designer Support for // InitializationComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.picbox = new System.Windows.Forms.PictureBox (); this.fullScreenBtn = new System.Windows.Forms.Button (); this.mouseCaptureBtn = new System. Windows.Forms.Button (); this.clearbtn = new system.windows.Forms.Button (); this.savebtn = new system.windows.Forms.Button (); this.aboutbtn = new system.windows.Forms.Button (); This.exitbtn = new system.windows.Forms.Button (); this.Panel1 = new system.windows.Forms.Panel (); this.panel1.suspendlayout (); this.suspendlayout (); // / / picbox // this.picbox.location = new system.drawing.point (0, 40); this.picbox.name = "picbox"; this.picbox.size = new system.drawing.size (680, 312); this.picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.picbox.TabIndex = 0; this.picbox.TabStop = false; // // fullScreenBtn // this.fullScreenBtn.Location = new System.Drawing. Point (0, 0); this.fullscreenbtn.name = "fullscreenbtn"; this.ffullscre enBtn.Size = new System.Drawing.Size (96, 32); this.fullScreenBtn.TabIndex = 1; this.fullScreenBtn.Text = "Full Screen taken"; this.fullScreenBtn.Click = new System.EventHandler (this.fullScreenBtn_Click ); // // mouseCaptureBtn // this.mouseCaptureBtn.Location = new System.Drawing.Point (120, 0); this.mouseCaptureBtn.Name = "mouseCaptureBtn"; this.mouseCaptureBtn.Size = new System.Drawing.Size ( 96, 32); this.mousecapturebtn.tabindex = 2; this.mousecapturebtn.text = "Interval"; this.mousecapturebtn.click = new system.eventhandler (this.mousecapturebtn_click);

// // clearbtn // this.clearbtn.location = new system.drawing.point (240, 0); this.clearbtn.name = "clearbtn"; this.clearbtn.size = new system.drawing.size (88, 32); this.clearbtn.tabindex = 3; this.clearbtn.text = "Clear intercept picture"; this.clearbtn.click = new system.eventhandler (this.clearbtn_click; /// savebtn // this.savebtn .Location = new system.drawing.point (352, 0); this.savebtn.name = "savebtn"; this.savebtn.size = new system.drawing.size (96, 32); this.savebtn.tabindex = 4 ; this.saveBtn.Text = "save interception picture"; this.saveBtn.Click = new System.EventHandler (this.saveBtn_Click); // // aboutBtn // this.aboutBtn.Location = new System.Drawing.Point ( 472, 0); this.aboutbtn.name = "aboutbtn"; this.aboutbtn.size = new system.drawing.size (96, 32); this.aboutbtn.tabindex = 5; this.aboutbtn.text = " "; This.aboutbtn.click = new system.eventhandler (this.aboutbtn_click); // // EXITBTN // this.exitbtn.location = new system.drawing.point (5 92, 0); this.exitbtn.Name = "exitbtn"; this.exitbtn.size = new system.drawing.size (88, 32); this.exitbtn.tabindex = 6; this.exitbtn.text = " "; This.exitbtn.click = new system.eventhandler (this.exitbtn_click); /// Panel1 // this.Panel1.controls.add (this.Mousescreenbtn); this.Panel1.controls.add (this.mousecapturebtn This.Panel1.controls.add (this.clearbtn); this.panel1.controls.add (this.savebtn); this.panel1.controls.add (this.aboutbtn); this.Panel1.controls.add (this .exitbtn; this.panel1.location =

New system.drawing.point (0, 0); this.Panel1.name = "panel1"; this.Panel1.size = new system.drawing.size (680, 32); this.Panel1.tabindex = 7; //// // CaptureScreenForm // this.autoscalebasesize = new system.drawing.size (6, 14); this.Autoscroll = true; this.clientsize = new system.drawing.size (696, 358); this.controls.add (this .panel1); this.Controls.add (this.picbox); this.name = "captureScreenform"; this.text = "intercept screen"; this.Panel1.ResumeLayout (false); this.ResumeLayout (false);} # EndRegion

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (New CapturescreenForm ());

Protected Override Void Onpaint (PainTeventargs E) {base.onpaint (e); if (image! = null) {this.picbox.image = image;} else {this.picbox.Image = null; this.picbox.refresh () }}

/ / Quote API

[System.Runtime.InteropServices.dllimportattribute ("gdi32.dll")] public static extern BOOL Bitblt (INTPTR HDCDEST, / / ​​Target device handle int NXDEST, // target INT NYDEST, // Target The X coordinate int NWIDTH, / / ​​target object's rectangular width int nHEight, // Target object's rectangular length INTPTR HDCSRC, // Source The handle INT NXSRC, // Source object's left upper left corner Coordinate int NYSRC, // The X coordinate system.int32 dwrop // raster is operated;

Private void exitbtn_click (object sender, system.eventargs e) {Application.exit ();

Private Void Aboutbtn_Click (Object Sender, System.EventArgs E) {MessageBox.show ("Code by Star IN 2006-3-17, E_MAIL: YGH_01@163.com, thank you to use this.");}

Private Void Clearbtn_Click (Object Sender, System.EventArgs E) {CaptureScreenform.Image = null; this.invalidate ();

private void saveBtn_Click (object sender, System.EventArgs e) {bool isSave = true; SaveFileDialog saveImageDialog = new SaveFileDialog (); saveImageDialog.Title = "Capture screen image save dialog"; saveImageDialog.Filter = @ "jpeg | * .jpg | BMP | * .BMP | GIF | * .GIF "; if (SaveImageDialog.Showdialog () == DialogResult.ok) {string filename = SaveImageDialog.FileName.toString (); if (filename! =" && filename! = null ) {String fileextName = filename.substring (filename.lastindexof (".") 1) .tostring ();

(! FileExtName = ""); System.Drawing.Imaging.ImageFormat imgformat = null if {switch (fileExtName) {case "jpg": imgformat = System.Drawing.Imaging.ImageFormat.Jpeg; break; case "bmp": imgformat = System.drawing.Imaging.ImageFormat.Bmp; Break; Case "gif": imgformat = system.drawing.image.imageformat.gif; break; default: messagebox.show ("only access to: JPG, BMP, GIF Format "); issave = false; Break;}

}

// Default Save JPG format if (imgformat == null) {imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;} if (isSave) {try {CaptureScreenForm.image.Save (fileName, imgformat); MessageBox.Show ( "The picture has been successfully saved! ~~");} catch {messagebox.show ("Save failed, you have not intercepted the picture or empty the picture!");}}

}

}

private void fullScreenBtn_Click (object sender, System.EventArgs e) {CaptureScreenForm.image = windowFullScreen (); picbox.Image = CaptureScreenForm.image;} public static Bitmap windowFullScreen () {// build screen Graphics Graphics grpScreen = Graphics.FromHwnd (IntPtr .Zero); // build according to the screen size of the bitmap bitmap bitmap = new bitmap (Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, grpScreen); // build bitmap related Graphics Graphics grpBitmap = Graphics.FromImage (Bitmap); // Establish screen context INTPTR HDCSCREEN = Grpscreen.getHDC (); // Setting up bitmap context INTPTR HDCBITMAP = GrPbitMap.GETHDC (); // Save the screen in the map Bitblt (HDCBitmap, 0, 0 Bitmap.width, bitmap.height, hdcscreen, 0,0,0x00cc0020); // Close bitmap handle GrPbitMap.ReleaseHDC (HDCBITMAP); // Close screen handle Grpscreen.ReleaseHDC (hdcscreen); // Release bitmap object Grpbitmap.dispose (); // Release screen object Grpscreen.dispose ();

// Return to the capture bitmap return bitmap;}

Private void mousecapturebtn_click (object sender, system.eventargs e) {mousecaptureform mousecapture = new mousecaptureform (); mousecapture.show ();}}}

Another screenshot file:

Mousecaptureform.cs

Using system; using system.collections; using system.componentmodel; using system.windows.form;

Namespace CaptureScreen {///

/// mousecaptureform's summary description. /// public class mousecaptureform: system.windows.forms.form {/// // The required designer variable. /// // // Custom Variable // Private Pot Pot; Private Rectangle Area = Rectangle.empty; Private Image Img; Private INDEX = -1; Private String NoselectString = "/ n. please press The left button is not dragged to select the screenshot area / n / n. Click the right mouse button or press ESC to cancel screenshot "; private string selectingstring =" / n. Loosen the left mouse button to determine the range / n / n. Press ESC Select "; private string selectedString =" / n. Press the left mouse button to adjust the selection range / n / n. Double-click the left mouse button to save the screenshot / n / n. Right click on the mouse button to reselect /N/n.esc key to cancel screenshot "; private System.Windows.Forms.Label tiptext; // // defines generation system // private System.ComponentModel.Container components = null; public MouseCaptureForm () {this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds; THIS.BACKGROUNDIMAGE = CaptureScreenform.windowFullscreen ();

// /////initiRizeComponent () support for Windows Form Designer Support;

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method. /// private void initializecomponent () {this.tiptext = new system.windows.forms.label (); this.suspendlayout (); /// tiptext // this.tiptext.backcolor = system.drawing . Color.FromargB ((System.byte)) ((192)) ((System.byte)) ((92))); this.tiptext.forecolor = system.drawing.color .White; this.tiptext.location = new system.drawing.point (10, 10); this.tiptext.name = "tiptext"; this.tiptext.size = new system.drawing.size (166, 140); this .tiptext.TabIndex = 0; this.tiptext.MouseMove = new System.Windows.Forms.MouseEventHandler (this.tiptext_MouseMove); // // MouseCaptureForm // this.AutoScaleBaseSize = new System.Drawing.Size (6, 14) ; this.ClientSize = new System.Drawing.Size (456, 320); this.Controls.Add (this.tiptext); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "MouseCaptureForm"; THIS.SHOWINTASKBAR = false; this.StartPosition = system.windows.forms.formStartPosition.windowsdef aultBounds; this.Text = "screenshot Mouse"; this.TopMost = true; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.KeyDown = new System.Windows.Forms.KeyEventHandler (this.MouseCaptureForm_KeyDown); THISTEM.EVENTHANDLER (this.mousecaptureform_load); this.doubleClick = new system.EventHandler (this.mousecaptureform_doubleclick; this.ResumeLayout (false);}

#ndregion

///

/// Reserved mousedown /// /// protected override void onmousedown (MouseEventArgs e) {base.onmousedown (e); {i (this.area == Rectangle.empty && E.Button == MouseButtons.LEFT) {this.tiptext.text = selectingString; this.area.location = new point (ex, EY);} this.pot = New Point (ex, EY); this.index = this .GetSelectedHandle (New Point (EX, EY)); this.setCursor ();}} /// /// Set mouse scheme /// private void setcursor () {Cursor cr = Cursors. Default; if (index == 1 || index == 5) {cr = Cursors.sizenwse;} else if (index == 2 || index == 6) {cr = cursors.sizens;} else} = = 3 || index == 7) {cr = Cursors.SizENESW;} else if (index == 4 || index == 8) {cr = cursors.sizewe;} else if (index == 0) {Cr = Cursors.sizeall;} Cursor.current = Cr;}

Protected Override Void onPaint (Painteventargs E) {base.onpaint (e); E.Graphics.drawRectangle (New Pen (this.Forecolor), this.area); for (int i = 1; i <9; i ) {e .Graphics.FillRectangle (New Solidbrush (Color.green), this.getHandleRect (i));}}

Private Rectangle GetHandleRect (int index) {Point Point = getHandle (index); Return New Rectangle (Point.x - 3, Point.y - 3, 6, 6);

Private point getHandle (int index) {Int x, y, xceenter, ycenter

Xcenter = area.x area.width / 2; ycenter = area.y area.Height / 2; x = area.x; y = area.y;

Switch (index) {casse 1: x = area.x; y = area.y; break; casse 2: x = xcenter; y = area.y; break; casse 3: x = area.right; y = area. Y; Break; Case 4: x = area.right; y = ycenter; break; casse 5: x = area.right; y = area.bottom; break; cas 6: x = xcenter; y = area.bottom; break Case 7: x = area.x; y = area.bottom; break; casser 8: x = area.x; y = ycenter; Break;} return new point (x, y);

Protected Override Void OnMouseUp (MouseEventArgs E) {Base.OnmouseUp (e); int = area.tic; int = area.top; int = area.right; int bottom = area.bottom; area.x = math. MIN (Left, Right); Area.y = Math.min (Top, Bottom); Area.Width = Math.Abs ​​(Left-Right); Area.Height = Math.Abs ​​(Top-Bottom); this.tiptext. TEXT = SELECTEDSTRING; if (e.Button == mousebuttons.right) {if (this.area == Rectangle.empty) {this.close ();} else {this.area = Rectangle.empty; this.invalidate () ;}} This.index = this.getSelectedHandle (New Point (EX, EY)); this.SetCursor ();}

Private int getSelectedHandle (POINT P) {int index = -1; for (int i = 1; i <9; i ) {if (GetHandleRect (i) .contains (p)) {index = i; break;}} (this.area.contains (p)) index = 0;

Return index;}

Protected Override Void OnMouseMove (MouseEventArgs E) {Base.Onmousemove (E); if (this.cape) {this.moveHandleto (New Point (EX, EY)); this.invalidate ();} else {this.index = THIS .GetSelectedHandle (New Point (EX, EY)); this.SetCursor ();}} private void movehandleto (point point) {int = area.LEFT; int top = area.top; int right = area.right; int right = area.right; int Bottom = Area.bottom;

Switch (index) {cas 0: area.x = point.x - this.pot.x; area.y = point.y - pot.y; this.pot = POINT; RETURN; case 1: left = Point .X; Top = Point.y; Break; Case 2: Top = Point.y; Break; Case 3: Right = Point.x; Top = Point.y; Break; Case 4: Right = Point.x; Break; Case 5: Right = Point.x; bottom = point.y; break; cas 6: bottom = point.y; break; case 7: left = point.x; bottom = point.y; break; cas 8: left = Point.x; Break;} this.pot = point; area.x = left; area.wi = top; area.width = right - left; area.height = bottom - top;}

private void MouseCaptureForm_DoubleClick (object sender, System.EventArgs e) {// corrected image taken through the boundary prompt memory BUG int left = area.Left; int right = area.Right; int top = area.Top; int bottom = area. Bottom; if (left Screen.PrimaryScreen.Bounds.Right) {right = Screen.PrimaryScreen.Bounds.Right;} if (top Screen.PrimaryScreen.Bounds.Bottom) {bottom = Screen.PrimaryScreen.Bounds.Bottom;} area. X = left; area.y = top; area.width = Right - left; area.height = bottom - top; // Intercept Select Area Picture Bitmap BM = New Bitmap (this.BackgroundImage); this.img = bm.clone (this.area, system.drawing.Imaging.pixelformat.format16bppargb1555); capturescreenform.image = img; captureScreenform.activeform.INVALIDATE (); this.close ();}

private void MouseCaptureForm_KeyDown (object sender, System.Windows.Forms.KeyEventArgs e) {if (e.KeyData == Keys.Escape && System.Windows.Forms.Button.MouseButtons == MouseButtons.Left) {this.area = Rectangle. Empty; this.invalidate (); tiptext.text = nosectString;} else if (e.keydata == keys.escape) {this.close ();}}

private void tiptext_MouseMove (object sender, System.Windows.Forms.MouseEventArgs e) {if (tiptext.Location.X == 10 && tiptext.Location.Y == 10) {Point formloc = new Point ((Screen.PrimaryScreen.Bounds .Width - 176), 10); tiptext.location = formloc;} else {tiptext.location = new point (10, 10);}}

转载请注明原文地址:https://www.9cbs.com/read-130912.html

New Post(0)