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: 
In your `button_Click` method, to capture a window other than the one which is doing the capturing, the only possible way I can see is to make use of the `user32` Windows API.

You will first need to platform invoke some methods like this:

```
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, GetWindowLongNIndex nIndex);

```

I recommend [www.pinvoke.net/](http://www.pinvoke.net/) for more info about this.

Here is my own `Window` class (work in progress, but useable) which you are welcome to use as an example: <http://pastebin.com/fj2tEaPY>

Some useful properties in there for you are:

* get `AllWindows`
* get `TaskbarWindows`
* set `ForegroundWindow`

You'll need to get the handle of your current window (use get `ForegroundWindow`, then the `Handle` property), then find the window which you want to capture the image of, perhaps using code similar to that in `TaskbarWindows`. Once you have the window, you will need to send the keypresses `Alt` + `PrtScn` (again maybe using `user32` API, but I won't go into it.) Then use the static `ForegroundWindow` property to set the orignal window as the forground using the handle which you saved at the start.

Good luck!
What about SendKeys?

```
Windows.Forms.SendKeys.Send("%{PRTSC}");

```

It will copy currently selected window in to Clipboard object.

Than calling 

```
BitmapSource image = Clipboard.GetImage();

```

you can get your image back