Introduction
In this blog we will discuss how take screenshot of desktop using Windows Application.
Let's see how it can be implemented:
Step 1: Add the following namespaces which helps for generating image:
- using System.Windows.Forms;
- using System.Drawing.Imaging;
Step 2: Add the following code to capture the screenshot and save it to drive:
- Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
- Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
- graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
- bitmap.Save(@"D:\MyDemo\Reports\ScreenShot.bmp", ImageFormat.Jpeg);
In preceding code:
Line 1: It is fetching computer screen height and width.
Line 2: Creating graphics object to draw/generate image.
Line 3: CopyFromScreen() method is copying background image and generate image through graphics object.
Line 4: It is saving the image to physical folder.
Output:
Figure 1: Captured screenshot in Drive
Hope this helps you.