Re: Saving a Device context (DC) into a picture file

Michael C <[email protected]> Fri, 28 Apr 2017 17:31:54 -0700
Newsgroups gmane.comp.python.ctypes
Message-ID <CANyKM1jrtghymw573ZWdRxx_A+aNnwmgcfRLzVxBDjiy=aXvQg@mail.gmail.com>
--===============3167679717153051998==
Content-Type: multipart/alternative; boundary=001a11451db6222898054e43519b

--001a11451db6222898054e43519b
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?

Thanks!

On Sat, Apr 22, 2017 at 12:15 PM, Michael C <[email protected]>
wrote:

> Never mind, I found this
> https://books.google.ca/books?id=9MS9BQAAQBAJ&pg=PA116&lpg=
> PA116&dq=python+device+context+save&source=bl&ots=tYhl4K1K6J&sig=
> zgzJmOFeOIZL2C5eGrZ-Oa_QRB4&hl=en&sa=X&ved=0ahUKEwijjIvX37jTAhVQ42MKHVIFC
> HEQ6AEIOjAE#v=onepage&q=python%20device%20context%20save&f=false
>
> thanks all
>
> On Sat, Apr 22, 2017 at 11:52 AM, Michael C <mysecretrobotfactory@gmail.
> com> wrote:
>
>> Quick Question:
>> I am using this code, and I have tried to find answers with google, and I
>> have read through MSDN.
>>
>> How do I save the DC into a png file or bmp?
>>
>> thanks!
>>
>>
>> import time
>> import ctypes
>> ##from ctypes import *
>> ## I don't like this. I like to import.
>>
>> time.sleep(2)
>>
>> # Set up the User32 and GDI32
>> User32 = ctypes.WinDLL('User32', use_last_error=True)
>> GDI32 = ctypes.WinDLL('GDI32', use_last_error=True)
>>
>> # Set up Fx to be used.
>> GetForegroundWindow = User32.GetForegroundWindow
>> GetWindowDC = User32.GetWindowDC
>> GetPixel = GDI32.GetPixel
>> ReleaseDC = User32.ReleaseDC
>>
>>
>> # main code
>> foreground_window = GetForegroundWindow()
>> dc = GetWindowDC(foreground_window)
>>
>> # set timer to see how fast it runs.
>> t0 = time.clock()
>>
>> for i in range(50):
>>     rgb = GetPixel(dc, 10, 10)
>>     #print(rgb)
>>     r = rgb & 0xff
>>     g = (rgb >> 8) & 0xff
>>     b = (rgb >> 16) & 0xff
>> ##    print("RGB(%d, %d, %d)" % (r, g, b))
>>
>> print(time.clock() - t0)
>> ReleaseDC(foreground_window, dc)
>>
>>
>

--001a11451db6222898054e43519b
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">Thanks!</div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_q=
uote">On Sat, Apr 22, 2017 at 12:15 PM, Michael C <span dir=3D"ltr">&lt;<a =
href=3D"mailto:[email protected]" target=3D"_blank">mysecretro=
[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex=
"><div dir=3D"ltr">Never mind, I found this<div><a href=3D"https://books.go=
ogle.ca/books?id=3D9MS9BQAAQBAJ&amp;pg=3DPA116&amp;lpg=3DPA116&amp;dq=3Dpyt=
hon+device+context+save&amp;source=3Dbl&amp;ots=3DtYhl4K1K6J&amp;sig=3DzgzJ=
mOFeOIZL2C5eGrZ-Oa_QRB4&amp;hl=3Den&amp;sa=3DX&amp;ved=3D0ahUKEwijjIvX37jTA=
hVQ42MKHVIFCHEQ6AEIOjAE#v=3Donepage&amp;q=3Dpython%20device%20context%20sav=
e&amp;f=3Dfalse" target=3D"_blank">https://books.google.ca/books?<wbr>id=3D=
9MS9BQAAQBAJ&amp;pg=3DPA116&amp;lpg=3D<wbr>PA116&amp;dq=3Dpython+device+<wb=
r>context+save&amp;source=3Dbl&amp;ots=3D<wbr>tYhl4K1K6J&amp;sig=3D<wbr>zgz=
JmOFeOIZL2C5eGrZ-Oa_QRB4&amp;<wbr>hl=3Den&amp;sa=3DX&amp;ved=3D<wbr>0ahUKEw=
ijjIvX37jTAhVQ42MKHVIFC<wbr>HEQ6AEIOjAE#v=3Donepage&amp;q=3D<wbr>python%20d=
evice%20context%<wbr>20save&amp;f=3Dfalse</a><br></div><div><br></div><div>=
thanks all</div></div><div class=3D"HOEnZb"><div class=3D"h5"><div class=3D=
"gmail_extra"><br><div class=3D"gmail_quote">On Sat, Apr 22, 2017 at 11:52 =
AM, Michael C <span dir=3D"ltr">&lt;<a href=3D"mailto:mysecretrobotfactory@=
gmail.com" target=3D"_blank">mysecretrobotfactory@gmail.<wbr>com</a>&gt;</s=
pan> 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">Quick Questi=
on:<div>I am using this code, and I have tried to find answers with google,=
 and I have read through MSDN.</div><div><br></div><div>How do I save the D=
C into a png file or bmp?</div><div><br></div><div>thanks!</div><div><br></=
div><div><br></div><div><div>import time</div><div>import ctypes</div><div>=
##from ctypes import *</div><div>## I don&#39;t like this. I like to import=
.</div><div><br></div><div>time.sleep(2)</div><div><br></div><div># Set up =
the User32 and GDI32</div><div>User32 =3D ctypes.WinDLL(&#39;User32&#39;, u=
se_last_error=3DTrue)</div><div>GDI32 =3D ctypes.WinDLL(&#39;GDI32&#39;, us=
e_last_error=3DTrue)</div><div><br></div><div># Set up Fx to be used.</div>=
<div>GetForegroundWindow =3D User32.GetForegroundWindow</div><div>GetWindow=
DC =3D User32.GetWindowDC</div><div>GetPixel =3D GDI32.GetPixel</div><div>R=
eleaseDC =3D User32.ReleaseDC</div><div><br></div><div><br></div><div># mai=
n code</div><div>foreground_window =3D GetForegroundWindow()</div><div>dc =
=3D GetWindowDC(foreground_window)</div><div><br></div><div># set timer to =
see how fast it runs.</div><div>t0 =3D time.clock()</div><div><br></div><di=
v>for i in range(50):</div><div>=C2=A0 =C2=A0 rgb =3D GetPixel(dc, 10, 10)<=
/div><div>=C2=A0 =C2=A0 #print(rgb)</div><div>=C2=A0 =C2=A0 r =3D rgb &amp;=
 0xff</div><div>=C2=A0 =C2=A0 g =3D (rgb &gt;&gt; 8) &amp; 0xff</div><div>=
=C2=A0 =C2=A0 b =3D (rgb &gt;&gt; 16) &amp; 0xff</div><div>## =C2=A0 =C2=A0=
print(&quot;RGB(%d, %d, %d)&quot; % (r, g, b))</div><div><br></div><div>pri=
nt(time.clock() - t0)</div><div>ReleaseDC(foreground_window, dc)</div></div=
><div><br></div></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>

--001a11451db6222898054e43519b--


--===============3167679717153051998==
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
--===============3167679717153051998==
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

--===============3167679717153051998==--