Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you access the pixel array in a Windows Device Context?
    primarykey
    data
    text
    <p>I'm working on a small automation tool that takes snapshots of the screen, does some template matching, and then responds accordingly. I've previously used PIL for this, but it's <code>ImageGrab</code> function is limited to the main monitor, I need the option of grabbing any display device attached to the machine. After some brief Googling, I arrived at the following code as a solution. </p> <h2>ScreenShot Code</h2> <pre><code>def capture_display(): monitors = win32api.EnumDisplayMonitors(None, None) hwnd = monitors[1][1].handle l,t,r,b = monitors[1][2] w = r - l h = b hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() saveBitMap.CreateCompatibleBitmap(mfcDC, w, h) saveDC.SelectObject(saveBitMap) saveDC.BitBlt((0, 0), (w, h), mfcDC, (l, t), win32con.SRCCOPY) saveBitMap.SaveBitmapFile(saveDC, 'screencapture.bmp') </code></pre> <p>This works remarkably well. However, it saves the image. All I need is access to the underlying pixel array in memory. </p> <p>Of course, there is the option of just saving it, reloading it from disc via PIL, and then getting the pixel data, but the IO overhead on this is too great to be of any use.</p> <p>Now, I'm in foreign territory with the Windows API, however, from reading on MSDN I found this: </p> <blockquote> <h3>Memory Device Contexts</h3> <p>It is an array of bits in memory that an application can use temporarily to store the color data for bitmaps created on a normal drawing surface. Because the bitmap is compatible with the device, a memory DC is also sometimes referred to as a compatible device context.</p> </blockquote> <p>So, it seems like these DCs indeed store the pixel data, but I just have no idea how to access it. How do I get at that underlying pixel array? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload