Today, a simple screen function is implemented through C #. Realize the idea, get the mouse down position and the left mouse button to release the position, calculate the width and height of this area. You can then get to the screen with the Graphics.copyfromscreen method.
This function is implemented by C#+WPF, with the following code:
Xaml:
<window x:class= "Wpfcapturescreenshot.capturewindow" xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml /presentation " xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml " title=" CaptureWindow " Allowstransparency= "True" windowstyle= "None" background= "Black" opacity= "0.1" windowstate= " Maximized " mousedown=" Capturewindow_mousedown " mousemove=" Capturewindow_mousemove "> <Canvas X:name= "Capturecanvas" > </Canvas></Window>
Public Partial classCapturewindow:window {Private Doublex; Private Doubley; Private Doublewidth; Private Doubleheight; Private BOOLIsmousedown =false; PublicCaptureWindow () {InitializeComponent (); } Private voidCapturewindow_mousedown (Objectsender, MouseButtonEventArgs e) {Ismousedown=true; X= E.getposition (NULL). X Y= E.getposition (NULL). Y } Private voidCapturewindow_mousemove (Objectsender, System.Windows.Input.MouseEventArgs e) { if(ismousedown) {//1. Use a rectangle to represent the current regionSystem.Windows.Shapes.Rectangle rect =NewSystem.Windows.Shapes.Rectangle (); DoubleDX = e.getposition (NULL). X DoubleDY = e.getposition (NULL). Y DoubleRectwidth = Math.Abs (DX-x); DoubleRectheight = Math.Abs (Dy-y); SolidColorBrush Brush=NewSolidColorBrush (Colors.white); Rect. Width=Rectwidth; Rect. Height=Rectheight; Rect. Fill=Brush; Rect. Stroke=Brush; Rect. StrokeThickness=1; if(DX <x) {canvas.setleft (rect, DX); Canvas.settop (rect, DY); } Else{canvas.setleft (rect, x); Canvas.settop (rect, y); } CaptureCanvas.Children.Clear (); CAPTURECANVAS.CHILDREN.ADD (rect); if(E.leftbutton = =mousebuttonstate.released) {CaptureCanvas.Children.Clear (); //2. Get the current regionwidth = Math.Abs (E.getposition (NULL). Xx); Height= Math.Abs (E.getposition (NULL). Yy); if(E.getposition (NULL). X >x) {capturescreen (x, y, width, height); } Else{capturescreen (e.getposition (NULL). X, E.getposition (NULL). Y, width, height); } Ismousedown=false; X=0.0; Y=0.0; This. Close (); } } } Private voidCapturescreen (DoubleXDoubleYDoubleWidthDoubleheight) { intIX =Convert.ToInt32 (x); intiy =Convert.ToInt32 (y); intIW =Convert.ToInt32 (width); intIH =convert.toint32 (height); System.Drawing.Bitmap Bitmap=NewBitmap (IW, IH); using(System.Drawing.Graphics Graphics =graphics.fromimage (bitmap)) {Graphics. CopyFromScreen (ix, IY,0,0,NewSystem.Drawing.Size (IW, IH)); SaveFileDialog Dialog=NewSaveFileDialog (); Dialog. Filter="Png files|*.png"; if(dialog. ShowDialog () = =System.Windows.Forms.DialogResult.OK) {bitmap. Save (dialog. FileName, imageformat.png); } } }
Operating effect:
Source code can click here to download.
If you have other ways to achieve, welcome to share with me. If you have questions about the code, you can point it out in the comments. Thanks for reading.
C # Screen