cross-platform screenshot of a *widget only*?

Need a little help on the best approach to get a cross-platform way to take a screenshot of just one widget in an app. I was using an example from Andrea, and then adapted at several places online, but it isn’t working for me (wx2.8.10.1, WinXP, Py2.5):

For example, this version of it, here:
http://stackoverflow.com/questions/2195792/saving-an-image-of-what-a-dc-drew-wxpython

What is dcSource? How do I get that to use it in the function?

Thanks,
Che

Hi Che,

Need a little help on the best approach to get a cross-platform way to take
a screenshot of just one widget in an app. I was using an example from
Andrea, and then adapted at several places online, but it isn't working for
me (wx2.8.10.1, WinXP, Py2.5):

For example, this version of it, here:
python - Saving an image of what a device context drew, wxPython - Stack Overflow

What is dcSource? How do I get that to use it in the function?

dcSource is a wx.ScreenDC. My suggestion for you would be to look at
the source code of AUI in AGW, and in particular at the
aui_utilities.py. There is a funtion called "TakeScreenShot" that
should do what you ask. Typically, for a frame, you may use it like
this:

rect = my_frame.GetScreenRect()
bitmap = TakeScreenShot(rect)

# Do whatever with the bitmap.

Be careful however, wx.ScreenDC might be unsupported (broken?) on Mac.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

···

On 16 February 2012 21:55, C M wrote:

Hi Che,

Hi Che,

Need a little help on the best approach to get a cross-platform way to take
a screenshot of just one widget in an app. I was using an example from
Andrea, and then adapted at several places online, but it isn't working for
me (wx2.8.10.1, WinXP, Py2.5):

For example, this version of it, here:
python - Saving an image of what a device context drew, wxPython - Stack Overflow

What is dcSource? How do I get that to use it in the function?

dcSource is a wx.ScreenDC. My suggestion for you would be to look at
the source code of AUI in AGW, and in particular at the
aui_utilities.py. There is a funtion called "TakeScreenShot" that
should do what you ask. Typically, for a frame, you may use it like
this:

rect = my_frame.GetScreenRect()
bitmap = TakeScreenShot(rect)

# Do whatever with the bitmap.

Be careful however, wx.ScreenDC might be unsupported (broken?) on Mac.

Just for the fun of it, I have put together a small script showing
this screenshot/dragging stuff for a wx.TextCtrl inside a panel. The
script is very rough, and it could use some enhancements (i.e., use a
wx.ScrolledWindow instead of a wx.Panel as container, limit the bitmap
screenshot movement inside the panel area only and so on). It could
also be easily modified to show how to drag other widgets (i.e., a
wx.TreeCtrl, or a wx.Slider, almost anything).

It works OK for me on Windows Vista, Python 2.7.2, wxPython 2.9.2.4
msw (classic).

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

dragtext.py (4.36 KB)

···

On 16 February 2012 22:37, Andrea Gavana wrote:

On 16 February 2012 21:55, C M wrote:

Hi, Andrea.

rect = my_frame.GetScreenRect()

bitmap = TakeScreenShot(rect)

That hadn’t (past tense) been working for me, because I was calling it early in the process of painting the window (though, in my defense, after all the calls to create the widgets) and what I was getting was part of the screen that was not even in the window. If I call it as an event-driven call (like binding it to a button to take a snapshot of it), then it works fine.

In my poking about, I also found this short example of a similar mechanism*, though perhaps this has some drawbacks that I am not aware of or isn’t as cross-platform:

def window_to_bitmap(self,window):

    width, height = window.GetClientSize()
    bitmap = wx.EmptyBitmap(width, height)
    wdc = wx.ClientDC(window)
    mdc = wx.MemoryDC(bitmap)
    mdc.Blit(0, 0, width, height, wdc, 0, 0)

    return bitmap

(*http://stackoverflow.com/questions/4773961/get-a-widgets-dc-in-wxpython)

Just for the fun of it, I have put together a small script showing

this screenshot/dragging stuff for a wx.TextCtrl inside a panel. The

script is very rough, and it could use some enhancements (i.e., use a

wx.ScrolledWindow instead of a wx.Panel as container, limit the bitmap

screenshot movement inside the panel area only and so on). It could

also be easily modified to show how to drag other widgets (i.e., a

wx.TreeCtrl, or a wx.Slider, almost anything).

Nice! Well, there it is then. Thanks, it’s fun to see in action! (wxpython 2.8.10.1, py2.5, winXP).

It works OK for me on Windows Vista, Python 2.7.2, wxPython 2.9.2.4

msw (classic).

So the million $ (or €) question: does it work well on Mac and Linux, folks?

Che