Declare bitmap ad Graphics like below
- private static Bitmap bmpScreen;
- private static Graphics gfxScreen;
Then write the Following line in the command button:
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- bmpScreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
- // Create a graphics object from the bitmap
- gfxScreen = Graphics.FromImage(bmpScreen);
- // Take the screenshot from the upper left corner to the right bottom corner
- gfxScreen.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
- // Save the screenshot to the specified path that the user has chosen
- bmpScreen.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
- }
We can change the pixel format, there are verity of different Format Enumeration are available. check the below link.
I have used the code inside the saveas dialog box. it help us to Save the Image in the specified location.
or
We can specify the location manually like below.
- bmpScreen.Save(image location here, ImageFormat.Jpeg);
Have Fun!!!
Thanks