win32ui screen capture

Discussion in 'Python' started by Me Mine, Jun 27, 2003.

  1. Me Mine

    Me Mine Guest

    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 =---
     
    Me Mine, Jun 27, 2003
    #1
    1. Advertising

  2. Me Mine

    Me Mine Guest

    On Fri, 27 Jun 2003 05:11:04 GMT, "Mark S Pryor"
    <> wrote:

    >Hi,
    ><Me Mine> wrote in message
    >news:eek:...
    >> 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=mailman.1011217637.9447.python-list@p
    >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 =---
     
    Me Mine, Jun 27, 2003
    #2
    1. Advertising

  3. Me Mine

    Mark S Pryor Guest

    <Me Mine> wrote in message
    news:...
    > 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
     
    Mark S Pryor, Jun 27, 2003
    #3
  4. 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=mailman.1011217637.9447.python-list@p
    > 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>
     
    Fredrik Lundh, Jun 27, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    7
    Views:
    9,157
  2. Howard Lightstone

    win32ui.CreatePrintDialog argument question

    Howard Lightstone, Jun 28, 2003, in forum: Python
    Replies:
    0
    Views:
    746
    Howard Lightstone
    Jun 28, 2003
  3. EricP
    Replies:
    1
    Views:
    446
    Roger Upole
    Oct 28, 2003
  4. Chris Gonnerman

    win32ui.CreateFont() question

    Chris Gonnerman, Nov 21, 2003, in forum: Python
    Replies:
    0
    Views:
    451
    Chris Gonnerman
    Nov 21, 2003
  5. Corwan
    Replies:
    1
    Views:
    492
    Mark Hammond
    Feb 1, 2004
Loading...

Share This Page