首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 虚拟主机评测对比
·.NET中的动态生成图像组件-.NET教程,组件控件开发
作者:网友供稿 点击:37
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
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 <object id="objupload" runat="server" progid="persits.upload.1"></object>
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 <script language="c#" runat="server">
5 protected void page_load(object sender, eventargs e)
6 {
7 httpimage myhttpimage = new httpimage();
8 myhttpimage.outputimageviahttp("//images//aspalliance_fade_468x60.gif", this.response, this.server);
9 }
10 </script>

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 /// <summary>
8 /// summary description for httpimage.
9 /// </summary>
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.

文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Asp.Net下导出/导入规则的Excel(.xls)文件-ASP教程,ASP应用
·中国万网,万网
·中资源
·上海火速
·加拿大中文网
·华夏名网
·世纪东方
·商务中国
·中国E动网
·神话网络
最新文章
·photoshop抠图合成特效:飞出照片相框_photoshop教程
·将升级的windows vista驱动还原到原来的版本_windows vista
·photoshop漫画笔刷绘制非主流照片漫画_photoshop教程
·看域名注册信息判断网赚公司真假_网赚技巧
·找坐标:对网站进行坐标定位_站长心得
·如何增加网站pv_站长心得
·google性格一:对活跃度高的网页收录很快_google推广
·googleadsense容易被k的可能性列表_google推广
·价值数万的googlepr作弊方_google推广
·如何抢占人家的“首要意念”_google推广
相关主题
西部数码虚拟主机

友情链接
CNNIC 西部数码
万网 自助建站
虚拟主机 asp空间
域名注册 域名
域名申请 主页空间
论坛空间 网站空间
国际域名 虚拟空间
空间租用 DDOS防火墙
成都主机托管 四川主机托管
主机租用 服务器租用
网站目录 自助建站
虚拟主机 网址大全
软件下载
自助链接
虚拟主机资讯 特价虚拟主机
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
发表评论 打印  刷新     关闭