首 页 ┆ 源码下载 ┆ IT学院 ┆ 字体下载 ┆ 模板下载 ┆ 源码发布 ┆ 广告合作 ┆ 网站地图 ┆ 虚拟主机 ┆ 中文域名
► 设为首页
► 加入收藏
► 联系我们
源码下载 >> ASP源码 | PHP源码 | ASP.net源码 | JSP源码 | CGI源码 | VC/C++源码 | VB源码 | Delphi源码 | Flash源码
文章学院 >> 网络编程 | 网页设计 | 图形图象 | 数据库 | 服务器 | 网络媒体 | 网络安全 | 操作系统 | 办公软件 | 软件开发 | 黑客知识
字体下载 >> 精制字体 | 非英字体 | 艺术字体 | 著名字体 | 哥特式 | 简单字体 | 手写体 | 节假日 | 图案字体 | 精度像素 | 中文字体
模板下载 >> 企业门户 | 数码网络 | 休闲娱乐 | 影视音乐 | 旅游名胜 | 文化艺术 | 电子商务 | 个性展示 | 登陆导航 | Flash模板
►►您当前的位置:源码园 → IT学院 → 软件开发 → VB编程 → 文章内容

.NET中的动态生成图像组件

作者:佚名  来源:网上收集  发布时间:2005-12-8 1:31:37
By Steven Smith from aspalliance.com


Yknow, theres this really cool library in .NET for dynamically creating images on the fly. However, this article has nothing to do with that, so if thats what youre looking for, stop now. What this article is about is very simple -- how to use a single line of code (via a component call) to output the contents of an image residing on the servers hard drive. Why not just use an IMG tag? Well, we want this page to used as the SRC of an IMG tag, so it has to actually have a content-type of "image/gif" and use BinaryWrite to display the image. To do this in Classic ASP requires a custom component (or a third party component such as Persits ASPUpload, which has a SendBinary method that does this). In fact, lets take a look at how this works with a quick sample before we do it in .NET. Below is the complete code required to display a static image using ASPUpload. You can see this file in action by clicking here.

displayimage.asp:
1 <% OPTION EXPLICIT %>
2
3 <%
4 objUpload.SendBinary Server.MapPath("/images/aspalliance_fade_468x60.gif"), True
5 %>

Now, lets do this in .NET. We can use the System.Drawing library to open an image and display it, using the following code:

displayimage.aspx:
1 <%@ Page language="c#" AutoEventWireup="false" Trace="false" Debug="false" %>
2 <% @Import Namespace="System.Drawing" %>
3 <% @Import Namespace="System.IO" %>
4 <% @Import Namespace="System.Drawing.Imaging" %>
5 <%@ OutputCache Duration="100" VaryByParam="none" %>
6 <%
7 string path;
8 path = Server.MapPath("///images//aspalliance_fade_468x60.gif");
9 System.Drawing.Image myImage = System.Drawing.Image.FromFile(path);
10
11 MemoryStream tempStream = new MemoryStream();
12 myImage.Save(tempStream,ImageFormat.Gif);
13
14 Response.ClearContent();
15 Response.ContentType = "image/gif";
16 Response.BinaryWrite(tempStream.ToArray());
17 Response.End();
18 %>

Here is the result of using this code as the SRC of an IMG tag:

Old:(Notice that there is a problem here -- this is an animated GIF, but this version doesnt show any more than the first frame of the graphic. Not good. So well scrap that version, hope that perhaps there is an animated GIF type supported in the future, and move on.)

As you can see, the above example renders the animated GIF image just fine now, thanks to updates to the .NET Framework in Beta2. The HTTPImage Class, below, is no longer necessary, but is left for posterity. -- Steve The HTTPImage Class -- NO LONGER FUNCTIONAL (or necessary) UNDER BETA 2

You can tell were getting to the real thing now, because I actually bothered to put the working code into a class file. In this case, I called it HTTPImage, and its written in C# and has two simple methods. Actually, one overloaded method, outputImageViaHTTP, which takes either a virtual file path or a static file path. Before we get into the class, lets see it in action by looking at a simple ASPX page that uses it. Click here for the example, and below is the source code. Note that the ASP.NET page is passing its own instances of Response and Server to the component (line 8). Well see how the component uses these below.

displayimage2.aspx:
1 <%@ Page Language="c#" ContentType="image/gif" %>
2 <%@ Import Namespace="stevenator.components" %>
3 <%@ OutputCache Duration="100" VaryByParam="none" %>
4

Pretty cool, eh? The image is actually animated, as its supposed to be. Now, the reason this is even remotely useful is so that if you want to show a random image, like for an ad banner, you can use this method to output the image from your file system. For example, this image tag has as its source the same file that we just looked at (it is animated to fade in about once per minute):

Ok, now were finally ready to actually look at the class. First, here is the source code:

HTTPImage.cs:
1 namespace stevenator.components
2 {
3 using System;
4 using System.IO;
5 using System.Web;
6
7 ///


8 /// Summary description for HTTPImage.
9 ///

10 public class HTTPImage
11 {
12 public HTTPImage()
13 {
14 //
15 // TODO: Add Constructor Logic here
16 //
17 }
18
19 public void outputImageViaHTTP(string absolutePath, HttpResponse Response)
20 {
21 FileStream oFileStream;
22 long lFileSize, lStartpos = 0;
23
24 oFileStream = new FileStream(absolutePath, FileMode.Open);
25 lFileSize = oFileStream.Length;
26
27 byte[] bBuffer = new byte[(int)lFileSize];
28 oFileStream.Read(bBuffer, 0, (int)lFileSize);
29 oFileStream.Close();
30
31 Response.ClearContent();
32 Response.ContentType = "image/gif";
33
34 Response.BinaryWrite(bBuffer);
35 oFileStream.Close();
36 Response.End();
37 }
38 public void outputImageViaHTTP(string virtualPath, HttpResponse Response, HttpServerUtility Server)
39 {
40 string path;
41
42 path = Server.MapPath(virtualPath);
43
44 outputImageViaHTTP(path, Response);
45 }
46 }
47 }

As you can see, this is a pretty simple class. No frills. Lets look at the second overloaded method first, since its simpler. On line 38 you can see the declaration. We need to pass in the a Response and a Server object from our ASP.NET page, which we saw on line 8 of displayimage2.aspx. We only need server for this second overloaded call, because were going to use Server.MapPath to determine the absolute path to the image and then simply call the other method. Which takes us to line 19, the other method call.

Were going to use the FileStream object to access the image, and we will need to know the files size so that we know when to stop reading and avoid an error. Lines 27 to 30 do all 90% of the work for this class, by loading the buffer with the contents of the file. Then all that remains is to set the content type to an image type (in this case, Ive hardcoded it to be "image/gif", but you could make that a parameter or base it on the file extension) and then use the BinaryWrite command to output the image. The BinaryWrite command is nothing new to Classic ASP developers -- in fact, the basic concepts of this whole article are all old news for Class ASP developers, but I hadnt done this in .NET before, so I thought Id share it with you. Hopefully itll help a few of you get going with .NET a little faster. If you just want to plug the component in and see it work, you can download it here: HTTPImage.dllX. The download didnt like the fact that it was a dll, so remove the X from the end of the filename after you download it. You can get the C# source here.

[] [返回上一页] [打 印]
  • 上一篇文章:ASP.NET验证控件祥解
  • 下一篇文章:全面剖析VB.NET(6)

  • 相关文章:
  • ASP.NET控件之ViewState
  • ASP.Net 2.0中的5个数据控件
  • Asp.net性能完全分析
  • .NET的自动下载代码
  • ASP.NET页面间的传值的几种方法
  • 在PHP中使用ASP.NET AJAX
  • ASP.NET获取IP与MAC地址的方法
  • ASP.NET生成静态页面实现方法
  • 九阳神功 ASP.NET技术的学习顺序
  • ASP.NET在IIS一些问题经验总结
  • asp.net数据校验部分的封装与应用
  • asp.net客户端回调功能的实现机制
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 源码发布
Copyright © 2003-2009 Ymyasp.Com. All Rights Reserved .
备案序号:粤ICP备07029071号