1

好的,所以当我使用此函数时出现此错误(错误:CreateCompatibleDC failed):

import win32api, win32ui, win32gui, win32con
def grab():
    global hwin, width, height, left, top
    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    #bmp.SaveBitmapFile(memdc, 'screenshot.bmp')
    return BMP(bmp, memdc)
hwin = win32gui.GetDesktopWindow()
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

它是这样说的:

Traceback (most recent call last):
  File "<pyshell#98>", line 1, in <module>
    grab()
  File "<pyshell#43>", line 9, in grab
    bmp.CreateCompatibleBitmap(srcdc, width, height)
error: CreateCompatibleDC failed

我认为这是因为我有太多的 DC,但是当我删除 DC 时,它并没有解决任何问题。请帮忙!谢谢!

4

0 回答 0