472,973 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,973 software developers and data experts.

win32ui screen capture

i,
I have trying to code a small console app that will
allow a user to select a window and then create a
screen capture of the window. I haven't been able to
figure out how to do the screen capture part. The
code is as follows, the commented out lines at the end
are things I have tried, but don't work. Whenever I
try to create a compatibe Bitmap or DC it comes back
with an error as
win32ui: CreateCompatibleDC failed
or
AttributeError: CreateCompatibleDC
Any help would be greatly appreciated.

import win32gui
import win32ui

numWindows = 0
windows = []
winDict = {}
windowsText=[]
inc = 0

def _MyCallback( hwnd, extra ):
extra.append(hwnd)
win32gui.EnumWindows(_MyCallback, windows)
for i in windows:
if win32gui.IsWindowVisible(i):
if win32gui.IsWindowVisible:
windowsText.append(i)
winDict[numWindows] = i
numWindows +=1


print "Please select a window to capture"
for window in windowsText:
windowText = win32gui.GetWindowText(window)
print inc, windowText
inc += 1

selection = input()

print win32gui.GetWindowText(winDict[selection])
myDC = win32ui.CreateDCFromHandle(winDict[selection])
win32gui.SetForegroundWindow(winDict[selection])
win_sz = win32gui.GetClientRect(winDict[selection])
myBitMap = win32ui.CreateBitmap()
#myMemDC = win32ui.CreateCompatibleDC(myDC)
#myBitMap.BitBlt((0,0),(win_sz[2],win_sz[3],myDC,(0,0),0))
#myBitMap.CreateCompatibleBitMap(myDC,win_sz[2], win_sz[3])
#myBitMap.CreateCompatibleDC(myDC)
Thanks,
Rob
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 18 '05 #1
3 13226
On Fri, 27 Jun 2003 05:11:04 GMT, "Mark S Pryor"
<ma**************@CHENEYverizon.net> wrote:
Hi,
<Me Mine> wrote in message
news:o4********************************@4ax.com.. .
i,
I have trying to code a small console app that will
allow a user to select a window and then create a
screen capture of the window. I haven't been able to
figure out how to do the screen capture part. The
code is as follows, the commented out lines at the end
are things I have tried, but don't work. Whenever I
try to create a compatibe Bitmap or DC it comes back
with an error as
win32ui: CreateCompatibleDC failed
or
AttributeError: CreateCompatibleDC
Any help would be greatly appreciated.

import win32gui
import win32ui

numWindows = 0
windows = []
winDict = {}
windowsText=[]
inc = 0

def _MyCallback( hwnd, extra ):
extra.append(hwnd)
win32gui.EnumWindows(_MyCallback, windows)
for i in windows:
if win32gui.IsWindowVisible(i):
if win32gui.IsWindowVisible:
windowsText.append(i)
winDict[numWindows] = i
numWindows +=1


print "Please select a window to capture"
for window in windowsText:
windowText = win32gui.GetWindowText(window)
print inc, windowText
inc += 1

selection = input()

print win32gui.GetWindowText(winDict[selection])
myDC = win32ui.CreateDCFromHandle(winDict[selection])
win32gui.SetForegroundWindow(winDict[selection])
win_sz = win32gui.GetClientRect(winDict[selection])
myBitMap = win32ui.CreateBitmap()
#myMemDC = win32ui.CreateCompatibleDC(myDC)
#myBitMap.BitBlt((0,0),(win_sz[2],win_sz[3],myDC,(0,0),0))
#myBitMap.CreateCompatibleBitMap(myDC,win_sz[2], win_sz[3])
#myBitMap.CreateCompatibleDC(myDC)
Thanks,
Rob


You may be able to make this work ... eventually. However,
circulating in the Python community you will find the C file
_grabscreen.c with all the same API calls you are trying to use
in standard C.

see this post
http://groups.google.com/groups?selm...ython-list%40p
ython.org

It explains how to compile the module _grabscreen.pyd with MS Visual C.
I was able to get a screen shot script in 10 minutes after I read this
post. If you don't have a grabscreen module already, see this ZIP file
for the relevant files. Use the file ImageGrab1.py to make a screen
capture of the desktop and save the results as PNG.

(for ActivePython 2.2 only)
http://mysite.verizon.net/res1ur2j/GrabScreen.zip

you will need PIL from
http://www.pythonware.com/products/pil/

hth,
Mark Pryor

Thanks for reponding Mark. I have looked at the grab function in
ImagrGrab from PIL. And it does do exactly what I want it to do,
which is great, but I was hoping to do the program using win32ui and
win32gui so that I could get a better understanding over how Windows
(the OS) operates. The part that bothers me (and the reason why I'm
pursueing this) is because in MS C++ the BitBlt function can take the
Window DC directly as opposed to the BitBlt function in python, which
cannot. Python's BitBlt function (for PyCDC objects in win32ui), can
only take other PyCDC objects for the source of the copy.
Unfortunatley, once I have the window handle I can call the
GetWindowDC method, but it returns HDC Yet, I unable to use this
object to call BitBlt on. When I uncomment the the BitBlt line of
code I get a n error of

TypeError: The 'O' param must be a PyCDC object

So, it appears that I need to do something with the HDC, so I can do a
bitblt transer to the memory DC, but I don't know how to get a PyCDC
object from a HDC.

Thanks,

Rob
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Jul 18 '05 #2

<Me Mine> wrote in message
news:bn********************************@4ax.com...
Hi again,

ok, so now I'm closer. I can create a compatible memory dc and a new
bitmap, which is always black is created. But I can't figure out how
to BitBlt to the memory DC from the window DC. The line is commented


I tried for 30 minutes, but no joy. I too get a black BMP result.

One suggestion. Add this
import win32con as con

In bltbit( , , , 204) use
con.SRCCOPY) instead

hth,
Mark Pryor
Jul 18 '05 #3
Mark S Pryor wrote:
You may be able to make this work ... eventually. However,
circulating in the Python community you will find the C file
_grabscreen.c with all the same API calls you are trying to use
in standard C.

see this post
http://groups.google.com/groups?selm...ython-list%40p
ython.org

It explains how to compile the module _grabscreen.pyd with MS Visual C.
I was able to get a screen shot script in 10 minutes after I read this
post. If you don't have a grabscreen module already, see this ZIP file
for the relevant files. Use the file ImageGrab1.py to make a screen
capture of the desktop and save the results as PNG.

(for ActivePython 2.2 only)
http://mysite.verizon.net/res1ur2j/GrabScreen.zip

you will need PIL from
http://www.pythonware.com/products/pil/


note that PIL 1.1.3 and later includes screen grabbing facilities;
for details, see:

http://www.effbot.org/zone/pil-imagegrab.htm

</F>


Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: TerryWilson | last post by:
I am developing a web based service tool using asp.net that we will distribute with our product. This service tool will be used for remotely configuring the product, problem determination, etc. ...
0
by: D Sheldon | last post by:
Has anyone ever created a screen capture method for ASP.Net? What I have in mind is adding a method to my custom error logging class that will take a screen capture of the user's browser and then...
4
by: gregory_may | last post by:
http://www.geocities.com/krishnapg/screencap.html This article gives a pretty good background on various ways to capture the screen. I have some code to do it the GDI way below, but I cant...
2
by: Eddie Dunn | last post by:
I have one here that I cannot find anything on in my searching. I am implementing a screen capture functionality into my Visual Basic ..NET application. The code I am using calls the bitblt...
2
by: py | last post by:
I need to take a screen shot of the computer screen. I am trying to use PIL and I saw there is ImageGrab...however it only works on Windows. Is there a platform-independent ability to work...
2
by: dumbledad | last post by:
Hi All, I'm using ASP.Net web services to provide the logic required for a Flash based prototype. The Flash and the ASP .Net run together on the client machine. One of the functions I'd like to...
1
by: JP2006 | last post by:
I'm trying to write a control that will take a screen capture of a particular website when a user submits a form in a web application; one of the form fields is for a URL - the control needs to get...
0
by: Jim McGivney | last post by:
I have goggled "Asp.net Screen Capture" and looked at many articles, but have never succeeded in a screen capture. Has anyone been able to accomplish Screen Capture in 2005 VWD C# ? Let me know...
2
by: raylopez99 | last post by:
Beware newbies: I spent a day before I figured this out: copying a bitmap (image) file to file is not quite like copying a text file--you have to do some tricks (see below), like using a...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.