I'm trying to get an image of an RTL window using the following python code using pywin32:
def get_window_image(hwnd: int, window_region: Region) -> np.ndarray:
PW_RENDERFULLCONTENT = 2
dc_handle = None
dc_obj = None
save_bitmap = None
memory_dc_obj = None
try:
# Creating device context for retrieving the screenshot. Based on the following:
# https://stackoverflow.com/questions/19695214/python-screenshot-of-inactive-window-printwindow-win32gui
# Documentation about device context:
# https://learn.microsoft.com/en-us/cpp/mfc/device-contexts?view=vs-2019
dc_handle = win32gui.GetWindowDC(hwnd)
dc_obj = win32ui.CreateDCFromHandle(dc_handle)
memory_dc_obj = dc_obj.CreateCompatibleDC()
# Create a bitmap from the device context
save_bitmap = win32ui.CreateBitmap()
save_bitmap.CreateCompatibleBitmap(dc_obj, window_region.get_width(), window_region.get_height())
memory_dc_obj.SelectObject(save_bitmap)
if sys.getwindowsversion().major >= 8:
render_flags = PW_RENDERFULLCONTENT
else:
render_flags = 0
# Take a screenshot of the window
windll.user32.PrintWindow(hwnd, memory_dc_obj.GetSafeHdc(), render_flags)
# Retrieve the bitmap content from the taken screenshot
bmp_info = save_bitmap.GetInfo()
bmp_str = save_bitmap.GetBitmapBits(True)
bmp_height, bmp_width = bmp_info['bmHeight'], bmp_info['bmWidth']
window_image = np.fromstring(string=bmp_str, dtype=np.uint8)
window_image = window_image.reshape((bmp_height, bmp_width, 4))
window_image[:, :, 3] = 255 # remove alpha channel
return window_image
finally:
if save_bitmap is not None:
win32gui.DeleteObject(save_bitmap.GetHandle())
if memory_dc_obj is not None:
memory_dc_obj.DeleteDC()
if dc_obj is not None:
dc_obj.DeleteDC()
if dc_handle is not None:
win32gui.ReleaseDC(hwnd, dc_handle)
The code to generate the RTL window:
win32gui.MessageBox(None, 'משהו בעברית', 'test-rtl', win32con.MB_ICONWARNING | win32con.MB_RTLREADING)
I'm passing PrintWindow
the flag - PW_RENDERFULLCONTENT = 2
, which should help render the window with it's correct frame and not with the classic frame (also see https://stackoverflow.com/a/40042587/4313487)
I have 2 PCs with the same specs. On the first PC the image is printed as mirrored and on the second it is printed as it's originally displayed.
Both PCs have windows 10 with the same region and locale settings. On both PCs the messagebox window has the same style flags including WS_EX_LAYOUTRTL.
Image from PC1 - gets mirrored:
Image from PC2 (same code):
When the flag passed to PrintWindow is 0 (instead of PW_RENDERFULLCONTENT) the image is mirrored in both PCs:
Why does it happen?
How can I make sure that the image gets printed in the same direction on different PCs?