C#: how to take a screenshot of a portion of screen

Louis Rhys picture Louis Rhys · Jul 22, 2010 · Viewed 34.5k times · Source

like

TakeScreenshot(new Rectangle(0,0,100,100), "output.jpg");

Answer

Marcel Gheorghita picture Marcel Gheorghita · Jul 22, 2010

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);