4

Screenshot captured using BitBlt in c# resulted a black image on Windows 10. Please help me to resolve this.

Screenshot is black image for Chrome (when hardware accelerated mode is on) and IE/Edge windows.

Output image is black only for Edge, IE browser windows in Windows 10 and Chrome browser window when hardware accelerated mode is ON. Apart from all other windows including transparent windows screenshots are good.

Here is the code:

const int Srccopy = 0x00CC0020;
var windowRect = new Rect();

GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;

// get te hDC of the target window
IntPtr hdcSrc = GetWindowDC(handle);

// create a device context we can copy to
IntPtr hdcDest = CreateCompatibleDC(hdcSrc);

// create a bitmap we can copy it to,
IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height);
// select the bitmap object
IntPtr hOld = SelectObject(hdcDest, hBitmap);

// bitblt over
BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Srccopy);
// restore selection
SelectObject(hdcDest, hOld);
// clean up
DeleteDC(hdcDest);
ReleaseDC(handle, hdcSrc);

Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
DeleteObject(hBitmap);
tbala
  • 43
  • 3
  • 9
  • 1
    sorry, now code included – tbala Aug 10 '16 at 08:54
  • This is not a duplicate at all. The linked question is about transparent windows, this is about browser windows. – Joe Coder Aug 04 '18 at 22:23
  • The linked duplicate question is about a completely different problem. Whoever marked this as a duplicate doesn't know how to read. – Mercalli May 30 '19 at 22:45
  • @Luaan marked as a duplicate, and their vote is automatically binding since they are a gold badge user. Perhaps they can retract to automatically reopen. – theMayer Sep 04 '19 at 13:04
  • @theMayer Please look at the question at the time the vote was placed. In any case, it's still the same problem (capturing overlay), though the way it is now I would write a specific answer anyway - it's not enough of a duplicate not to deserve its own answer, and if you don't understand how overlays work, it's not obvious it's the same problem with the same solution. The duplicate policy used to be a bit harsher three years ago :) – Luaan Sep 04 '19 at 14:38
  • @Mercalli The solution is the same. I wouldn't mark it as duplicate today, but saying I can't read is a bit harsh, methinks. – Luaan Sep 04 '19 at 14:47
  • @Luaan, the solution for capturing semi-transparent windows doesn't solve this question (black image). The problem here is that the GDI library can't capture a hardware-accelerated window. It's a completely different problem. – Mercalli Sep 04 '19 at 16:07
  • @Luaan, the general consensus for identifying duplicates is to flag on the question, not the answer. If the question is not clearly a duplicate, and it seems like we are all in agreement it is not, then it’s ok to keep it around, even if the answer is the same or similar to others. – theMayer Sep 04 '19 at 18:50
  • @Mercalli The image isn't black, it says "here this shall be filled by whatever is rendered in overlay". It's the exact same problem, and the only difference is that now all the windows are (essentially) overlay, so you no longer see the old effect where you could e.g. play videos in paint by capturing the screen and pasting the screenshot to paint :P – Luaan Sep 05 '19 at 07:40
  • 1
    @theMayer Again, I agree with you. This has been closed three years ago, not last week :) – Luaan Sep 05 '19 at 07:40

1 Answers1

0

Hardware accelerated windows are rendered using overlay mode, which means that your BitBlt only gets pixels that say "Hey, this is overlay!". When the overlay isn't being rendered, this results in a black image - and if it is being rendered, you always see the current render, not something frozen in time. You're not capturing the pixels being shown on the screen, just some internal details of how window rendering works.

The solution is fortunately quite simple:

BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, 
       CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);

(you can modify your BitBlt P/Invoke definition to use CopyPixelOperation instead of int, or just cast those values to int yourself).

As a side-note, please don't forget to check the return values and handle errors accordingly.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • So windows (outside of browsers) are not hardware accelerated? (asking because the op said, it all works but it is just not working in the browsers) – Postlagerkarte Sep 05 '19 at 07:53
  • @Postlagerkarte Well, Microsoft always tries to keep things as backwards compatible as possible, so even though the window manager now uses hardware rendering for everything, the old applications are still captured without `CaptureBlt` as they always have been. Applications like Chrome don't use the window manager for anything but having an empty window with custom chrome - they don't handle the WM_PAINT event. In any case, I get a screenshot that shows Chrome just fine using this method. – Luaan Sep 05 '19 at 08:05
  • 1
    `CaptureBlt` will make `BitBlt` to copy from the screen DC, and so, anything that's overlapping your window will show up in the screenshot. The question is clearly about capturing a window, not the screen. The black image problem cannot be solved with the GDI library because it cannot obtain an image from a hardware-accelerated window. – Mercalli Sep 06 '19 at 01:02
  • Couldn't you ask the DWM for any window surface and then work with that? Given DWM cannot be turned off anymore under normal circumstances in Windows 10... – Ray Oct 18 '19 at 08:45