Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using S10806Class;
Using System. IO;
Namespace GetImage
{
Public partial class GetScreenImgForm: Form
{
/// <Summary>
/// Image operations
/// </Summary>
ImageWork imageWork = new ImageWork ();
/// <Summary>
/// Save the captured desktop image
/// </Summary>
Image screenImg = null;
/// <Summary>
/// Save the image area captured by the user
/// </Summary>
Rectangle imgRect = new Rectangle ();
/// <Summary>
/// Form's GDI
/// </Summary>
Graphics g = null;
/// <Summary>
/// Area border paint brush
/// </Summary>
Pen pen = new Pen (Color. Black, 1 );
/// <Summary>
/// Whether the area should be drawn
/// </Summary>
Bool isDraw = false;
/// <Summary>
/// Whether the user has selected a region
/// </Summary>
Bool isOption = false;
Public GetScreenImgForm ()
{
InitializeComponent ();
// Obtain the current desktop
ScreenImg = imageWork. GetScreenImage ();
G = this. CreateGraphics ();
}
Private void GetScreenImgForm_Load (object sender, EventArgs e)
{
This. Size = new Size (screenImg. Width, screenImg. Height );
This. BackgroundImage = screenImg;
}
Private void GetScreenImgForm_MouseUp (object sender, MouseEventArgs e)
{
// No and right-click to close this form
If (e. Button = MouseButtons. Right &&! This. isOption)
{
This. Close ();
}
// And right-click to cancel this operation
Else if (e. Button = MouseButtons. Right & this. isOption)
{
G. DrawImage (screenImg, new Point (0, 0 ));
This. isOption = false;
Return;
}
// In this case, release the left button, select the region, and wait for double-click to intercept
Else if (e. Button = MouseButtons. Left & this. isOption & this. isDraw)
{
This. isDraw = false;
ImgRect. Width = e. X-imgRect. X; // region length
ImgRect. Height = e. Y-imgRect. Y; // region Height
}
}
Private void GetScreenImgForm_MouseDown (object sender, MouseEventArgs e)
{
If (e. Button! = MouseButtons. Left) return;
If (this. isOption) return;
ImgRect. X = e. X; // start X axis
ImgRect. Y = e. Y; // start Y axis
This. isDraw = true; // allow plotting
}
Private void GetScreenImgForm_MouseMove (object sender, MouseEventArgs e)
{
If (isDraw)
{
// Refresh the image
G. DrawImage (screenImg, new Point (0, 0 ));
This. isOption = true;
Point p1 = new Point (imgRect. X, imgRect. Y );
Point p2 = new Point (e. X, imgRect. Y );
Point p3 = new Point (imgRect. X, e. Y );
Point p4 = new Point (e. X, e. Y );
// Draw four borders
G. DrawLine (pen, p1, p2 );
G. DrawLine (pen, p2, p4 );
G. DrawLine (pen, p4, p3 );
G. DrawLine (pen, p3, p1 );
}
Else
{
// Determine the user's selected region
Int top = imgRect. Y;
Int down = imgRect. Y + imgRect. Height;
Int left = imgRect. X;
Int right = imgRect. X + imgRect. Width;
// The mouse changes in the selected area
If (e. X> left & e. X <right & e. Y> top & e. Y <down)
{
This. Cursor = Cursors. SizeAll;
}
Else
{
This. Cursor = Cursors. Cross;
}
}
}
Private void GetScreenImgForm_MouseDoubleClick (object sender, MouseEventArgs e)
{
Try
{
// When you double-click to select a region .....
If (this. Cursor = Cursors. SizeAll)
{
Bitmap B = new Bitmap (this. screenImg );
Bitmap img = B. Clone (this. imgRect, System. Drawing. Imaging. PixelFormat. DontCare );
SaveFileDialog dlg = new SaveFileDialog ();
Dlg. Filter = "(*. jpg) | *. jpg ";
If (dlg. ShowDialog ()! = DialogResult. OK) return;
Img. Save (dlg. FileName );
This. Close ();
}
}
Catch (Exception err)
{
MessageBox. Show (err. Message );
}
}
}
} <Collapse
Qing · Jing · Xin
|2009-07-21
10