C#: Capture a ScreenShot

The following code demonstrates one method of capturing a screenshot. The method displayed returns a screenshot of the entire screen as a bitmap.

 

private Bitmap GetScreenShot()
        {
            // set screenshot bounds to match the size of the screen
            Rectangle bounds = Screen.PrimaryScreen.Bounds;
            // crate a bitmap with the dimensions of the sreen
            Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
            }
            return bitmap;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = GetScreenShot();
        }