我曾经使用CPython + PIL + bottle在我的网站上提供图像文件。我使用下面的代码直接提供来自PIL内存的图像数据。来自MemoryStream的图像文件(IronPython网络服务)
# works well in CPython
@route('/test')
def index():
response.set_content_type('image/png')
img = DrawSomePILImage()
output = StringIO.StringIO()
img.save(output,'PNG')
contents = output.getvalue()
output.close()
return contents
现在,我需要使用IronPython而不是CPython来提供图像文件。我正在尝试提供Microsoft图表控件(System.Windows.Forms.DataVisualization.Charting)的图像。
# image is broken in IronPython
@route('/test')
def index():
response.set_content_type('image/png')
cht = GetChartControl()
stream = MemoryStream()
cht.SaveImage(stream, ChartImageFormat.Png)
contents = System.Text.ASCIIEncoding.ASCII.GetString(stream.ToArray())
stream.Close()
return contents
但这不起作用。 Web浏览器上的投放图像文件已损坏。你们能帮助我如何使这个蟒蛇代码工作?
感谢您的回答,但这似乎不起作用。我之前尝试过,它使内部(网络)服务器错误。我想我需要将.net类型(字节[])转换为python类型(str或其他) – 2010-12-15 17:18:13