win32ui.error: CreateCompatibleDC failed

while trying to reproduce codes on PoE AI Part 4: Real-time Screen Capture and Plumbing, i encountered an error like this :

PS E:\tmp\game_assistant> python .\SSTime.py
Exception in thread Thread-1:
Traceback (most recent call last):
File “D:\Program Files\Anaconda3\lib\threading.py”, line 914, in _bootstrap_inner
self.run()
File “D:\Program Files\Anaconda3\lib\threading.py”, line 862, in run
self._target(*self._args, **self._kwargs)
File “E:\tmp\game_assistant\ScreenViewer.py”, line 114, in ScreenUpdateT
self.i1 = self.GetScreenImg()
File “E:\tmp\game_assistant\ScreenViewer.py”, line 69, in GetScreenImg
dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
win32ui.error: CreateCompatibleDC failed

it is due to minimizing window, that is to say:

TLDR
TLDR
TLDR
you should unminimizing the required window, then the codes will run sucessfully.
TLDR
TLDR
TLDR

PS:

elasped time using two screen capture methods on my own pc:

direct method : 0.03 ~ 0.05 s

author’s method : 0.006 ~0.011 s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
这段代码是用于截取屏幕区域的图像,并将其转换为OpenCV的BGR格式。它使用了`win32gui`、`win32api`和`win32ui`等库来实现。具体的步骤如下: 1. 获取当前窗口的句柄:`hwin = win32gui.GetDesktopWindow()` 2. 根据给定的区域参数计算截取图像的宽度、高度和起始点坐标(如果没有指定区域,则获取整个屏幕的参数)。 3. 获取窗口设备上下文:`hwindc = win32gui.GetWindowDC(hwin)` 4. 创建与窗口设备上下文兼容的内存上下文:`srcdc = win32ui.CreateDCFromHandle(hwindc)` 5. 创建一个与内存上下文兼容的位图对象:`bmp = win32ui.CreateBitmap()` 6. 创建一个与位图对象兼容的内存上下文:`memdc = srcdc.CreateCompatibleDC()` 7. 将位图对象与内存上下文关联:`memdc.SelectObject(bmp)` 8. 使用位块传输函数(BitBlt)将屏幕图像复制到位图对象中:`memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)` 9. 获取位图对象的像素数据并转换为NumPy数组:`signedIntsArray = bmp.GetBitmapBits(True); img = np.fromstring(signedIntsArray, dtype='uint8')` 10. 调整数组形状以匹配图像的高度、宽度和通道数:`img.shape = (height, width, 4)` 11. 释放资源,删除对象:`srcdc.DeleteDC(); memdc.DeleteDC(); win32gui.ReleaseDC(hwin, hwindc); win32gui.DeleteObject(bmp.GetHandle())` 12. 将图像从BGRA格式转换为BGR格式:`return cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)` 这个函数可以方便地在Windows环境下截取屏幕图像,并使用OpenCV进行后续处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值