.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

TeboScreen

Posted By:Nikhil Kumar       Posted Date: January 12, 2010    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  

I have seen many times that we used CTRL+PRINT+SCREEN buttons 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 a good solution.
 

Background

I have seen many times that we used CTRL+PRINT+SCREEN buttons 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 a good solution.

Solution

To make this process simpler, here is a small tool which helps you to capture the whole screen and a specific are at run time.

Interface



I used keyboard button because it doesn't allow to take its picture.

Coding

Here is the coding of Mouse Event which used in this tool.

public 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
}
}


Download the attached project for more details. Project is an open source project. Feel free to make changes to the project and upload it back here to share with other developers.

Thanks !
Nikhil Kumar

 Subscribe to Articles

     

Further Readings:

    Responses

    No response found. Be the first to respond this post

    Post Comment

    You must Sign In To post reply
    Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend