C # Development Example-Custom screen capture tool (iv) Basic screenshot function Implementation code

Source: Internet
Author: User

Implementation principle

The basic function is to respond to the main form of the mouse press, mouse movement, the mouse raised several events function to achieve. The captured picture area is displayed using the label component, and the Paint method of the label component needs to be re-implemented.

Left-click to start, right-click Cancel, double click left mouse button to finish, save the captured picture to the Windows Clipboard.

Add the Label component

Toolbox Common Components Double-click the Label component to modify the properties of the component:

Name=lbl_cutimage,

Autosize=false,

Backcolor=transparent,

Text = ""

"Form1_Load" event add code:

This.lbl_CutImage.Hide ();

Defining basic variables for functional dependencies

        #region Basic variables///<summary>////        for determining whether the information box is displayed or not. ///        </summary>        private bool iscuting;        <summary>///        mouse pressed points///        </summary>        private point beginpoint;        <summary>/////        </summary> Private Point endPoint of the final set of drawing points        ;        <summary>        ////To record the size of the display area (including the area of the adjustment block, adjust the area border width 2px)///        </summary>        Private Rectangle Cutimagerect = new Rectangle (0, 0, 5, 5);        #endregion

Defining enumeration types: Updating the mode of the UI

        <summary>///        Update UI mode to mark what needs to be displayed and what needs to be hidden;///        </summary>        [FlagsAttribute]        public Enum Updateuimode:uint        {            //It is important to note that if you want to use a combined value, you cannot use a concatenated number to indicate that it must be a geometry-level growth!            None = 0,            Showtextpro = 1,            Showpenstyle = 2,            showtoolbox = 4,            Showinfobox = 8,            Showzoombox = +,            Showcutimage = +,            Hidetextpro = +,            hidepenstyle = $,            Hidetoolbox =,            Hideinfobox =        

Add method: Calculate and save the size of the area box

        <summary>        ////calculate and save the size of the area box        ///</summary> private void savecutimagesize (Point beginpoint , point EndPoint)        {            //Save the final drawing base to intercept the selected area            this.endpoint = beginpoint;            Calculate the size of the captured picture            int imgwidth = Math.Abs (endpoint.x-beginpoint.x) + 1;            int imgheight = Math.Abs (ENDPOINT.Y-BEGINPOINT.Y) + 1;            int lblwidth = imgwidth + 4;            int lblheight = imgheight + 4;            Set the location and size of the area            this.cutimagerect = new Rectangle (beginpoint.x-2, Beginpoint.y-2, Lblwidth, lblheight);        }

Add method: Execute to save a picture of the selected area to the Clipboard

        <summary>////Execute, save picture of selected area to Clipboard///</summary>//<param name= "Savetodisk ">//whether to save the picture to disk///</param> private void Execcutimage (bool savetodisk, BOOL uploadimage) BOOL Savetodisk = FALSE, bool Uploadimage = false {//if the picture capture area is not visible, exit the Save Picture Procedure if (!this.lbl_            cutimage.visible) {return;}            Rectangle srcrect = new Rectangle ();            Srcrect.x = this.lbl_cutimage.location.x + 2;            Srcrect.y = this.lbl_cutimage.location.y + 2;            Srcrect.width = this.lbl_cutimage.width-4;            Srcrect.height = this.lbl_cutimage.height-4;            Rectangle destrect = new Rectangle (0, 0, srcrect.width, srcrect.height);            Bitmap bmp = New Bitmap (srcrect.width, srcrect.height);            Graphics g = graphics.fromimage (BMP);            G.drawimage (This.screenimage, Destrect, Srcrect, GraphicsUnit.Pixel);            Clipboard.setimage (BMP);Exitcutimage (TRUE); }

Add Method: Exit procedure

       <summary>        ///exit process///</summary> private void Exitcutimage (bool Hidewindow)//  = True        {            this.lbl_CutImage.Visible = false;            This.iscuting = false;            if (Hidewindow)            {                this.screenImage.Dispose ();                This. Hide ();            }        }

Main window mouse down event handler

        <summary>///Windows mouse down event handlers///</summary>//<param name= "Sender" >< /param>//<param name= "E" ></param> private void Form1_mousedown (object sender, Mouseeventar                GS e) {//left-click event if (E.button = = MouseButtons.Left && E.clicks = = 1) {                    if (!this.lbl_cutimage.visible) {this.iscuting = true;                    This.beginpoint = e.location;                    This.endpoint = e.location;                    Savecutimagesize (E.location, e.location); Updatecutinfolabel (Updateuimode.showcutimage |                Updateuimode.showinfobox);                }}//left-click event if (E.button = = MouseButtons.Left && E.clicks = = 2) {                if (this.lbl_CutImage.Visible) {Execcutimage (false, false);       }            }     Right-click the event if (E.button = = mousebuttons.right) {exitcutimage (!this.lbl_cutimage.            Visible); }        }

main Window Mouse move event handler

        <summary>///Window mouse movement event handlers///</summary>//<param name= "Sender" >< /param>//<param name= "E" ></param> private void Form1_mousemove (object sender, Mouseeventar GS e) {//If the Intercept area is not visible, exit the process if (!this.lbl_cutimage.visible) {UPDA                Tecutinfolabel (Updateuimode.none);            Return            } Point pntbgn = This.beginpoint;            Point pntend = e.location;            If you are dragging backwards, reset the starting point if (e.location.x < this.beginpoint.x && E.LOCATION.Y < THIS.BEGINPOINT.Y)                {pntbgn = e.location;            Pntend = This.beginpoint;                    } else {if (E.location.x < this.beginpoint.x) {                    PNTBGN = new Point (e.location.x, THIS.BEGINPOINT.Y);     Pntend = new Point (This.beginpoint.x, E.LOCATION.Y);           } else {if (E.location.y < THIS.BEGINPOINT.Y)                        {pntbgn = new Point (This.beginpoint.x, E.LOCATION.Y);                    Pntend = new Point (e.location.x, THIS.BEGINPOINT.Y); }}} if (this.iscuting) {savecutimagesize (PNTBGN, Pntend)            ;        } updatecutinfolabel (Updateuimode.none); }

Main Window Mouse Lift event handler

        <summary>///        window Mouse Lift event handlers///</summary>//        <param name= "Sender" ></param>        //<param name= "E" ></param>        private void Form1_mouseup (object sender, MouseEventArgs e)        {            if (E.button = = MouseButtons.Left)            {                if (this.iscuting)                {                    this.iscuting = false;                    Updatecutinfolabel (Updateuimode.none);}}}        

Capture area picture Drawing

        <summary>///Draw event handler for image of area////</summary>//<param name= "Sender" >& lt;/param>//<param name= "E" ></param> private void Lbl_cutimage_paint (object sender, Painte            Ventargs e) {int imgwidth = this.lbl_cutimage.width-4;            int imgheight = this.lbl_cutimage.height-4;            if (ImgWidth < 1) {imgwidth = 1;}            if (ImgHeight < 1) {imgheight = 1;} Create cached images, draw all the content to be drawn to the cache, and then draw to the Label once,//This can improve performance, and can prevent screen flicker problem Bitmap bmp_lbl = new Bitmap (            This.lbl_CutImage.Width, This.lbl_CutImage.Height);            Graphics g = graphics.fromimage (BMP_LBL);            The part to be intercepted is drawn to the cache Rectangle Destrect = new Rectangle (2, 2, ImgWidth, imgheight);            Point srcpoint = this.lbl_CutImage.Location;            Srcpoint.offset (2, 2); Rectangle srcrect = new Rectangle (srcpoint, New System.Drawing.Size (ImgWidth, imgHeight));            G.drawimage (This.screenimage, Destrect, Srcrect, GraphicsUnit.Pixel);            SolidBrush brush = new SolidBrush (Color.FromArgb (10, 124, 202));            Pen pen = new Pen (brush, 1.0F); The following sections (borders and adjustment blocks) are drawn after (edited) to resolve issues where the drawing edits overwrite (borders and adjustment blocks)//areas outside the draw border to resolve issues that will be overwritten by the edits//top dest            Rect = new Rectangle (0, 0, this.lbl_CutImage.Width, 2);            Srcpoint = this.lbl_CutImage.Location;            Srcpoint.offset (2, 2);            Srcrect = new Rectangle (srcpoint, New System.Drawing.Size (This.lbl_CutImage.Width, 2)); G.drawimage (this.            BackgroundImage, Destrect, Srcrect, GraphicsUnit.Pixel);            Bottom Destrect = new Rectangle (0, This.lbl_cutimage.height-2, This.lbl_CutImage.Width, 2);            Srcpoint = this.lbl_CutImage.Location;            Srcpoint.offset (0, this.lbl_cutimage.height-2);       Srcrect = new Rectangle (srcpoint, New System.Drawing.Size (This.lbl_CutImage.Width, 2));     G.drawimage (this.            BackgroundImage, Destrect, Srcrect, GraphicsUnit.Pixel);            Left destrect = new Rectangle (0, 2, 2, this.lbl_cutimage.height-4);            Srcpoint = this.lbl_CutImage.Location;            Srcpoint.offset (0, 2);            Srcrect = new Rectangle (srcpoint, New System.Drawing.Size (2, this.lbl_cutimage.height-4)); G.drawimage (this.            BackgroundImage, Destrect, Srcrect, GraphicsUnit.Pixel);            Right destrect = new Rectangle (This.lbl_cutimage.width-2, 2, 2, this.lbl_cutimage.height-4);            Srcpoint = this.lbl_CutImage.Location;            Srcpoint.offset (This.lbl_cutimage.width-2, 2);            Srcrect = new Rectangle (srcpoint, New System.Drawing.Size (2, this.lbl_cutimage.height-4)); G.drawimage (this.            BackgroundImage, Destrect, Srcrect, GraphicsUnit.Pixel);            Draw Border G.drawline (pen, 2, 2, this.lbl_cutimage.width-3, 2); G.drawline (pen, 2, 2, 2, This.lbl_cutimagE.HEIGHT-3);            G.drawline (pen, this.lbl_cutimage.width-3, 2, this.lbl_cutimage.width-3, this.lbl_cutimage.height-3);            G.drawline (pen, 2, this.lbl_cutimage.height-3, this.lbl_cutimage.width-3, this.lbl_cutimage.height-3);            Draw Four corners of the adjustment block G.fillrectangle (brush, 0, 0, 4, 5);            G.fillrectangle (Brush, this.lbl_cutimage.width-4, 0, 4, 5);            G.fillrectangle (brush, 0, This.lbl_cutimage.height-5, 4, 5);            G.fillrectangle (Brush, This.lbl_cutimage.width-4, This.lbl_cutimage.height-5, 4, 5);            Draw the middle of the four adjustment blocks int blockx = THIS.LBL_CUTIMAGE.WIDTH/2-2;            int blocky = THIS.LBL_CUTIMAGE.HEIGHT/2-2;            G.fillrectangle (Brush, Blockx, 0, 4, 5);            G.fillrectangle (brush, 0, blocky, 4, 5);            G.fillrectangle (Brush, Blockx, This.lbl_cutimage.height-5, 4, 5);            G.fillrectangle (Brush, This.lbl_cutimage.width-4, blocky, 4, 5);  Draw to Label          E.graphics.drawimage (bmp_lbl, 0, 0); Bmp_lbl.        Dispose (); }

Double click the left mouse button to complete the function

        <summary>////        mouse down event handler for image Capture////</summary>//        <param name= "Sender" ></ Param>        //<param name= "E" ></param>        private void Lbl_cutimage_mousedown (object sender, MouseEventArgs e)        {            //left button double-click event            if (E.button = = MouseButtons.Left && E.clicks = = 2)            {                if ( this.lbl_CutImage.Visible)                {                    Execcutimage (false, False);                }            }        }

Note: The code is all done, so don't forget to bind the event handler for the form or component;

For example, the mouse down event handler "Lbl_cutimage_mousedown" that intercepts an area picture is the handler for the "MouseDown" event of the "Lbl_cutimage" component, and the binding method reference:


To this, the basic function implementation has been implemented, quickly to intercept a picture, paste into the QQ Chat window to see it.

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.