22

like

TakeScreenshot(new Rectangle(0,0,100,100), "output.jpg");
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221

4 Answers4

37

Use the following:

Rectangle rect = new Rectangle(0, 0, 100, 100);
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
bmp.Save(fileName, ImageFormat.Jpeg);
ASh
  • 34,632
  • 9
  • 60
  • 82
Marcel Gheorghita
  • 1,853
  • 13
  • 19
  • what is "PixelFormat.Format32bppArgb" for? – Louis Rhys Jul 22 '10 at 07:35
  • 1
    PixelFormat.Format32bppArgb specifies that the format is 32 bits per pixel; 8 bits each are used for the alpha, red, green, and blue components. – Marcel Gheorghita Jul 22 '10 at 07:53
  • This is the preferable solution, since it's only depending on the coordinates of the rectangle - and not the bounds of a primary screen. – MMM Nov 01 '17 at 09:15
11

Here is the code to capture the screen. Change the values to the size you need.

 Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);  

 Graphics graphics = Graphics.FromImage(printscreen as Image);  

 graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);  

 printscreen.Save(@"C:\printscreen.jpg", ImageFormat.Jpeg);

Or make method which will return you captured image like this :

Image CaptureScreen(int sourceX, int sourceY, int destX, int destY, 
            Size regionSize)
{
    Bitmap bmp = new Bitmap(regionSize.Width, regionSize.Height);
    Graphics g = Graphics.FromImage(bmp);
    g.CopyFromScreen(sourceX, sourceY, destX, destY, regionSize);
    return bmp;
}
 ......
 // call 
 Image image = CaptureScreen(sourceX, sourceY,  destX,  destY, regionSize);
 image.Save(@"C:\Somewhere\screen.jpg);
Incognito
  • 16,567
  • 9
  • 52
  • 74
3

Use the Graphics.CopyFromScreen method. Google turns up this tutorial.

Appulus
  • 18,630
  • 11
  • 38
  • 46
Thomas
  • 174,939
  • 50
  • 355
  • 478
1

Have you checked the Graphics.CopyFromScreen method?

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193