How to capture a screen using c#
Create a new “Windows Forms application” in Visual Studio and name it as you choose (I here named it ScreenCaptureDemo).
Now go to the toolbox and add a Button Control to the project also resize the window size.
Add the following using directives:
using System.Drawing.Imaging;
using System.Drawing;
using System;
2
3
- Now navigate to the code file Form1.cs and the following code in the Button Event Handler that we added to our project.
private void buttonCapture_Click(object sender, EventArgs e)
{
//Creating a Method name CaptureMyScreen
CaptureMyScreen();
}
private void CaptureMyScreen()
{
}
2
3
4
5
6
7
8
- Now add the following code into the method that we created (CaptureMyScreen()):
private void CaptureMyScreen()
{
try
{
//Creating a new Bitmap object
Bitmap captureBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
//Bitmap captureBitmap = new Bitmap(int width, int height, PixelFormat);
//Creating a Rectangle object which will
//capture our Current Screen
Rectangle captureRectangle = Screen.AllScreens[0].Bounds;
//Creating a New Graphics Object
Graphics captureGraphics = Graphics.FromImage(captureBitmap);
//Copying Image from The Screen
captureGraphics.CopyFromScreen(captureRectangle.Left,captureRectangle.Top,0,0,captureRectangle.Size);
//Saving the Image File (I am here Saving it in My E drive).
captureBitmap.Save(@"E:\Capture.jpg",ImageFormat.Jpeg);
//Displaying the Successfull Result
MessageBox.Show("Screen Captured");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- Now compile it and run it. You will get your captured screen.
Create your own Snipping Tool
Introduction
In this article I will show you how to create your own Snipping Tool. This tool captures a screenshot of anything on your desktop, like a picture or a section of webpage. This tool allows you to select a partial section of the whole screen.
Background
Before Snipping Tool in Windows 7, the common way to edit snap shoot was to press Print Screen key, open paint tool (or any other image editor), crop the region and copy it. With this tool the need for image editor is eliminated. This small project can be used like base for other projects like OCR or image processor as well.
Using the code
First we must to set the form properties. That can be done manually or by code
- BackColor - To whatever color you prefer displayed in areas not covered by the image itself. I prefer Black.
- FormBorderStyle - None
- StartPosition - Manual
- Top - 0
- Left - 0
- WindowState - Maximized (strange that this is required. If you don't set it the form doesn't go completely full screen).
Add a PictureBox control to the form. Set these properties:
- Location - 0, 0
- SizeMode - Zoom (ensure the image is displayed in the proper aspect ratio)
Set this code into form load event:
//Hide the Form
this.Hide();
//Create the Bitmap
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
//Create the Graphic Variable with screen Dimensions
Graphics graphics = Graphics.FromImage(printscreen as Image);
//Copy Image from the screen
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
//Create a temporal memory stream for the image
using (MemoryStream s = new MemoryStream())
{
//save graphic variable into memory
printscreen.Save(s, ImageFormat.Bmp);
pictureBox1.Size = new System.Drawing.Size(this.Width, this.Height);
//set the picture box with temporary stream
pictureBox1.Image = Image.FromStream(s);
}
//Show Form
this.Show();
//Cross Cursor
Cursor = Cursors.Cross;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Now you must declare some private variables for mouse location and draw crop area:
//These variables control the mouse position
int selectX;
int selectY;
int selectWidth;
int selectHeight;
public Pen selectPen;
//This variable control when you start the right click
bool start = false;
2
3
4
5
6
7
8
9
Mouse down and mouse move events will be used for create this effects. This is the code for mouse move event:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
//validate if there is an image
if (pictureBox1.Image == null)
return;
//validate if right-click was trigger
if(start)
{
//refresh picture box
pictureBox1.Refresh();
//set corner square to mouse coordinates
selectWidth = e.X - selectX;
selectHeight = e.Y - selectY;
//draw dotted rectangle
pictureBox1.CreateGraphics().DrawRectangle(selectPen,
selectX, selectY, selectWidth, selectHeight);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
And this is for mouse down event:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//validate when user right-click
if (!start)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//starts coordinates for rectangle
selectX = e.X;
selectY = e.Y;
selectPen = new Pen(Color.Red, 1);
selectPen.DashStyle = DashStyle.DashDotDot;
}
//refresh picture box
pictureBox1.Refresh();
//start control variable for draw rectangle
start = true;
}
else
{
//validate if there is image
if (pictureBox1.Image == null)
return;
//same functionality when mouse is over
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Refresh();
selectWidth = e.X - selectX;
selectHeight = e.Y - selectY;
pictureBox1.CreateGraphics().DrawRectangle(selectPen, selectX,
selectY, selectWidth, selectHeight);
}
start = false;
//function save image to clipboard
SaveToClipboard();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
The mouse move event had the variables selectX and selectY. They will save the initial mouse position after mouse down event set the Boolean variable start in true or false that means when start the cropping action or ends respectively with help of event arg we can easily determine the rectangles width and height.
private void SaveToClipboard()
{
//validate if something selected
if (selectWidth > 0)
{
Rectangle rect = new Rectangle(selectX, selectY, selectWidth, selectHeight);
//create bitmap with original dimensions
Bitmap OriginalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
//create bitmap with selected dimensions
Bitmap _img = new Bitmap(selectWidth, selectHeight);
//create graphic variable
Graphics g = Graphics.FromImage(_img);
//set graphic attributes
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel);
//insert image stream into clipboard
Clipboard.SetImage(_img);
}
//End application
Application.Exit();
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
The SaveToClipboard function saves into clipboard memory what the object picture box has in its image buffer and set all the property like size and quality.

评论
发表评论