Under Win7 I would like to get the content of a window on the clipboard and set/adjust the DPI setting on the clipboard and copy it to a final application.
The MCVE below is not yet working as desired.
There is an issue:
sometimes it can happen that apparently the window is not yet set to foreground and the ImageGrab.grab(bbox) gets the wrong content. Waiting for some time (2-5 sec) helps, but is not very practical. How to avoid or workaround this?
Here is the code:
from io import BytesIO
from PIL import Image,ImageGrab
import win32gui, win32clipboard
import time
def get_screenshot(window_name, dpi):
    hwnd = win32gui.FindWindow(None, window_name)
    if hwnd != 0:
        win32gui.SetForegroundWindow(hwnd)
        time.sleep(2)  ### sometimes window is not yet in foreground. delay/timing problem???
        bbox = win32gui.GetWindowRect(hwnd)
        screenshot = ImageGrab.grab(bbox)
        width, height = screenshot.size
        lmargin = 9
        tmargin = 70
        rmargin = 9
        bmargin = 36
        screenshot = screenshot.crop(box = (lmargin,tmargin,width-rmargin,height-bmargin))
        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        output = BytesIO()
        screenshot.convert("RGB").save(output, "BMP", dpi=(dpi,dpi))
        data = output.getvalue()[14:]
        output.close()
        win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
        win32clipboard.CloseClipboard()
        print("Screenshot taken...")
    else:
        print("No window found named:", window_name)
window_name = "Gnuplot (window id : 0)"
get_screenshot(window_name,200)
Edit:
also this attempt to improve still gets sometimes the wrong content. Maybe somebody can explain why?
win32gui.SetForegroundWindow(hwnd)
for i in range(1000):
    print(i)
    time.sleep(0.01)
    if win32gui.GetForegroundWindow() == hwnd:
        break
bbox = win32gui.GetWindowRect(hwnd)
Addition:
That's what I (typically) get when I remove the line with the delay time time.sleep(2). 
Left: desired content, right: received content. How can I get a reliable capture of content the desired window? What's wrong with the code? The larger I set the delay time the higher the probability that I get the desired content. But I don't want to wait several seconds to be sure. How can I check whether the system is ready for a screenshot?
