国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

用ASP.NET動態生成圖像(轉1)

2024-07-10 12:57:59
字體:
來源:轉載
供稿:網友
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).
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嵩明县| 玉环县| 乐至县| 得荣县| 岑溪市| 庄河市| 胶州市| 余姚市| 隆安县| 兴宁市| 平果县| 剑河县| 皋兰县| 富平县| 合川市| 兴宁市| 鄂托克前旗| 中阳县| 西乌| 康乐县| 永城市| 辽中县| 萨嘎县| 东港市| 平远县| 九龙县| 通州区| 三门县| 睢宁县| 辰溪县| 东兰县| 桂阳县| 当雄县| 靖州| 丰宁| 岳阳县| 大同县| 玛纳斯县| 凌云县| 葫芦岛市| 原平市|