I have the following event handler code:
def on_export(self, event):
"""
Exports the current tab as an image.
"""
wc = "PNG (*.png)|*.png|JPEG (*.jpg, *.jpeg)|*.jpeg;*.jpg|GIF (*.gif)|*.gif|BMP (*.bmp)|*.bmp|TIFF (*.tiff)|*.tiff"
dlg = wx.FileDialog(self, "Export data to...", os.getcwd(),
style=wx.SAVE | wx.OVERWRITE_PROMPT, wildcard=wc)
if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPath()
_name = os.path.splitext(filename)[1].replace(".", "")
types = {0: "png", 1: "jpg", 2: "gif", 3: "bmp", 4: "tiff"}
if not os.path.splitext(filename)[1]:
_name = types[dlg.GetFilterIndex()]
filename += "." + _name
if not _name in self.util.types[2:]:
wx.MessageBox("Invalid filetype to export as.", "Invalid type")
else:
self.board.export(filename)
dlg.Destroy()
then, in board.export (pretty much taken from the wiki cookbook):
def export(self, filename):
"""
Exports the current view as a file. Select the appropriate wx constant
depending on the filetype.
"""
_name = os.path.splitext(filename)[1].replace(".", "").lower()
types = {"png": wx.BITMAP_TYPE_PNG, "jpg": wx.BITMAP_TYPE_JPEG, "jpeg":
wx.BITMAP_TYPE_JPEG, "bmp": wx.BITMAP_TYPE_BMP, "gif":
wx.BITMAP_TYPE_GIF, "tiff": wx.BITMAP_TYPE_TIF, "pcx":
wx.BITMAP_TYPE_PCX }
const = types[_name]
context = wx.BufferedDC(None, self.buffer, wx.BUFFER_VIRTUAL_AREA)
memory = wx.MemoryDC()
x, y = self.GetClientSizeTuple()
bitmap = wx.EmptyBitmap(x, y, -1)
memory.SelectObject(bitmap)
memory.Blit(0, 0, x, y, context, 0, 0)
memory.SelectObject(wx.NullBitmap)
bitmap.SaveFile(filename, const)
It works great for every file type except for gifs - it creates a 0-byte image. All the other file formats work correctly, so I'm not sure what's wrong. I'm using 2.8.9.1, python 2.5.2, Ubuntu 8.10 64bit, kernel 2.6.27-9-generic #1 SMP