wx.PaintDC and SetBackgroundMode( wx.TRANSPARENT ) support

1,029 views
Skip to first unread message

Barry Scott

unread,
Feb 21, 2010, 10:31:06 AM2/21/10
to wxpytho...@googlegroups.com
It seems that only Mac OS X supports drawing in wx.TRANSPARENT mode with a DC.
On Windows and Linux (Fedora-12) no alpha blending takes place.

The code I'm using is:

dc = wx.PaintDC( self )
dc.BeginDrawing()
dc.SetBackgroundMode( wx.SOLID )

dc.DrawBitmap( self.editor_bitmap, 0, 0, False )

c_x, c_y = self.__pixelPoint( self.cursor_x, self.cursor_y )

# alpha blend the cursor
dc.SetBackgroundMode( wx.TRANSPARENT )
cursor_colour = wx.Colour( 0, 0, 0, 92 )
dc.SetPen( wx.Pen( cursor_colour ) )
dc.SetBrush( wx.Brush( cursor_colour ) )
dc.DrawRectangle( c_x, c_y, self.char_width, self.char_length )

dc.EndDrawing()

Is this expected?

I'm using these wx versions:

'2.8.10.1 (mac-unicode)'
'2.8.9.2 (msw-unicode)'
'2.8.9.2 (gtk2-unicode)'

Barry

King

unread,
Feb 21, 2010, 11:09:58 AM2/21/10
to wxPython-users
I myself faced many problems with DC's and transparency+bitmaps.
DC's actually used native graphic context in order to render drawing.
(GDI on 2k, GDI+ on XP and such).

I you are on XP 32 then use wx.GCDC or wx.GraphicsContext. This
internally uses GDI+ on XP which supports transparency and anti-
aliased drawing. Have a look in Demo->Using Images> Alpha Drawing or
Demo>Miscellaneous>GraphicsContext example.

On GTK aka Linux I would suggest you to check the above examples or a
better and faster option would be use wx.lib.wxcairo. Robin Dunn has
done a great job (Thanks Robin;-)) and wrapped it using
wx.lib.graphics. I have personally used it on ms-win and Linux and it
has solved all the problems that I was facing with native DCs as well
as a slight performance boost in drawing operations.

Try this in above code if you are in hurry:
Replace:
dc = wx.PaintDC(self)

With:
pdc = wx.PaintDC(self)
dc = wx.GCDC(pdc)

Hope it helps.

If it doesn't, comment all the dc.SetBackgroundMode*.* commands and
check one more time.

Prashant

Barry Scott

unread,
Feb 22, 2010, 2:55:38 PM2/22/10
to wxpytho...@googlegroups.com

Thanks that fixed the problems.

I now have transparency work on Windows, Mac and Linux GTK.

>
> If it doesn't, comment all the dc.SetBackgroundMode*.* commands and
> check one more time.
>
> Prashant
>

> --
> To unsubscribe, send email to wxPython-user...@googlegroups.com
> or visit http://groups.google.com/group/wxPython-users?hl=en

Barry

Robin Dunn

unread,
Feb 22, 2010, 4:06:03 PM2/22/10
to wxpytho...@googlegroups.com
On 2/21/10 7:31 AM, Barry Scott wrote:
> It seems that only Mac OS X supports drawing in wx.TRANSPARENT mode with a DC.

First of all, you appear to be misunderstanding what SetBackgroundMode(
wx.TRANSPARENT ) is for. It only effects the drawing of text, (whether
it is given a solid background color or not) not all the drawing
operations on the DC.

> On Windows and Linux (Fedora-12) no alpha blending takes place.
>

Secondly, on the Mac the wx.DC class is implemented using a
wx.GraphicsContext (sort of, IIRC they actually both use the same code
underneath the covers) so it is in effect the same as using wx.GCDC on
the other platforms, so that is why setting the alpha on the brush color
works there. This is technically an "implementation detail" and not a
bug. If you assume that wx.DC does not support alpha on the Mac (and
therefore don't try to use it) then it will work approximately the same
as wx.DC on the other platforms.

In an nutshell, wx.DC is designed for old-style 24-bit raster based
drawing with no alpha nor anti-aliasing other than drawing bitmaps that
have alpha and drawing text, but only if the platform automatically
anti-aliases text. wx.GraphicsContext on the other hand is designed to
be implemented using the new-style drawing APIs available on each
platform (GDI+, CoreGraphics, or Cairo) and is meant to be more vector
based and to fully support alpha and anti-aliasing. wx.GCDC is a hybrid
of the two where the DC API is reimplemented using wx.GraphicsContext.
So if you want to draw with alpha then you need to switch to using
wx.GraphicsContext or use wx.GCDC.


--
Robin Dunn
Software Craftsman
http://wxPython.org

Barry Scott

unread,
Feb 22, 2010, 7:06:16 PM2/22/10
to wxpytho...@googlegroups.com

If I got this right all I need to do is use colours with alpha and draw with a wx.GCDC.
I can remove the call to SetBackgroundMode( wx.TRANSPARENT ) as its not affecting DrawRectangle at all.

Barry

Reply all
Reply to author
Forward
0 new messages