"""
The idea is render a bitmap using memoryDC and keet it.
Later draw this bitmap on another memoryDC operation.
Problem is that bitmap is loosing it's transparency.
The code below will show you the problem
"""
# -*- coding: iso-8859-1 -*-#
#!/usr/bin/env python2.4
import wx
class DemoApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers() # called so a PNG can be saved
# Render a bitmap using MemoryDC.
# We are drawing an ellipse thus we need a bitmap with
# transparency. We are going to keep this bitmap for later...
width, height = 100, 50
size = width, height
colorBitmap = wx.EmptyBitmapRGBA(size[0], size[1], 0, 0, 0,
0)
mdc = wx.MemoryDC(colorBitmap)
mdc.SetPen(wx.Pen("white", 1))
mdc.SetBrush(wx.Brush("red"))
mdc.SetDeviceOrigin(0, 0)
mdc.DrawEllipse(0, 0, width, height)
# Select the Bitmap out of the memory DC
# by selecting a new uninitialized Bitmap
mdc.SelectObject(wx.NullBitmap)
del mdc
# Create another bitmap using memoryDC and
# draw the bitmap created above.
newBitmap = wx.EmptyBitmap(*size)
mdc = wx.MemoryDC()
mdc.SelectObject(newBitmap)
# Draw bitmap with a gray background
mdc.SetBackground(wx.Brush("gray"))
mdc.Clear() # make sure you clear the bitmap!
mdc.DrawBitmap(colorBitmap, 0, 0)
# Select the Bitmap out of the memory DC
# by selecting a new uninitialized Bitmap
mdc.SelectObject(wx.NullBitmap)
del mdc
# BUG:
# Save file and you can see that ellipse is saved
# with some level of transparency. It's not complete red.
img = newBitmap.ConvertToImage()
fileName = "memdc.png"
img.SaveFile(fileName, wx.BITMAP_TYPE_PNG)
return True
if __name__ == "__main__":
print "about to initialize the app"
app = DemoApp(0)
app.MainLoop()