Search code examples
c#wpfscreenshot

Capturing a window with WPF


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?


Solution

  • You can:

    1. CreateBitmap() to create a hBitmap
    2. Call GetDC() on the hWnd
    3. BitBlt() the contents to the hBitmap
    4. ReleaseDC()
    5. Call Imaging.CreateBitmapSourceFromHBitmap() to create a managed BitmapSource
    6. DeleteObject() on the hBitmap
    7. 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