转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
ASP.NET动态创建图象         ★★★★

ASP.NET动态创建图象

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2398 更新时间:2009/4/23 10:48:39
ASP.NET动态创建图象 
http://www.tongyi.net  出处:chinaasp    点击:435  

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). 


StockPicker.aspx:
<script language="VB" runat=server>
   Sub ChartBtn_Click(Sender as Object, E as EventArgs) 
      chart.ImageUrl = "ImageGenerator_Vb.aspx?"
      chart.Visible = true
      For i=0 to Stocks.Items.Count-1
         If (Stocks.Items(i).Selected = true) Then
             chart.ImageUrl = chart.ImageUrl & "symbols=" & Stocks.Items(i).Value & "&"
         End If
      Next
   End Sub
</script>
<html>
   <body>
      <form runat=server>
         <h1>Scott''''s Stock Picker</h1>
         <asp:checkboxlist id="Stocks" runat=server>
             <asp:listitem>MSFT</asp:listitem>
             <asp:listitem>SUN</asp:listitem>
         </asp:checkboxlist>
         <asp:button text="Chart Your Selected Stocks" OnClick="ChartBtn_Click" runat=server/>
         <hr>
         <asp:Image id="chart" ImageUrl="" Visible=false runat=server/>
      </form>
   </body>
</html>

ImageGenerator_VB.aspx:
<%@ Page Language="VB" ContentType="image/jpeg" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="ChartGenerator" %>
<%@ OutputCache Duration="10" %>
<script language="VB" runat=server>
    Function GetStockDetails(Symbol as String) as ChartLine 
        Dim myChartLine as new ChartLine
   &nbs

[1] [2] [3] [4] [5]  下一页


[C语言系列]NET 中C#的switch语句的语法  [系统软件]托拽Explore中的文件到VB.net的窗口
[系统软件]Boost库在XP+Visual C++.net中的安装  [常用软件]新配色面板:Paint.Net3.0RC1官方下载
[常用软件]用内建的“Net Meeting”聊天  [VB.NET程序]Henry的VB.NET之旅(三)—共享成员
[VB.NET程序]Henry的VB.NET之旅(二)—构造与析构  [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体
[VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET)  [VB.NET程序]Henry手记-VB.NET中动态加载Treeview节点(二)
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台