Access visible area of scrolledwindow?

I have a wx.ScrolledWindow and would like to save the visible area of the window (minus the scrollbars) as a PNG. I’ve been looking at the docs for wx.ScrolledWindow and wx.Scrolled and am not finding a way to access the contents of the visible window.

How can I extract the visible area (say, as a wx.Bitmap)?

If you are drawing the content of the window yourself, then you can organize your code so you can use the same drawing code to draw to the wx.PaintDC or to a wx.MemoryDC with a bitmap selected into it.

Otherwise you may (depending on the platform and other factors) be able to access the screen content via a wx.WindowDC or a wx.ScreenDC and use Blit to copy from the screen to a wx.MemoryDC.

Thanks for the helpful reply. So, if I understand correctly, I would override the scrolledWindows’ Draw method, let it do the default drawing, and then tap the DC from the result?

  • Eric

My suggestion is to refactor your drawing code such that it can be used both by the paint event handler and also by the make-a-bitmap code. Something like this:

Assuming the paint event is bound for the window like this:

    self.Bind(wx.EVT_PAINT, self.OnPaint)

Then factoring out the drawing code would look something like this:

    def DoDrawing(self, dc):
        # draw your stuff to the given dc

    def OnPaint(self, dc):
        dc = wx.PaintDC(self)

    def MakeBitmap(self, dc):
        bmp = wx.Bitmap(width, height)
        dc = wx.MemoryDC(bmp)
        return bmp