transparent background in wxImage

How do I make a transparent background in wxImage?

The following code just makes it black:

        b=wx.EmptyBitmap(width, height)
        mdc=wx.MemoryDC()
        mdc.SelectObject(b)
        mdc.SetBackground(wx.TRANSPARENT_BRUSH)
        mdc.Clear()

Setting Background to other colours causes them to be
used. Since EmptyBitmap gives you a random chunk of
memory, it has to be cleared first.

Roger

Roger Binns wrote:

How do I make a transparent background in wxImage?

The following code just makes it black:

        b=wx.EmptyBitmap(width, height)
        mdc=wx.MemoryDC()
        mdc.SelectObject(b)
        mdc.SetBackground(wx.TRANSPARENT_BRUSH)
        mdc.Clear()

Setting Background to other colours causes them to be
used. Since EmptyBitmap gives you a random chunk of
memory, it has to be cleared first.

Set the mask to be the colour you use to clear the image.

···

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

How do I make a transparent background in wxImage?

The following code just makes it black:

        b=wx.EmptyBitmap(width, height)
        mdc=wx.MemoryDC()
        mdc.SelectObject(b)
        mdc.SetBackground(wx.TRANSPARENT_BRUSH)
        mdc.Clear()

Setting Background to other colours causes them to be
used. Since EmptyBitmap gives you a random chunk of
memory, it has to be cleared first.

Set the mask to be the colour you use to clear the image.

I cannot get that to work. I can make it have a black
background with my image, a white background with my
image, or a transparent background with no image. What I
want is to create a bitmap (eg 100x100) draw a smaller bitmap
into that, and then save the result as a PNG. Here was my
code taking in the above suggestion trying to work:

   blackmask=wx.EmptyBitmap(width, height, 1)
   mdc=wx.MemoryDC()
   mdc.SelectObject(blackmask)
   mdc.SetBackground(wx.BLACK_BRUSH)
   mdc.Clear()
   whitemask=wx.EmptyBitmap(width, height, 1)
   mdc.SelectObject(whitemask)
   mdc.SetBackground(wx.WHITE_BRUSH)
   mdc.Clear()
   mdc.SelectObject(wx.NullBitmap)
        
   b=wx.EmptyBitmap(width, height)
   b.SetMask(wx.Mask(blackmask))
   mdc.SelectObject(b)
   mdc.Clear()
   # mdc.SetBackground(wx.TRANSPARENT_BRUSH)
   b.SetMask(wx.Mask(whitemask))
   # b.SetMask(wx.Mask(wx.NullBitmap))
   # b.SetMask(wx.Mask(blackmask))

   mdc.DrawBitmap(img.ConvertToBitmap(), (width-img.GetWidth())/2, (height-img.GetHeight())/2, True)
   mdc.SelectObject(wx.NullBitmap)

   b.SaveFile("test.png", wx.BITMAP_TYPE_PNG)

Roger

Roger Binns wrote:

How do I make a transparent background in wxImage?

The following code just makes it black:

       b=wx.EmptyBitmap(width, height)
       mdc=wx.MemoryDC()
       mdc.SelectObject(b)
       mdc.SetBackground(wx.TRANSPARENT_BRUSH)
       mdc.Clear()

Setting Background to other colours causes them to be
used. Since EmptyBitmap gives you a random chunk of
memory, it has to be cleared first.

Set the mask to be the colour you use to clear the image.

I cannot get that to work. I can make it have a black
background with my image, a white background with my
image, or a transparent background with no image. What I
want is to create a bitmap (eg 100x100) draw a smaller bitmap
into that, and then save the result as a PNG. Here was my
code taking in the above suggestion trying to work:

Try this:

    b=wx.EmptyBitmap(width, height)

    mdc.SelectObject(b)
    mdc.SetBackground(wx.BLACK_BRUSH)
    mdc.Clear()
    mdc.DrawBitmap(img.ConvertToBitmap(), (width-img.GetWidth())/2, (height-img.GetHeight())/2, True)
    mdc.SelectObject(wx.NullBitmap)

    mask = wxMaskColour(b, WX.BLACK)
    b.SetMask(mask)

···

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

Try this:

    b=wx.EmptyBitmap(width, height)

    mdc.SelectObject(b)
    mdc.SetBackground(wx.BLACK_BRUSH)
    mdc.Clear()
    mdc.DrawBitmap(img.ConvertToBitmap(), (width-img.GetWidth())/2,
(height-img.GetHeight())/2, True)
    mdc.SelectObject(wx.NullBitmap)

    mask = wxMaskColour(b, WX.BLACK)
    b.SetMask(mask)

Thanks, that did the trick. Since some of my images had black in them,
I used img.FindFirstUnusedColour and used that as the background and
mask colour.

Roger

Roger Binns wrote:

Try this:

   b=wx.EmptyBitmap(width, height)

   mdc.SelectObject(b)
   mdc.SetBackground(wx.BLACK_BRUSH)
   mdc.Clear()
   mdc.DrawBitmap(img.ConvertToBitmap(), (width-img.GetWidth())/2,
(height-img.GetHeight())/2, True)
   mdc.SelectObject(wx.NullBitmap)

   mask = wxMaskColour(b, WX.BLACK)
   b.SetMask(mask)

Thanks, that did the trick. Since some of my images had black in them,
I used img.FindFirstUnusedColour and used that as the background and
mask colour.

That's what it's there for. :wink:

···

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