0

I found out that I can take a screenshot of an application using this method (and I'm currently taking a screenshot of my application and its working fine):

Bitmap image = new Bitmap(x, y, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(image);
IntPtr hDC = graphics.GetHdc();
SendMessage(this.Handle, WM_PRINT, hDC, PRF_CLIENT | PRF_CHILDREN | PRF_ERASEBKGND);
graphics.ReleaseHdc(hDC);

Unfortunately, this method stops working when I try to print the screen out, for example changing it to this:

Bitmap image = new Bitmap(x, y, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(image);
IntPtr hDC = graphics.GetHdc();
SendMessage(WIN32_API.GetDesktopWindow(), WM_PRINT, hDC, PRF_CLIENT | PRF_CHILDREN | PRF_ERASEBKGND);
graphics.ReleaseHdc(hDC);

Even when I try to get the process of my application and then use it's handler (Process.Handler), it's still not working although I can use (this.Handler). Why is this happening? and is there a way I can print out any process with this method (WM_PRINT)? I don't want to use other methods.

  • Does this answer your question? [What is wrong with PrintWindow?](https://stackoverflow.com/questions/6241407/what-is-wrong-with-printwindow) – Jeff Oct 15 '20 at 18:04
  • No, because I'm also trying to use it on any process (Process.Handler), but it doesn't seem to work even when I use it at my own application's process. How does it work with this.Handler but doesn't work with Process.Handler? I want a way to use SendMessage on any process – user3425606 Oct 16 '20 at 00:27
  • You are conflating process handles with window handles; they are not the same thing and cannot be used interchangeably. You need to use a window handle for `SendMessage`. `this.Handle` works because it returns a window handle. See [here](https://stackoverflow.com/questions/1888863/how-to-get-main-window-handle-from-process-id); looks like you'll need to use `EnumWindows` to get the window handles of another process. – Jeff Oct 16 '20 at 01:06

0 Answers0