0

I think the same error as this thread; Python win32ui.error: CreateCompatibleDC failed

It's been a few months since I took a look at this code because I couldn't get past this error.

I'm using source_needle_images as the artefacts I'm trying to find, and haystack as the image I'm trying to find them in. My needles are currently two jpg's however, in future, this may be dozens of jpg's and my haystack is an application window named wincap

The eventual intention is to "do something" if anything whatsoever from source_needle_images is found within the application window.

This code is from my main.py, and I'm using two functions search and windowcapture

windowcapture captures the application window, search performs the OCR.

from windowcapture import WindowCapture
from search import Search
    
# the window to capture 
wincap = WindowCapture('P')

# load needle images and start the matching process 
source_needle_images = glob.glob(r'C:\\\\\\\*.jpg')
search = Search(source_needle_images)

loop_time = time()
while(True):

    # get an updated image of the background
    haystack_img = wincap.get_screenshot()

    # display the processed image
    points = search.find(haystack_img, 0.85, 'rectangles')
   

This is the section of code causing issue;

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())

and this is the error in question;

Traceback (most recent call last):
  File "c:\\\\\\main.py", line 23, in <module>
    haystack_img = wincap.get_screenshot()
  File "c:\\\\\\windowcapture.py", line 52, in get_screenshot
    dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
win32ui.error: CreateCompatibleDC failed

Updated with requested code:

   # constructor
    def __init__(self, window_name=None):
        # find the handle for the window we want to capture.
        # if no window name is given, capture the entire screen
        if window_name is None:
            self.hwnd = win32gui.GetDesktopWindow()
        else:
            self.hwnd = win32gui.FindWindow(None, window_name)
            if not self.hwnd:
                raise Exception('Window not found: {}'.format(window_name))
  • What is `self.hwnd`? – Tim Roberts Aug 29 '21 at 01:13
  • added to the OP, it's been a little while but I'm passing the window name across via wincap = WindowCapture('P') from my main.py –  Aug 29 '21 at 01:18
  • Is that constructor from the `WindowCapture` class? So, you are looking for a window named "P"? You're sure such a window exists, and continues to exist through your whole run? – Tim Roberts Aug 29 '21 at 03:24
  • 100% certain, but running this; dataBitMap.SaveBitmapFile(cDC, r'C:\Users\debug.bmp') isn't saving anything. Could it be possible win32 isn't finding the window for some reason? –  Aug 29 '21 at 08:59
  • And yes sorry, that's the constructor from the windowcapture class! It definitely used to work, it worked all through testing and the just stopped and I can't for the life of me figure out why. –  Aug 29 '21 at 09:00

0 Answers0