选择显示字体大小

用asp.net动态生成图像(转1)

dynamic image generation with asp.net

scott guthrie
january 14, 2001

level: beginner/intermediate

one of the neat features that you can now leverage with .net is the ability to easily generate dynamic images from code, which you can then either save to disk or directly stream back to a browser client with asp.net.

the functionality to generate images with .net is encapsulated within the system.drawing namespace. it provides built-in support for generating images with a numer of file formats including: jpeg, gif, png, tiff, bmp, photocd, flashpix, wmf, emf and exif. note that there are no license issues to worry about with any of these file formats; microsoft implementation of each format is license free (including for gif images).

the general mechanism through which you generate these graphics images is by constructing a bitmap object which provides an in-memory representation of your image. you can then call its "save" method to either save it to disk, or stream it out to any .net output stream. because asp.net exposes a .net outputstream via the response.outputstream property. this means you can stream the image contents directly to the browser without ever having to save it to disk.

for example, to do this in vb you would write code like:


' create in-memory bitmap of jpeg
dim mychartengine as new chartengine
dim stockbitmap as bitmap = mychartengine.drawchart(600, 400, mychartdata)

' render bitmap stream back to browser
stockbitmap.save(response.outputstream, imageformat.jpeg)


if you are using an aspx page to do this, you will want to make sure you set the appropriate http contenttype header as well, so that the browser client doesn't interpret the page's content as html but rather as an image. you can do this either via setting the response.contenttype property through code, or via the new "contenttype" attribute that you can set on the top-level page directive:


<%@ page language="vb" contenttype="image/jpeg" %>


note that the output caching features of asp.net work for both textual content as well as for binary output. as such, if you are dynamically generating an image from a page, you can easily leverage the output cache directive to avoid having to regenerate the image on each request. note that image generation can be expensive, so this feature is highly recommended. for example, the below directive could be used to output cache the generated image for a 60 second interval:


<%@ page language="vb" contenttype="image/jpeg" %>
<%@ outputcache duration="60" %>


for a complete sample of how to use image generation, i've included a simple stock chart generation sample below. note that the stock prices aren't real, just wishful thinking on my part. the sample uses a custom "chartengine" class that helps encapsulate the logic required to build up a generic chart. you should be able to use this helper component to do any custom charting of your own, it is definitely not limited to just stock data.

feel free to use any of the code however you want and like with all my other samples, feel free to post elsewhere as well as to use for articles, other samples, etc).


instructions:


to run the sample, copy/paste/save the below files into an iis application vroot. then type the below statements into a command line:


mkdir bin
csc /t:library /out:bin\chartgen.dll chartengine.cs /r:system.web.dll /r:system.winforms.dll /r:system.drawing.dll /r:system.dll


once the chartengine helper utility is compiled, hit the stockpicker.aspx page to run the sample (note that this in turn sets up a <img> tag to point to the imagegenerator_vb.aspx page that does the actual image generation work).

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons