May be cairo's DrawBimap function is not able to read properly or some
explanation
is required here.
"""
# -*- coding: iso-8859-1 -*-#
#!/usr/bin/env python2.4
import wx
import wx.lib.graphics as Cairo
class DemoApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers() # called so a PNG can be saved
font = wx.FFont(9, wx.SWISS, wx.FONTFLAG_BOLD, "Lucid
Console")
# Render a bitmap using MemoryDC.
width, height = 100, 100
size = width, height
colorBitmap = wx.EmptyBitmapRGBA(size[0]+1, size[1]+1, 0, 0,
0, 0)
mdc = wx.MemoryDC(colorBitmap)
ctx = wx.GraphicsContext.Create(mdc)
#Cairo.GraphicsContext.Create(mdc)
ctx.SetFont(font, "blue")
ctx.DrawText("wxPython Rocks!!!", 0, 45)
mdc.SelectObject(wx.NullBitmap)
del mdc
colorBitmap.SaveFile("text.png", wx.BITMAP_TYPE_PNG)
# Create another bitmap using memoryDC and
# draw the bitmap created above.
newBitmap = wx.EmptyBitmapRGBA(size[0]+1, size[1]+1, 0, 0, 0,
0)
mdc = wx.MemoryDC(newBitmap)
mdc.SetBackground(wx.Brush("gray"))
mdc.Clear() # make sure you clear the bitmap!
mdc.DrawBitmap(colorBitmap, 0, 0)
mdc.SelectObject(wx.NullBitmap)
del mdc
newBitmap.SaveFile("memdc.png", wx.BITMAP_TYPE_PNG)
My guess is that Cairo is having some of the same problems with the
alpha-ignorant Windows DC that wx has had to deal with. If you avoid
using the wx.MemoryDC and do a little Cairo specfic things then it does
what I think it is you are wanting:
import wx.lib.wxcairo as wxcairo
colorBitmap = wx.EmptyBitmapRGBA(size[0]+1, size[1]+1, 0, 0, 0, 0)
surface = wxcairo.ImageSurfaceFromBitmap(colorBitmap)
ctx = Cairo.GraphicsContext.CreateFromSurface(surface)
ctx.SetFont(font, "blue")
ctx.DrawText("wxPython Rocks!!!", 0, 45)
del ctx
colorBitmap = wxcairo.BitmapFromImageSurface(surface)
colorBitmap.SaveFile("text.png", wx.BITMAP_TYPE_PNG)
# Create another bitmap using memoryDC and
# draw the bitmap created above.
newBitmap = wx.EmptyBitmap(size[0]+1, size[1]+1)
mdc = wx.MemoryDC(newBitmap)
mdc.SetBackground(wx.Brush("light grey"))
mdc.Clear() # make sure you clear the bitmap!
mdc.DrawBitmap(colorBitmap, 0, 0, True)
mdc.SelectObject(wx.NullBitmap)
del mdc
newBitmap.SaveFile("memdc.png", wx.BITMAP_TYPE_PNG)
Now I started to test my app on Linux and none of the rendering method
is working properly. Cairo is badly crashing the
application. See here:
http://groups.google.com/group/wxpython-users/browse_thread/thread/89d0b0054eb8d1e4/6730a4f916b5c5cf#6730a4f916b5c5cf
and again bitmap transparency issue has risen. I am rendering off-line
bitmaps and then drawing them later. The alpha(transparency) is
replaced by pure black color in the bitmap. Here is code that I using
to draw a bitmap
import wx
class DemoApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers() # called so a PNG can be saved
font = wx.FFont(9, wx.SWISS, wx.FONTFLAG_BOLD, "LucidConsole")
# Render a bitmap using MemoryDC.
width, height = 100, 100
size = width, height
colorBitmap = wx.EmptyBitmapRGBA(size[0]+1, size[1]+1, 0, 0,
0, 0)
mdc = wx.MemoryDC(colorBitmap)
ctx = wx.GraphicsContext.Create(mdc)
ctx.SetFont(font, "blue")
ctx.DrawText("wxPython Rocks!!!", 0, 45)
ctx.SetBrush(wx.Brush("red"))
ctx.DrawRectangle(10,10,25,25)
# Select the Bitmap out of the memory DC by selecting a new
# uninitialized Bitmap
mdc.SelectObject(wx.NullBitmap)
del mdc
# Save file
colorBitmap.SaveFile("text.png", wx.BITMAP_TYPE_PNG)
return True
if __name__ == "__main__":
print "about to initialize the app"
app = DemoApp(0)
app.MainLoop()
The bitmap is coming empty and instead of wx.EmptyBitmapRGBA if you
use wx.EmptyBitmap, bitmap is coming out with
a text and a red colour rectangle in it but also some garbage is
pixels is coming out.
I must find a solution for either wx.GraphicsContext or Cairo on Linux
or I am f|<ed.....
Thanks
Prashant
#####################################
Operating System: Linux 2.6.31-14-generic i686
Python Version: 2.6.4rc2 (r264rc2:75497, Oct 20 2009, 02:55:11)
[GCC 4.4.1]
wxPython Version: 2.8.10.1 (gtk2-unicode)
wxPython Info: (__WXGTK__, wxGTK, unicode, gtk2, wx-assertions-off,
SWIG-1.3.29)
Python Encoding: Default=UTF-8 File=UTF-8
wxPython Encoding: utf-8
System Architecture: 32bit i686
Byte order: little
Frozen: False
#####################################