通行证: 用户 密码 域名空间  下载中心 社区论坛 信息公告 MY小屋
联系我们
设为首页
加入收藏

 

QQ,ASP,PHP,JSP,XML,SQL,.Net,编程 程序 网页图象 建站经验 私服
首页 | 新闻资讯 | 编程开发 | 网页设计 | 图形图象 | 网络媒体 | 网站模板 | 数 据 库 | 投稿
论坛 | 操作系统 | 系统优化 | 网络安全 | 黑客技术 | 硬件学堂 | 硬件报价 | 服 务 器 | 地图
专题 | 应用软件 | 聊天通讯 | Q Q 专栏 | 建站经验 | 在线工具 | 站长Club | 注 册 表 | 旧版
社会 | 游戏娱乐 | 设计欣赏 | 疑难解答 | 社区论坛 | 韩国素材 | 素材图库 | 广告服务 | 服务
当前位置:首页>>编程开发>>NET专区>>正文 新版上线![旧版]
注:打开慢时请稍等

用ASP.NET动态生成图像(转1)

http://www.iyit.net  日期:2006-6-12 1:59:41  来源:  点击:
参加讨论】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).



编辑:黑鹰 [发送给好友] [打印本页] [关闭窗口] [返回顶部]
上一篇:用ASP.NET动态生成图像(转2)
下一篇:在ASP.NET中操作文件的例子(VB)
转载请注明来源:www.iyit.net
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。

 相关文章
使用 ASP.NET 加密口令 [ASP.NET] Session 详解 从ASP迁移至ASP+
从ASP迁移至ASP+ ----------进入DataSe 从ASP迁移至ASP+ --将HTML表格转换为AS 从ASP迁移至ASP+ --急不可耐了?转换其
从ASP迁移至ASP+ --处理会话变量(Sess ASP+ 与 Java(一) ASP+ 与 Java(二)
asp+的论坛列表程序---代码部分 asp+的论坛列表程序---页面部分 用c#写的asp+域名查询程序
ASP+联结数据库 asp+文件上传增强实例 从ASP迁移至ASP+ --处理会话变量(Sess
ASP.NET学习手记:验证用户表单输入 asp+ 操作Cookie 方法大全 asp+ 现在已经被官方正式更名为 asp.ne
在ASP+ 中我们如何使用 Class 而不是组 asp+ 如何跨站抓取 页面 在 ASp+ 中的一些可能会用到的 小函数
列出asp+中所有request 的属性和数值 妙用asp+的global.asax 一个asp+ 版本的 Active Server Explor
最新更新 热点排行 推荐新闻
使用 ASP.NET 加密口令
[ASP.NET] Session 详解
从ASP迁移至ASP+
从ASP迁移至ASP+ ----------进入DataS
从ASP迁移至ASP+ --将HTML表格转换为A
CGI教程(2)
CGI教程(3)
CGI教程(5)
CGI教程(6)
CGI教程(7)
CGI教程(8)
CGI教程(5)
CGI教学:第一章 cgilib例
CGI教学:第二章 动态创建图像
CGI教学:第三章 计数器的编写方法
为ASP.NET 2.0菜单控件增加target属性
将Asp.net页面输出为HTML
在ASP.NET中防止注入攻击
ASP.NET 2.0 中的异步页功能应用
asp.net面试试题收集
QQ密码本地破解
msn8.0下载
PPLive最新内部版本揭密
珊蝴虫QQ探测隐身的招式用法
Visual Basic 概述
exeplorer.exe错误的问题的总结、解决
Authorware7.0基础与实例教程连载 第5
解决Windows中的explorer.exe出错
Windows常见文件修复技巧
Photoshop打造美女性感纹身(2)
在ASP.NET中防止注入攻击
用ASP.NET开发Web服务的五则技巧
ASP.NET 2.0 中的异步页功能应用
Windows下的虚拟主机设置全功略
危险无处不在 Html标签带来的安全隐患
网络游戏是06年互联网最具发展潜力业务
巧用ACDSee 8.0截取QQ表情每一帧
解除瑞星 诺顿遗留下的杀毒兼容问题
第一款开源AJAX安全扫描工具Sprajax
WinRAR人性化功能揭密
在ASP.NET中防止注入攻击
用ASP.NET开发Web服务的五则技巧
Linux操作系统12则经典应用技巧
 友情链接
设置首 页 - 版权声明 - 广告服务 - 关于我们 - 联系我们 - 友情连接
Copyrights © 2004-2006 iYiT.Net All Rights Reserved.
网站合作、广告联系QQ:147007642、466949678
易特网络技术 点击这里给我发消息