How to take print screen using C#

No.of Views2229
Bookmarked1 times
Downloads 
Votes0
By  NikhilJohari   On  16 Feb 2010 03:02:18
Tag : CSharp , How to
How to take print screen using C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Background

I 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.

Image Loading

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

 

Image Loading

I used keyboard button because it dosen’t allow to take its picture 

Code Snippet

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

 

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

 
Sign Up to vote for this article
 
About Author
 
NikhilJohari
Occupation-
Company-
Member Type-Senior
Location-India
Joined date-15 Aug 2009
Home Page-http://fast-get.com
Blog Page-http://dotnetask.blog.co.in
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top