Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing a window with WPF

Tags:

c#

screenshot

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?

like image 902
directedition Avatar asked Dec 03 '22 06:12

directedition


1 Answers

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

like image 181
Ray Burns Avatar answered Dec 20 '22 11:12

Ray Burns