title: How to get screenshot and change DPI on the clipboard?
body: <p>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.</p>
  
  <p>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 <code>ImageGrab.grab(bbox)</code> gets the wrong content. Waiting for some time (2-5 sec) helps, but is not very practical. How to avoid or workaround this?</p>
  
  <p>Here is the code:</p>
  
  <pre><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)
  </code></pre>
  
  <p>Edit: </p>
  
  <p>also this attempt to improve still gets sometimes the wrong content. Maybe somebody can explain why?</p>
  
  <pre><code>win32gui.SetForegroundWindow(hwnd)
  for i in range(1000):
      print(i)
      time.sleep(0.01)
      if win32gui.GetForegroundWindow() == hwnd:
          break
  bbox = win32gui.GetWindowRect(hwnd)
  </code></pre>
  
  <p><strong>Addition:</strong></p>
  
  <p>That's what I (typically) get when I remove the line with the delay time <code>time.sleep(2)</code>. </p>
  
  <p><strong>Left:</strong> desired content, <strong>right:</strong> 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?</p>
  
  <p><a href="https://i.stack.imgur.com/pVSn2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pVSn2.png" alt="enter image description here"></a></p>