Python win32ui.error:CreateCompatibleDC 失败

Posted

技术标签:

【中文标题】Python win32ui.error:CreateCompatibleDC 失败【英文标题】:Python win32ui.error: CreateCompatibleDC failed 【发布时间】:2021-06-29 16:17:10 【问题描述】:

我正在尝试使用 win32 为 opencv 捕获屏幕,但是当我启动它时,它给我一个关于 CreateCompatibleDC 函数的错误。

这是我第一次接触这个库,所以我真的不知道如何解决这个问题。

import numpy as np
import win32gui,win32ui,win32con


class WindowCapture:

    # properties
    w = 0
    h = 0
    hwnd = None
    cropped_x = 0
    cropped_y = 0
    offset_x = 0
    offset_y = 0

    # constructor
    def __init__(self, window_name):
        # find the handle for the window we want to capture
        self.hwnd = win32gui.FindWindow(None, window_name)
        if not self.hwnd:
            raise Exception('Window not found: '.format(window_name))

        # get the window size
        window_rect = win32gui.GetWindowRect(self.hwnd)
        self.w = window_rect[2] - window_rect[0]
        self.h = window_rect[3] - window_rect[1]

        # account for the window border and titlebar and cut them off
        border_pixels = 8
        titlebar_pixels = 30
        self.w = self.w - (border_pixels * 2)
        self.h = self.h - titlebar_pixels - border_pixels
        self.cropped_x = border_pixels
        self.cropped_y = titlebar_pixels

        # set the cropped coordinates offset so we can translate screenshot
        # images into actual screen positions
        self.offset_x = window_rect[0] + self.cropped_x
        self.offset_y = window_rect[1] + self.cropped_y

    def get_screenshot(self):

        # get the window image data
        wDC = win32gui.GetWindowDC(self.hwnd)
        dcObj = win32ui.CreateDCFromHandle(wDC)
        cDC = dcObj.CreateCompatibleDC()
        dataBitMap = win32ui.CreateBitmap()
        dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
        cDC.SelectObject(dataBitMap)
        cDC.BitBlt((0, 0), (self.w, self.h), dcObj, (self.cropped_x, self.cropped_y), win32con.SRCCOPY)

        # convert the raw data into a format opencv can read
        #dataBitMap.SaveBitmapFile(cDC, 'debug.bmp')
        signedIntsArray = dataBitMap.GetBitmapBits(True)
        img = np.fromstring(signedIntsArray, dtype='uint8')
        img.shape = (self.h, self.w, 4)

        # free resources
        dcObj.DeleteDC()
        cDC.DeleteDC()
        win32gui.ReleaseDC(self.hwnd, wDC)
        win32gui.DeleteObject(dataBitMap.GetHandle())

        # drop the alpha channel, or cv.matchTemplate() will throw an error like:
        #   error: (-215:Assertion failed) (depth == CV_8U || depth == CV_32F) && type == _templ.type() 
        #   && _img.dims() <= 2 in function 'cv::matchTemplate'
        img = img[...,:3]

        # make image C_CONTIGUOUS to avoid errors that look like:
        #   File ... in draw_rectangles
        #   TypeError: an integer is required (got type tuple)
        # see the discussion here:
        # https://github.com/opencv/opencv/issues/14866#issuecomment-580207109
        img = np.ascontiguousarray(img)

        return img

    # find the name of the window you're interested in.
    # once you have it, update window_capture()
    # https://***.com/questions/55547940/how-to-get-a-list-of-the-name-of-every-open-window
    def list_window_names(self):
        def winEnumHandler(hwnd, ctx):
            if win32gui.IsWindowVisible(hwnd):
                print(hex(hwnd), win32gui.GetWindowText(hwnd))
        win32gui.EnumWindows(winEnumHandler, None)

    # translate a pixel position on a screenshot image to a pixel position on the screen.
    # pos = (x, y)
    # WARNING: if you move the window being captured after execution is started, this will
    # return incorrect coordinates, because the window position is only calculated in
    # the __init__ constructor.
    def get_screen_position(self, pos):
        return (pos[0] + self.offset_x, pos[1] + self.offset_y)

这是我写的,指的是Learn Code By Gaming video

但启动时,出现以下错误:

File "C:\X\windowcapture.py", line 49, in get_screenshot
        dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
    win32ui.error: CreateCompatibleDC failed

【问题讨论】:

替换:d3dshot,python 模块,可在 pypi (pip) 上找到。不过,您似乎想捕获特定的窗口,而我认为 d3dshot 不能做到这一点。 ...啊视频已经提到了 d3dshot。哦,好吧。 命名窗口是否存在?您的帖子缺少实际运行此代码的代码。 【参考方案1】:

发件人:

border_pixels = 8
titlebar_pixels = 30
self.w = self.w - (border_pixels * 2)
self.h = self.h - titlebar_pixels - border_pixels
self.cropped_x = border_pixels
self.cropped_y = titlebar_pixels

收件人:

border_pixels = 1  # <-- Change this
titlebar_pixels = 10  # <-- Change this
self.w = self.w - border_pixels  # <-- Change this
self.h = self.h - titlebar_pixels - border_pixels
self.cropped_x = border_pixels
self.cropped_y = titlebar_pixels

【讨论】:

以上是关于Python win32ui.error:CreateCompatibleDC 失败的主要内容,如果未能解决你的问题,请参考以下文章

怎样用python调用vc++编出来的win32 DLL

Python 下载win32api 模块

python操作word,关于win32com

转 python win32api操作

python win32api win32gui win32con 窗口句柄 发送消息 常用方法 键盘输入

带有win32con的Python滚轮鼠标