Draw Bitmap With Transparent Background

Is there a way to draw to a wxEmptyBitmap while maintaining
a transparent background. I’ve been trying to do it with a wxMemoryDC,
but it always fills the background. I know there is a hack to fill the
background to a unused color like (2,2,2) and then later make a wxMask of that
color. But this doesn’t help if you have faded alphas, like the ImageAlpha
in the wxDemo.

I basically want to draw a bunch things to the wxEmptyBitmap,
could be other bitmap or text… so that I can then crop that Bitmap before
drawing to another wxMemoryDC which is in turn drawn to a wxPaintDC which is
displayed on screen.

I might have to find a way of doing a screen grab of the
MemoryDC and blit that down before I start drawing the bitmap…

Keith wrote:

Is there a way to draw to a wxEmptyBitmap while maintaining a transparent background. I’ve been trying to do it with a wxMemoryDC, but it always fills the background. I know there is a hack to fill the background to a unused color like (2,2,2) and then later make a wxMask of that color. But this doesn’t help if you have faded alphas, like the ImageAlpha in the wxDemo.

I think I would try something like this:

  i = wx.EmptyImage(w, h)
  i.SetMaskColour(0,0,0)
  i.InitAlpha()
  bmp = i.ConvertToBitmap()

wx.EmptyImage will by default set all pixels to be black so the 2nd line simply makes the whole image be masked. Then when InitAlpha is called it will make all of the masked pixels (all of them in this case) have a fully transparent alpha level. Then when you draw other bitmaps onto the bmp the alpha of the source will be blended with the existing alpha. (I think.)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!