2

I am developing an application in C# and want to know how to get the screen shot image of any window when we only know its window handle. The functionality I am trying to achieve is to be able to just copy the screen shot of any window (it can be a whole application or a control inside an application) when I only know its window handle.

Example: I am running a calculator application, and Spy++. When I use find window functionality of the Spy++ and hover my mouse over different buttons of the calculator, it show different window handles. Lets say I hover mouse over "9" button in the calculator. Now as I know its window handle, I want to get image of the "9" button only. If I have window handle of the complete application then I want to be able to get the screen shot of that application only. Similar to Alt+PrintScr.

ResVic
  • 117
  • 4
  • 11

1 Answers1

1

You'll need to use GetWindowRect.

Just like shown in this example.

Then you can use Graphics.CopyFromScreen as shown in this SO post c# Take a screenshot of specific area.

Putting it all together, you call GetWindowRect, which accepts an HWnd to get the rect of the window you want. Then you use those rect values with CopyFromScreen to capture your image.

Community
  • 1
  • 1
Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
  • I tried this technique but there is one problem, the image taken is just the screenshot. It is not the image of the window. It works fine if the window is completely visible, but does not work if the window is hidden (completely, or even partially) behind another application. Is there any way of getting the image of the windows even when they are not completely shown on the screen? – ResVic Apr 23 '14 at 12:54
  • 1
    If it doesn't appear on the screen then it's probably not being drawn, which means you've got nothing to copy. My only suggestion would be to activate the window, move it fully into the viewable area, capture your image, then move it back and return focus to whatever was active before. It might cause a bit of flicker but I don't know of any other way... Perhaps you should ask a new question. – Scott Solmer Apr 23 '14 at 13:33
  • I will try this, but I think the flicker will be a problem, as I would be doing it frequently in my application. I am going to ask a new question, lets see if someone has a better solution. – ResVic Apr 23 '14 at 14:24
  • 1
    it late but solution here [User32.PrintWindow](https://stackoverflow.com/a/911225/5034139) – Trương Quốc Khánh Jul 07 '21 at 09:21