Re: Saving a Device context (DC) into a picture file
Michael C <[email protected]> Fri, 28 Apr 2017 17:32:52 -0700
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <CANyKM1is4S77K2iaGC7GVJV_ySE7zftKxfmPvegzwHKTAAWMGQ@mail.gmail.com> |
--===============8843406549205761628== Content-Type: multipart/alternative; boundary=001a11448f0aa6e1d8054e43546b --001a11448f0aa6e1d8054e43546b Content-Type: text/plain; charset=UTF-8 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 <[email protected]> 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 <[email protected] > > wrote: > >> neat-o >> >> >> On Sat, Apr 22, 2017 at 2:27 PM, eryk sun <[email protected]> wrote: >> >>> On Sat, Apr 22, 2017 at 7:15 PM, Michael C >>> <[email protected]> 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. >>> >> >> > --001a11448f0aa6e1d8054e43546b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><span style=3D"font-size:12.8px">Hi all:</span><div style= =3D"font-size:12.8px"><br></div><div style=3D"font-size:12.8px">I have my o= wn code that samples pixel colour,=C2=A0</div><div style=3D"font-size:12.8p= x"><a href=3D"https://pastebin.com/4EMd765h" target=3D"_blank">https://past= ebin.com/4EMd765h</a><br></div><div style=3D"font-size:12.8px"><br></div><d= iv style=3D"font-size:12.8px">=C2=A0and now I am trying to add the feature = of saving the Device Context to a .bmp file, so I found this code=C2=A0</di= v><div style=3D"font-size:12.8px"><br></div><div style=3D"font-size:12.8px"= ><a href=3D"https://books.google.ca/books?id=3D9MS9BQAAQBAJ" target=3D"_bla= nk">https://books.google.ca/books?<wbr>id=3D9MS9BQAAQBAJ</a></div><div styl= e=3D"font-size:12.8px"><br></div><div style=3D"font-size:12.8px">however th= ere is one problem: The book example screenshots the whole desktop and I on= ly 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?<= /div><div style=3D"font-size:12.8px"><br></div><div style=3D"font-size:12.8= px"><span style=3D"font-size:12.8px">namely, these 4 lines.</span><div styl= e=3D"font-size:12.8px"><br></div><div style=3D"font-size:12.8px"><div>width= =3D win32api.GetSystemMetrics(<wbr>win32con.SM_CXVIRTUALSCREEN)</div><div>= height =3D win32api.GetSystemMetrics(<wbr>win32con.SM_CYVIRTUALSCREEN)</div= ><div>left =3D win32api.GetSystemMetrics(<wbr>win32con.SM_XVIRTUALSCREEN)</= div><div>top =3D win32api.GetSystemMetrics(<wbr>win32con.SM_YVIRTUALSCREEN)= </div></div></div><div style=3D"font-size:12.8px"><br></div><div style=3D"f= ont-size:12.8px">Thanks!</div></div><div class=3D"gmail_extra"><br><div cla= ss=3D"gmail_quote">On Fri, Apr 28, 2017 at 5:28 PM, Michael C <span dir=3D"= ltr"><<a href=3D"mailto:[email protected]" target=3D"_blank= ">[email protected]</a>></span> wrote:<br><blockquote class= =3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd= ing-left:1ex"><div dir=3D"ltr">Hi all:<div><br></div><div>I have my own cod= e that samples pixel colour,=C2=A0</div><div><a href=3D"https://pastebin.co= m/4EMd765h" target=3D"_blank">https://pastebin.com/4EMd765h</a><br></div><d= iv><br></div><div>=C2=A0and now I am trying to add the feature of saving th= e Device Context to a .bmp file, so I found this code=C2=A0</div><div><br><= /div><div><a href=3D"https://books.google.ca/books?id=3D9MS9BQAAQBAJ" targe= t=3D"_blank">https://books.google.ca/books?<wbr>id=3D9MS9BQAAQBAJ</a></div>= <div><br></div><div>however there is one problem: The book example screensh= ots the whole desktop and I only want a screenshot of a given window. Is th= ere a way to modify the black hat example into something that only screensh= ot a window given its handle?</div><div><br></div><div>Thanks!</div><div><b= r></div><div><br></div></div><div class=3D"HOEnZb"><div class=3D"h5"><div c= lass=3D"gmail_extra"><br><div class=3D"gmail_quote">On Sat, Apr 22, 2017 at= 2:29 PM, Michael C <span dir=3D"ltr"><<a href=3D"mailto:mysecretrobotfa= [email protected]" target=3D"_blank">mysecretrobotfactory@gmail.<wbr>com</a>&= gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 = 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">neat-o= <div><br></div></div><div class=3D"m_-8829367243863059807HOEnZb"><div class= =3D"m_-8829367243863059807h5"><div class=3D"gmail_extra"><br><div class=3D"= gmail_quote">On Sat, Apr 22, 2017 at 2:27 PM, eryk sun <span dir=3D"ltr">&l= t;<a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]<= /a>></span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:= 0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Sat, Apr 2= 2, 2017 at 7:15 PM, Michael C<br> <<a href=3D"mailto:[email protected]" target=3D"_blank">mys= [email protected]<wbr>m</a>> wrote:<br> > Never mind, I found this<br> > <a href=3D"https://books.google.ca/books?id=3D9MS9BQAAQBAJ" rel=3D"nor= eferrer" target=3D"_blank">https://books.google.ca/books?<wbr>id=3D9MS9BQAA= QBAJ</a><br> <br> </span>Thankfully a lot of the Windows code in Black Hat Python uses PyWin3= 2.<br> The few ctypes examples aren't top notch. For example, the author<br> calls<br> <br> =C2=A0 =C2=A0 =C2=A0user32.GetWindowThreadProcess<wbr>Id(hwnd, byref(pid))<= br> <br> without setting the prototype of GetWindowThreadProcessId. In 64-bit<br> Python 2, the value of the hwnd argument may get corrupted if the<br> prototype isn't defined. It's only by luck of the draw that it usua= lly<br> works.<br> <br> Some WinAPI pseudohandles are non-zero in the upper DWORD, such as<br> HWND_TOPMOST, which is defined as (HWND)(-1), i.e. 0xFFFFFFFFFFFFFFFF<br> on a 64-bit system. This has the same problem in Python 2 if you<br> forget to set the prototype or manually wrap the value in a pointer<br> type. Python 3 ctypes doesn't have this problem, since it sign extends<= br> a negative argument as an intptr_t.<br> <br> Here's another problem in the book's example code. The default rest= ype<br> is a signed c_int, and here the author is calling GetTickCount without<br> first setting GetTickCount.restype =3D ctypes.c_uint:<br> <br> =C2=A0 =C2=A0 run_time =3D kernel32.GetTickCount()<br> =C2=A0 =C2=A0 elapsed =3D run_time - struct_lastinputinfo.dwTime<br> <br> After 24.86 days system uptime, the tick count reaches 0x80000000<br> (milliseconds), and the signed return value wraps around to<br> -<a href=3D"tel:(214)%20748-3648" value=3D"+12147483648" target=3D"_blank">= 2147483648</a>, incrementing toward 0. The value of `elapsed` will be<br> nonsense.<br> </blockquote></div><br></div> </div></div></blockquote></div><br></div> </div></div></blockquote></div><br></div> --001a11448f0aa6e1d8054e43546b-- --===============8843406549205761628== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot --===============8843406549205761628== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ ctypes-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ctypes-users --===============8843406549205761628==--