[python-win32] GetDIBits Troubles

Tim Roberts timr at probo.com
Mon Feb 22 20:23:57 CET 2010


McBuell wrote:
> I know that GetDIBits is not part of the win32 module, but it seems for me
> the best matching forum for my question.
>
> I'm currently trying to capture a screenshot on a Windows XP machine.
> Although there are several other ways (ImageGrab of PIL, third party dlls
> etc.) I want to try to use the Win32 API to capture it.
> As input for my brain, I'm using a working c++ script, that calls some win32
> apis.
>
> Although most of my script seems to work without problems, the final call to
> GetDIBits
> (of the venster.gdi module, which is the only module that includes GetDIBits
> as far as I know)
> fails (returns 0), without a meeningfull error message. So I'm stuck a
> little bit.
>   

Odd.  I traced your code in windbg through GetDIBits clear to the point
where it calls into the kernel, and it has passed all the parameter
validation.  I don't see anything immediately wrong with this.  The doc
says that that the bitmap must not be selected into a DC at the time of
the call, but the sample application doesn't follow that, and when I
selected hbmOld back into memDC it didn't change anything.

FWIW, you can replace venster.gdi.GetDIBits with
ctypes.windll.gdi32.GetDIBits with the same results and use one less module.

There are a couple of alternatives.  After you do the BitBlt, you can
call GetBitmapBits to make a copy of the bits.  That's quicker than
GetDIBits, and as long as you're running true color (which everyone is
today), it's the same end result.

Alternatively, you can call GetObject on the hbm, which returns a BITMAP
structure.  The bmBits member contains a pointer to the bits (which
you'd still have to copy).

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list