Re: [ctypes-users] Saving a Device context (DC) into a picture file
Brought to you by:
theller
From: Michael C <mys...@gm...> - 2017-04-29 00:33:00
|
Hi all: I have my own code that samples pixel colour, https://pastebin.com/4EMd765h and now I am trying to add the feature of saving the Device Context to a .bmp file, so I found this code https://books.google.ca/books?id=9MS9BQAAQBAJ however there is one problem: The book example screenshots the whole desktop and I only want a screenshot of a given window. Is there a way to modify the black hat example into something that only screenshot a window given its handle? namely, these 4 lines. width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN) height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN) left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN) top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN) Thanks! On Fri, Apr 28, 2017 at 5:28 PM, Michael C <mys...@gm...> wrote: > Hi all: > > I have my own code that samples pixel colour, > https://pastebin.com/4EMd765h > > and now I am trying to add the feature of saving the Device Context to a > .bmp file, so I found this code > > https://books.google.ca/books?id=9MS9BQAAQBAJ > > however there is one problem: The book example screenshots the whole > desktop and I only want a screenshot of a given window. Is there a way to > modify the black hat example into something that only screenshot a window > given its handle? > > Thanks! > > > > On Sat, Apr 22, 2017 at 2:29 PM, Michael C <mys...@gm... > > wrote: > >> neat-o >> >> >> On Sat, Apr 22, 2017 at 2:27 PM, eryk sun <er...@gm...> wrote: >> >>> On Sat, Apr 22, 2017 at 7:15 PM, Michael C >>> <mys...@gm...> wrote: >>> > Never mind, I found this >>> > https://books.google.ca/books?id=9MS9BQAAQBAJ >>> >>> Thankfully a lot of the Windows code in Black Hat Python uses PyWin32. >>> The few ctypes examples aren't top notch. For example, the author >>> calls >>> >>> user32.GetWindowThreadProcessId(hwnd, byref(pid)) >>> >>> without setting the prototype of GetWindowThreadProcessId. In 64-bit >>> Python 2, the value of the hwnd argument may get corrupted if the >>> prototype isn't defined. It's only by luck of the draw that it usually >>> works. >>> >>> Some WinAPI pseudohandles are non-zero in the upper DWORD, such as >>> HWND_TOPMOST, which is defined as (HWND)(-1), i.e. 0xFFFFFFFFFFFFFFFF >>> on a 64-bit system. This has the same problem in Python 2 if you >>> forget to set the prototype or manually wrap the value in a pointer >>> type. Python 3 ctypes doesn't have this problem, since it sign extends >>> a negative argument as an intptr_t. >>> >>> Here's another problem in the book's example code. The default restype >>> is a signed c_int, and here the author is calling GetTickCount without >>> first setting GetTickCount.restype = ctypes.c_uint: >>> >>> run_time = kernel32.GetTickCount() >>> elapsed = run_time - struct_lastinputinfo.dwTime >>> >>> After 24.86 days system uptime, the tick count reaches 0x80000000 >>> (milliseconds), and the signed return value wraps around to >>> -2147483648 <(214)%20748-3648>, incrementing toward 0. The value of >>> `elapsed` will be >>> nonsense. >>> >> >> > |