Question: I'm writing a simple Windows Forms utility to screenshot either the entire screen or an active window and save the file. The winform has a button called 'Capture' that, when clicked, takes the screenshot.

I have the code entirely working for the entire screen but can't figure it out for the active window.

By my current code, if I select 'Active Window', it ends up Print Screening the application itself which isn't what I want.

Here's my code (under the Capture\_Click method):

```
try
        {
            this.Hide();

            bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            //bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height, PixelFormat.Format32bppArgb);

            gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            if (AltPrint.Checked)
            {
                gfxScreenshot.CopyFromScreen(this.Bounds.X, this.Bounds.Y, 0, 0, this.Bounds.Size, CopyPixelOperation.SourceCopy);
            }
            else
            {
                gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
            }

            bmpScreenshot.Save(SaveLocation, ImageFormat.Png);

            number++;
        }

```

Answer: 
I ended up taking a different route.

I allow the user to resize my form window to cover exactly the area they need to screenshot, and then when they click the button, the form hides itself, takes a screenshot of the area it was covering and appears again. Contrary to my initial plan, I believe this will be more beneficial, as my targeted users (friends at an overclocking forum I'm part of) seldom take screenshots of just one window. They mostly need a couple of windows covered (temp monitoring, clockspeed monitoring, stability test etc.).

Thanks for all the answers guys. They definitely got me thinking, and made me realize what my options were.

Should anyone else need it, this is the code I use for the button that captures only part of the screen:

```
        try
        {
            this.Hide();
            Thread.Sleep(250);

            bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height, PixelFormat.Format32bppArgb);

            gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            gfxScreenshot.CopyFromScreen(this.Bounds.X, this.Bounds.Y, 0, 0, this.Bounds.Size, CopyPixelOperation.SourceCopy);

            bmpScreenshot.Save(SaveLocation, ImageFormat.Png);

            tbxStatus.AppendText(Environment.NewLine);
            tbxStatus.AppendText(Environment.NewLine);
            tbxStatus.AppendText("Screenshot saved at " + SaveLocation);

            numSuffix++;
        }
        catch (Exception ex)
        {
            tbxStatus.AppendText(Environment.NewLine);
            tbxStatus.AppendText(Environment.NewLine);
            tbxStatus.AppendText("Unable to take screenshot. Exception: " + ex.ToString());
        }
        finally
        {
            this.Show();
        }

```
You need to borrow the design of commercially available screen capturing apps, such as Psp. I think you need to use a 'hot' key to trigger the capturing of the active window. Instead of doing it on Capture\_Click event. This way, you can hide your application's window so that it does not become the active window. Then when the user pushes the 'hot' key, you can capture it.