首页 > 解决方案 > 如何在剪贴板上获取屏幕截图并更改 DPI?

问题描述

在 Win7 下,我想在剪贴板上获取窗口的内容,并在剪贴板上设置/调整 DPI 设置并将其复制到最终应用程序中。

下面的 MCVE 尚未按预期工作。有一个问题:有时可能会发生显然窗口尚未设置为前景并且ImageGrab.grab(bbox)获取错误内容的情况。等待一段时间(2-5 秒)会有所帮助,但不是很实用。如何避免或解决这个问题?

这是代码:

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)

编辑:

这种改进的尝试有时也会得到错误的内容。也许有人可以解释为什么?

win32gui.SetForegroundWindow(hwnd)
for i in range(1000):
    print(i)
    time.sleep(0.01)
    if win32gui.GetForegroundWindow() == hwnd:
        break
bbox = win32gui.GetWindowRect(hwnd)

添加:

这就是我(通常)在删除带有延迟时间的线路时得到的time.sleep(2)

左:想要的内容,右:收到的内容。如何可靠地捕获所需窗口的内容?代码有什么问题?我设置的延迟时间越大,我获得所需内容的概率就越高。但我不想等几秒钟才能确定。如何检查系统是否已准备好截屏?

在此处输入图像描述

标签: python-3.xpython-imaging-libraryscreenshotdpiwin32gui

解决方案


正如所讨论的,您可以使用下面讨论的方法

Python 非活动窗口截图 PrintWindow + win32gui

import win32gui
import win32ui
from ctypes import windll
import Image

hwnd = win32gui.FindWindow(None, 'Calculator')

# Change the line below depending on whether you want the whole window
# or just the client area. 
#left, top, right, bot = win32gui.GetClientRect(hwnd)
left, top, right, bot = win32gui.GetWindowRect(hwnd)
w = right - left
h = bot - top

hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC  = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()

saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)

saveDC.SelectObject(saveBitMap)

# Change the line below depending on whether you want the whole window
# or just the client area. 
#result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 1)
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
print result

bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)

im = Image.frombuffer(
    'RGB',
    (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
    bmpstr, 'raw', 'BGRX', 0, 1)

win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)

if result == 1:
    #PrintWindow Succeeded
    im.save("test.png")

推荐阅读