Recently I want to write a program similar to remote desktop monitoring, which uses screen capture. A small DEMO is provided to implement some functions of the program. the program is very simple, and there are not many technologies used. It can only implement functions similar to qq (although stupid)
The procedure is as follows:
1. Capture and save the entire Screen
2. Open a new full screen window with the saved screen as the background
3. Drag the mouse to change the screenshot range, right-click to cancel
4. Double-click the screenshot and save it on the clipboard. The full screen window is closed.
Okay, the following is the code section.
First create a 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 void btnCutter_Click (object sender, EventArgs e)
{
Image img = new Bitmap (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 obtain all the current device windows. I only have one monitor, and of course I am the first one.
Use the CopyFromScreen function of Graphics to obtain the current screen.
Now, press the button to display the full screen window.
Next we will talk about ScreenBody in the full screen window. First, set FormBorderStyle of the form to None, and then declare the following variables.
Private Graphics MainPainter; // master brush
Private Pen pen; // a Pen.
Private bool isDowned; // determines whether the mouse is pressed
Private bool RectReady; // specifies whether the rectangle is drawn.
Private Image baseImage; // basic Image (original Image)
Private Rectangle Rect; // indicates the Rectangle to be saved.
Private Point downPoint; // the Point at which the cursor is clicked.
Int tmpx;
Int tmpy;
The mouse function of the form is followed, and a lot of code is not sorted out. After reading it, the sorted code should be less streamlined.
Private void ScreenBody_DoubleClick (object sender, EventArgs e)
{
If (MouseEventArgs) e). Button = MouseButtons. Left & Rect. Contains (MouseEventArgs) e). X, (MouseEventArgs) e). Y ))
{
// There are many methods to save... I only use this method here
Image memory = new Bitmap (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 void ScreenBody_MouseDown (object sender, MouseEventArgs e)
{
If (e. Button = MouseButtons. Left)
{
IsDowned = true;
If (RectReady = false)
{
Rect. X = e. X;
Rect. Y = e. Y;
DownPoint = new Point (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 void ScreenBody_MouseUp (object sender, MouseEventArgs e)
{
If (e. Button = MouseButtons. Left)
{
IsDowned = false;
RectReady = true;
}
}
Private void ScreenBody_MouseMove (object sender, 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 with the last position to obtain the offset
Rect. X = Rect. X + e. X-tmpx;
Rect. Y = Rect. Y + e. Y-tmpy;
// Record the current location
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
I should have written more auxiliary functions and put the code in the form response function into it, but I am very lazy, so I will.
Private void DrawRect (Graphics Painter, int Mouse_x, int Mouse_y)
{
Int width = 0;
Int heigth = 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 = new Size (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 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 ();
}
At this point, even if the code is finished, run
The screenshot result. The screenshot boundary is not well controlled, so you can see the boundary. Just set it a bit.
Okay, this stuff is done like this. Next we need to use hooks... hope we can finish it quickly. Tired ~~~~