Capturing a window with WPF

c#, screenshot, wpf

Solution

You can:

- `CreateBitmap()` to create a hBitmap

- Call `GetDC()` on the hWnd

- `BitBlt()` the contents to the hBitmap

- `ReleaseDC()`

- Call `Imaging.CreateBitmapSourceFromHBitmap()` to create a managed `BitmapSource`

- `DeleteObject()` on the hBitmap

- Use the `BitmapSource` as desired

Steps 1-4 and 6 use the Win32 API (GDI to be precise), Steps 5 and 7 are done using WPF

Problem

With Windows Presentation Foundation, if I have an HWND, how can I capture it's window as an image that I can manipulate and display?

Original source