- 热门文章:
- · Uploading Images to a Database - Part I (转)
- · 在ASP.NET中操作文件的例子(VB)
- · 在ASP.NET中处理 datetime 的一些通用函数(vb)
- · 用ASP.NET动态生成图像(转2)
- · 在 ASP.NET 中使用多个 runat=server form
- · ASP.NET实现HTTP方式获取功能
- · ASP.NET中的XML表单控件
- · Assembly和Import区别
- · 两个获取http页面的c#函数
- · asp.net实现pop功能
- · 深入讲解 ASP+ 验证 (转自ms 一)
- · 深入讲解 ASP+ 验证 (转自ms 二)
用ASP.NET动态生成图像(转1)
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).
相关文章:
- · 使用 ASP+ 列表绑定控件 (转自ms 一)
- · 使用 ASP+ 列表绑定控件 (转自ms 二)
- · 查看服务器磁盘、文件的aspx
- · ASP+全新接触(1) {转}
- · 就是那个上传的。按实际上传时的文件名称保存(不在是test.jpg了)图片可以改大小,图上写字(可选字体...
- · ASP+全新接触(2) {转}
- · 用asp.net写的论坛程序
- · forum.aspx 论坛主页
- · reply.aspx 浏览贴子内容及回复
- · postmessage.aspx 上贴保存
- · .net的几个重要问题
- · 解决问题的方法:(JspFuns与开心的对话录)
- · 控件发布:带日期标注的日历控件。
- · 续上文:由于16K的限制,只能再接一节了。
- · 为什么我在中文win2000+sp1上装的vs.net不能创建各种web application,而英...
- · ASP.NET中密码保护,MD5和SHA1算法的使用
- · ASP。NET连SQL7接口源代码?
- · 我对.Net技术中asp.net应用的一点看法
- · 从一个舆论调查的制作谈面向对象的编程思路(一)
- · 从一个舆论调查的制作谈面向对象的编程思路(二)
- · 从一个舆论调查的制作谈面向对象的编程思路(三)
- · 从一个舆论调查的制作谈面向对象的编程思路(五)
- · 在asp.net中使用组件,也包括import和asemble的区别
- · 从一个舆论调查的制作谈面向对象的编程思路(四)
- · 分别用DataGrid、Repeater、DataList绑定XML数据的例子
- · 用DataList 控制元件开发的一个简单的留言本程序:
- · .Net边学边讲(二)
- · .Net边学边讲(一)
- · 里面是对一个body的属性进行server的一些设定,不过可以衍生到其他的一些htmlcontrol新手看看,或者有点...
- · 一个实现自定义event的文章。。。我还没有完全摸透。。不知道有没人有兴趣。。新手就不用看了,先学会走...
- · Server.Transfer,Response.Redirect 和 Page.Navigate 的区别
- · ASP.NET发送ICQ消息DIY
- · web页面用水晶报表的例子
- · Creating Custom Portal Modules
- · 几个.net的重要问题
- · SQL命令中DateTime格式参考
- · 关于webcontrol和pagelet的一点看法
- · 关于.net的几个重要问题的bigeagle版本
