C # to achieve a similar QQ screen capture program _c# tutorial

Source: Internet
Author: User
Because I recently wanted to write a program similar to Remote Desktop monitoring, screen capture is used in this program. To achieve the program's part of the function, made a small demo. The program is very simple, the technology used is not much, can only achieve similar QQ screenshot function (although the method is very stupid)
The program flow is as follows:

1. Intercept the entire screen and save
2. Open a new Full-screen window, the saved screen as the background
3. Drag the mouse to change the scope of interception, right key cancellation
4. Double click Intercept, save on Pasteboard, full screen window closed

Okay, here's the Code section.

First create a new project Screencutter (VS2005), change the form name to MainForm, and then create a new form screenbody.
Add a button btncutter to Screencutter and add a button event:
Private voidBtncutter_click (Objectsender, EventArgs e)
{
Image img= NewBitmap (screen.allscreens[0]. Bounds.width, screen.allscreens[0]. Bounds.height);
Graphics g= graphics.fromimage (IMG);
G.copyfromscreen (new Point (0, 0), new Point (0, 0), screen.allscreens[0]. Bounds.size);
Screenbody Body = new screenbody ();
Body. BackgroundImage = img;
Body. Show ();
}
Screen.allscreens[0] is the first to get all the current device Windows, I have only one monitor here, of course I am the first one.
Use the graphics CopyFromScreen function to get the current screen.

OK, now press the button and the Full screen window will come out.

The Full screen window screenbody, first set the form FormBorderStyle to none, and then declare the following variables
PrivateGraphics Mainpainter; Main brushes
PrivatePen pen; It's a pen.
Private bool isdowned; Determine if the mouse is pressed
Private bool rectready; Whether the rectangle is drawn complete
Private Image baseimage; Basic graphics (original picture)
Private Rectangle Rect; is the rectangle to be saved
Private point downpoint; Mouse Down Point
int tmpx;
int tmpy;

After that is the form of the mouse function, which many of the code did not make a collation, looked at, the collation of the code should be less and more concise



Private voidScreenbody_doubleclick (Objectsender, EventArgs e)
{
If(((MouseEventArgs) e). Button==MouseButtons.Left&&Rect.contains (((MouseEventArgs) e). X, ((MouseEventArgs) e). Y))
{
There are many ways to save it ... I only use this here.
Image Memory= NewBitmap (Rect.width, rect.height);
Graphics g=Graphics.fromimage (memory);
G.copyfromscreen (rect.x+ 1, Rect.y+ 1,0,0, rect.size);
Clipboard.setimage (memory);
This. Close ();
}
}

Private voidScreenbody_mousedown (Objectsender, MouseEventArgs e)
{
If(E.button==MouseButtons.Left)
{
isdowned= True;

If(Rectready== False)
{
Rect.x=e.x;
Rect.y=E.y;
Downpoint= NewPoint (e.x, e.y);
}
If(Rectready== True)
{
Tmpx=e.x;
Tmpy=E.y;
}
}
If(E.button==Mousebuttons.right)
{
If(Rectready!= True)
{
This. Close ();
Return;
}
Mainpainter.DrawImage (Baseimage,0,0);
Rectready= False;
}

}

Private voidScreenbody_mouseup (Objectsender, MouseEventArgs e)
{
If(E.button==MouseButtons.Left)
{
isdowned= False;
Rectready= True;
}
}

Private voidScreenbody_mousemove (Objectsender, MouseEventArgs e)
{

If(Rectready== False)
{
If(isdowned== True)
{
Image New=Drawscreen (Image) Baseimage.clone (), e.x, e.y);
Mainpainter.drawimage (New,0,0);
New.dispose ();
}
}
If(Rectready== True)
{
If(Rect.contains (e.x, e.y))
{
//This. Cursor = Cursors.hand;
If(isdowned== True)
{
//Compare the previous position to get the offset
Rect.x=Rect.x+E.x-TMPX;
Rect.y=Rect.y+E.y-Tmpy;
//Record the current position
Tmpx=e.x;
Tmpy=E.y;
Moverect ((Image) Baseimage.clone (), Rect);
}
}
}

}

Private void screenbody_load (object sender, EventArgs e)
{
this. WindowState = formwindowstate.maximized;
Mainpainter = this . CreateGraphics ();
Pen = new pen (brushes.blue);
isdowned = false;
Baseimage = this . BackgroundImage;
Rect = new Rectangle ();
Rectready = false;
}


Auxiliary functions
Should write more auxiliary functions, the form response function inside the code into the inside, but I am very lazy, it will be done. hehe


Private voidDrawRect (Graphics painter,IntMouse_x,Intmouse_y)
{
IntWidth= 0;
IntHeigth= 0;
If(mouse_y<RECT.Y)
{
Rect.y=mouse_y;
Heigth=Downpoint.y-mouse_y;
}
Else
{
Heigth=Mouse_y-Downpoint.y;
}
If(mouse_x<Rect.x)
{
Rect.x=mouse_x;
Width=Downpoint.x-mouse_x;
}
Else
{
Width=Mouse_x-Downpoint.x;
}
Rect.size= NewSize (width, heigth);
Painter.drawrectangle (pen, Rect);
}

Private Image Drawscreen (image back, int mouse_x, int mouse_y)
{
Graphics painter = graphics.fromimage (back);
DrawRect (painter, mouse_x, mouse_y);
Return to back ;
}
Private void moverect (image image, Rectangle Rect)
{
Graphics painter = graphics.fromimage (image);
Painter.drawrectangle (pen, Rect.x, Rect.y, Rect.width, rect.height);
Drawrects (painter);
Mainpainter.drawimage (image, 0, 0);
Image. Dispose ();
}


Here, the code, even if it's finished, runs



The interception of the result, there is no control of the boundary, so there are boundaries can be seen, a little set up on it



All right, this thing is finished, next to make use of the hook .... Hope to finish quickly, tired Ah ~ ~ ~ ~ ~
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.