win32 parts are being blacked out when taking screenshots of vmware

328 Views Asked by At

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)
1

There are 1 best solutions below

0
Anisimow On
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