0

When trying to take a screenshot of vmware machine the inside of it is being blacked out for some reason.

blank screen proof

Where as I expect to see the following:

visible screen proof

The code seems to work fine when testing on calculator and few other applications, what could be causing this issue?

import win32gui
import win32ui
import win32con

def backgroundScreenshot(hwnd, width, height):
    wDC = win32gui.GetWindowDC(hwnd)
    dcObj=win32ui.CreateDCFromHandle(wDC)
    cDC=dcObj.CreateCompatibleDC()
    dataBitMap = win32ui.CreateBitmap()
    dataBitMap.CreateCompatibleBitmap(dcObj, width, height)
    cDC.SelectObject(dataBitMap)
    cDC.BitBlt((0,0),(width, height) , dcObj, (0,0), win32con.SRCCOPY)
    dataBitMap.SaveBitmapFile(cDC, 'test.bmp')
    dcObj.DeleteDC()
    cDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, wDC)
    win32gui.DeleteObject(dataBitMap.GetHandle())

title="Win10_64 - VMware Workstation 16 Player (Non-commercial use only)"
hwnd = win32gui.FindWindow(None, title)
print(hwnd)
backgroundScreenshot(hwnd, 900, 780)
Anisimow
  • 26
  • 3
  • 1
    [SetWindowDisplayAffinity](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowdisplayaffinity), for example. In this case, though, it's probably due to non-GDI rendering of the client area. – IInspectable Nov 19 '20 at 20:46
  • I get black image for calculator on Windows 10 2004 but it works on Windows 8.1. What's your Windows version? – Rita Han Nov 20 '20 at 06:12
  • You can check [New Ways to do Screen Capture](https://blogs.windows.com/windowsdeveloper/2019/09/16/new-ways-to-do-screen-capture/) to see if it helps. – Rita Han Nov 20 '20 at 06:33
  • @RitaHan-MSFT the window cannot be minimized, windows 10 64bit 10.0 – Anisimow Nov 20 '20 at 19:55

1 Answers1

0
import pyautogui
import win32gui
import numpy as np

def screenshot(window_title=None):
    if window_title:
        hwnd = win32gui.FindWindow(None, window_title)
        if hwnd:
            win32gui.SetForegroundWindow(hwnd)
            x, y, x1, y1 = win32gui.GetClientRect(hwnd)
            x, y = win32gui.ClientToScreen(hwnd, (x, y))
            x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y))
            im = pyautogui.screenshot(region=(x, y, x1, y1))
            
            img = np.array(im)
            return img
        else:
            print('Error: Virtual Machine is not running!')
    else:
        im = pyautogui.screenshot()
        return im


im = screenshot('Win10_64 - VMware Workstation 16 Player (Non-commercial use only)')
if im:
    im.save('test.png')

This is alternative way of taking screenshots, it does work however it doesn't achieve the desired functionality of being able to take a screenshot of the window while it's in the background since other tabs are being also screenshoteted.

Problem solved, cause: unknown (if anyone has some suggestions for the cause, I would appreciate it)

Virtual Machine Screen

Master screen

Desired Output

Anisimow
  • 26
  • 3