Chris
OK, here's an offer: if you (or anyone) writes up a paragraph or so
about the Image to stream stuff, and sends to to me, I'll post it in the
Wiki.
I think it needs a little more explaination than your post and a tested
example before I put it in. This way someone who understands this better
than I can write it, and I'll deal with the password hell and formatting
issues of the Wiki.
Not that I know the technical nitty gritty, but I have created and tested
the following piece of code that could be used as an example.
1. It creates an empty bitmap and uses another Wiki example to write some
text to it.
2. It saves the bitmap to a file (since there is no wrapper for saving to a
stream or string)
3. It reads the file into a string and then saves the string to a text field
in a database using MySql and SQLObject
4. It then reads the field value out of the database, into a stream, into a
wxBitmap
5. And for good measure out to another file so you can see it actually
works.
from wxPython.wx import *
from SQLObject import *
from cStringIO import StringIO
class Person(SQLObject):
_connection = MySQLConnection(host='localhost', db='jcpos',
user='test', passwd='')
first_name = StringCol(length=100,default="")
middle_initial = StringCol(length=1, default="")
last_name = StringCol(length=100, default="")
signature = StringCol(default="")
def _setupContext( memory, font=None, color=None ):
if font:
memory.SetFont( font )
else:
memory.SetFont(wxFont(20,wxSWISS,wxNORMAL,wxNORMAL))
if color:
memory.SetTextForeground( color )
def write( text, bitmap, pos=(0,0), font=None, color=None):
"""Simple write into a bitmap doesn't do any checking."""
memory = wxMemoryDC( )
_setupContext( memory, font, color )
memory.SelectObject( bitmap )
try:
memory.DrawText( text, pos[0],pos[1],)
finally:
memory.SelectObject( wxNullBitmap)
return bitmap
# 1. create a bitmap
myBitMap = write("Hello world", wxEmptyBitmap(200,50),color="WHITE")
# 2. Save it to a file in leiu of a stream
myBitMap.SaveFile("hello.bmp", wx.wxBITMAP_TYPE_BMP)
# 3. Open it and send it to a string
bitmapdata = open('hello.bmp', "rb").read()
# 4. And then to a database text field
newperson = Person.new(first_name="Mike")
newperson.signature = bitmapdata
# 6. Create a wxBitmap from the stream
listofpersons = Person.select()
for personobj in listofpersons:
if personobj.first_name == "Mike":
stream = StringIO(personobj.signature)
newbmp = wxBitmapFromImage( wxImageFromStream( stream ))
# 7. Put it somewhere where you can look at it
newbmp.SaveFile("newhello.bmp", wx.wxBITMAP_TYPE_BMP)