Hello Everyone.
In this article: You will know to capture the screen in 2 ways:
1)- Capture full screen.
2)- Capture region.
Let's Begin:
First make a new Windows Application project.
And make sure your program uses these namespaces:
-
using System;
-
using System.Drawing;
-
using System.Drawing.Imaging;
-
using System.Windows.Forms;
-
Now, we will begin writing the code:
1)- Create a new bitmap object.
-
//Bitmap screenBitmap = new Bitmap(int width, int height, PixelFormat);
-
Bitmap screenBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
-
2)- Create a new rectangle object.
-
//screenRegion will grab the size of your current screen.
-
Rectangle screenRegion = Screen.AllScreens[0].Bounds;
-
3)- Create a new graphics object.
-
Graphics screenGraphics = Graphics.FromImage(screenBitmap);
-
4)- Copy the image from the screen.
-
//screenGraphics will copy the image from the screen.
-
screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size);
-
5)- Save the image:
-
screenBitmap.Save(@"C:\Screen.jpg", ImageFormat.Jpeg);
-
Here is the code:
-
Bitmap screenBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
-
Rectangle screenRegion = Screen.AllScreens[0].Bounds;
-
-
// It will copy the current screep image to the bitmap image.
-
Graphics screenGraphics = Graphics.FromImage(screenBitmap);
-
screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size);
-
-
screenBitmap.Save(@"c:\test.jpg", ImageFormat.Jpeg);
-
You can also capture a part of the screen (region):
-
/* The Code was:
-
screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size);
-
*/
-
-
//See the difference.
-
-
//screenGraphics.CopyFromScreen(intSourceX, intSourceY......);
-
screenGraphics.CopyFromScreen(200, 300, 0, 0, screenRegion.Size);
-
Hope this help you.