BackgroundI have seen many times that we used following this button many times when we have to capture an image and after capturing the image we paste this in MS Paint and cut the area which is required but it’s a big process do to and some times this is not fixed. Solution So for removing this problem here is a small tool which helps you to capture the whole screen and a specific are at run time, it’s just a smiplest way too. Tool Screen I used keyboard button because it dosen’t allow to take its picture Code Snippetpublic partial class Form1 : Form
{
#region:::::::::::::::::::::::::::::::::::::::::::Form level declarations:::::::::::::::::::::::::::::::::::::::::::
public bool LeftButtonDown = false;
string ScreenPath;
public Point ClickPoint = new Point();
public Point CurrentTopLeft = new Point();
public Point CurrentBottomRight = new Point();
Graphics g;
Pen MyPen = new Pen(Color.Blue, 1);
Pen EraserPen = new Pen(Color.FromArgb(255, 255, 192), 1);
private Form m_InstanceRef = null;
public Form InstanceRef
{
get
{
return m_InstanceRef;
}
set
{
m_InstanceRef = value;
}
}
#endregion
#region:::::::::::::::::::::::::::::::::::::::::::Mouse Event Handlers & Drawing Initialization:::::::::::::::::::::::::::::::::::::::::::
public Form1()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(mouse_Click);
this.MouseUp += new MouseEventHandler(mouse_Up);
this.MouseMove += new MouseEventHandler(mouse_Move);
MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
g = this.CreateGraphics();
}
#endregion
#region:::::::::::::::::::::::::::::::::::::::::::Exit Button:::::::::::::::::::::::::::::::::::::::::::
private void button1_Click(object sender, EventArgs e)
{
Close();
}
#endregion
#region:::::::::::::::::::::::::::::::::::::::::::Mouse Buttons:::::::::::::::::::::::::::::::::::::::::::
private void mouse_Click(object sender, MouseEventArgs e)
{
g.Clear(Color.FromArgb(255, 255, 192));
LeftButtonDown = true;
ClickPoint = new Point(System.Windows.Forms.Control.MousePosition.X, System.Windows.Forms.Control.MousePosition.Y);
}
private void mouse_Up(object sender, MouseEventArgs e)
{
LeftButtonDown = false;
saveFileDialog1.DefaultExt = "bmp";
saveFileDialog1.Filter = "bmp files (*.bmp)|*.bmp";
saveFileDialog1.Title = "Save screenshot to...";
saveFileDialog1.ShowDialog();
ScreenPath = saveFileDialog1.FileName;
if ("" != ScreenPath)
{
SaveScreen();
}
this.Hide();
ControlPanel controlpanel = new ControlPanel();
controlpanel.InstanceRef = this;
controlpanel.Show();
}
#endregion
#region:::::::::::::::::::::::::::::::::::::::::::Drawing the rectangular selection window:::::::::::::::::::::::::::::::::::::::::::
private void mouse_Move(object sender, MouseEventArgs e)
{
//Resize (actually delete then re-draw) the rectangle if the left mouse button is held down
if (LeftButtonDown)
{
//Erase the previous rectangle
g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y, CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - CurrentTopLeft.Y);
//Calculate X Coordinates
if (Cursor.Position.X < ClickPoint.X)
{
CurrentTopLeft.X = Cursor.Position.X;
CurrentBottomRight.X = ClickPoint.X;
}
else
{
CurrentTopLeft.X = ClickPoint.X;
CurrentBottomRight.X = Cursor.Position.X;
}
//Calculate Y Coordinates
if (Cursor.Position.Y < ClickPoint.Y)
{
CurrentTopLeft.Y = Cursor.Position.Y;
CurrentBottomRight.Y = ClickPoint.Y;
}
else
{
CurrentTopLeft.Y = ClickPoint.Y;
CurrentBottomRight.Y = Cursor.Position.Y;
}
//Draw a new rectangle
g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y, CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - CurrentTopLeft.Y);
}
}
#endregion
#region:::::::::::::::::::::::::::::::::::::::::::SaveScreen:::::::::::::::::::::::::::::::::::::::::::
private void SaveScreen()
{
Point StartPoint = new Point(CurrentTopLeft.X, CurrentTopLeft.Y);
Rectangle bounds = new Rectangle(CurrentTopLeft.X, CurrentTopLeft.Y, CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - CurrentTopLeft.Y);
ScreenShot.CaptureImage(StartPoint, Point.Empty, bounds, ScreenPath);
}
#endregion
}
}
That's all, very simple code and easy to understand every lines. In above i'm using GDI++ to produce the image from graphic object. Sample Project Source Download source files -51 kb |