Hi Ben,
I’m trying to draw an image with alpha to save out as a PNG by using a wxMemoryDC with a wxBitmap. The problem I’m having is that when I save it my alpha channel is completely black, regardless of what I’ve drawn. The only exception is that if I use DrawBitmap to draw a bitmap I loaded off of disk with alpha, that alpha does save. Here’s a sample:
bmp = wx.EmptyBitmap(512,512)
bmp.UseAlpha()
dc = wx.MemoryDC(bmp)
dc.SetBrush(wx.WHITE_BRUSH)
dc.DrawCircle(256,256,256)
dc.SelectObject(wx.NullBitmap)
bmp.SaveFile(“test.bmp”,wx.BITMAP_TYPE_BMP)In this example I saved it as a BMP instead of PNG because that way when the alpha channel is all black I can at least still see what is in the RGB channels. In this case I get a white circle in RGB but all black in alpha. I’ve also tried wrapping that wxMemoryDC in a wxGraphicsContext but the result is the same. Asking dc.GetPixel(256,256) does show the alpha as 255, but in the bitmap it is 0. I’ve confirmed this by saving the image (as above), also by converting it to a wxImage and using GetAlpha, and also by using CopyToBuffer.
The following example, however, does work to produce a white circle with transparent around it (assuming that’s what’s in circle.png to begin with):
circle = wx.EmptyBitmap(0,0)
circle.LoadFile(“circle.png”,wx.BITMAP_TYPE_ANY)
bmp = wx.EmptyBitmap(512,512)
bmp.UseAlpha()
dc = wx.MemoryDC(bmp)
dc.DrawBitmap(circle,0,0)
dc.SelectObject(wx.NullBitmap)
bmp.SaveFile(“test.bmp”,wx.BITMAP_TYPE_BMP)With this example there is a white circle in RGB and alpha channels.
Is there something I can do to be able to draw into a bitmap that has alpha?
In addition to the other suggestions you’ve gotten, make sure that the wx.EmptyBitmap you create is a 32-bit bitmap by specifying wx.EmptyBitmap(x, y, depth=32). I believe the default color depth for a wx.Bitmap under Windows is 16 bit. Windows does some weird things under the hood to ‘up convert’ the depth automagically, but I’ve found it sometimes doesn’t work and you have to explicitly set the depth yourself.
Regards,
Kevin
···
On Dec 29, 2012, at 8:03 PM, Ben Buchwald wrote:
I’m using wxPython 2.8.9.1 on Windows XP.
–
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en