RE: [wxPython-users] Grab screen in wxpython

42 views
Skip to first unread message

Gavana, Andrea

unread,
Nov 8, 2006, 8:38:55 AM11/8/06
to wxPytho...@lists.wxwidgets.org
Hi Giovanni,
 
> Is there a way to grab an image from the screen using wxpython?
 
If you mean taking a screenshot of one widget or one part of the application, you may do something like:
 
def TakeScreenShot(rect):
    """ Takes a screenshot of the screen at give pos & size (rect). """
 
    #Create a DC for the whole screen area
    dcScreen = wx.ScreenDC()
 
    #Create a Bitmap that will later on hold the screenshot image
    #Note that the Bitmap must have a size big enough to hold the screenshot
    #-1 means using the current default colour depth
    bmp = wx.EmptyBitmap(rect.width, rect.height)
 
    #Create a memory DC that will be used for actually taking the screenshot
    memDC = wx.MemoryDC()
 
    #Tell the memory DC to use our Bitmap
    #all drawing action on the memory DC will go to the Bitmap now
    memDC.SelectObject(bmp)
 
    #Blit (in this case copy) the actual screen on the memory DC
    #and thus the Bitmap
    memDC.Blit( 0, #Copy to this X coordinate
        0, #Copy to this Y coordinate
        rect.width, #Copy this width
        rect.height, #Copy this height
        dcScreen, #From where do we copy?
        rect.x, #What's the X offset in the original DC?
        rect.y  #What's the Y offset in the original DC?
        )
 
    #Select the Bitmap out of the memory DC by selecting a new
    #uninitialized Bitmap
    memDC.SelectObject(wx.NullBitmap)
 
    return bmp
 
 
HTH.
 
Andrea.
 

_________________________________________

Andrea Gavana (gav...@kpo.kz)

Reservoir Engineer

KPDL

4, Millbank

SW1P 3JA London

 

Direct Tel: +44 (0) 20 717 08936

Mobile Tel: +44 (0) 77 487 70534

Fax: +44 (0) 20 717 08900

Web: http://xoomer.virgilio.it/infinity77

¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

 

 


From: Giovanni Porcari [mailto:giovanni...@gmail.com]
Sent: 08 November 2006 13:31
To: wxpython-users
Subject: [wxPython-users] Grab screen in wxpython

Hi all
Is there a way to grab an image from the screen using wxpython?
I know that it could be done using PIL ImageGrab.grab(bbox) => image but unfortunately it is only for windows.

TIA

G

Giovanni Porcari

unread,
Nov 8, 2006, 8:31:06 AM11/8/06
to wxpython-users

Porcari Giovanni

unread,
Nov 8, 2006, 8:44:07 AM11/8/06
to wxPytho...@lists.wxwidgets.org
Thank you Andrea

  :) 

G

Il giorno 08/nov/06, alle ore 14:38, Gavana, Andrea ha scritto:

Josiah Carlson

unread,
Nov 8, 2006, 12:04:44 PM11/8/06
to Gavana, Andrea, wxPytho...@lists.wxwidgets.org

That is very nifty.

If one uses the print screen button on Windows, then one can also use
something like the following...

def GetClipboardImage():
success = False
do = wx.BitmapDataObject()
if wx.TheClipboard.Open():
success = wx.TheClipboard.GetData(do)
wx.TheClipboard.Close()

if success:
return do.GetBitmap()
return None

- Josiah

Reply all
Reply to author
Forward
0 new messages