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

2016-01-29 12:06 50 1 收藏

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

【 tulaoshi.com - vb 】

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:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/vb/)

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/vb/)

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 jus

来源:https://www.tulaoshi.com/n/20160129/1484629.html

延伸阅读
标签: ASP
  以前在这里也曾经有一些文章谈到了在ASP中生成动态图表的问题,但那些文章中所谈到的一些方法都有某些方面的限制,如需要在客户端安装office2000或安装OWC,有些则可能在Intranet中使用时没什么问题,但在通过Internet访问的时候,就可能出现无法正常工作的现象(这主要和SQL Server的安全设置有关)。那么,我们如何来避免这些问题呢?...
标签: flash教程
编程技术的发展真是日新月异。通过ASP等程序动态生成HTML网页似乎已不能算是最新科技。Macromedia公司的Flash技术自推出以来,已有2亿4千8百万用户,制作flash网站已是流行趋势。就像HTML页面可以用ASP动态生成一样,Flash 动画也可以使用支持COM Automation的语言(如Borland Delphi,ASP, Visual Basic 等)动态生成。该文详细介绍了如何应...
    Macromadia公司出品的多媒体著作软件是一个功能全面的多媒体制作软件。他可以方便地集成文字、声音、图形、动画、视频等媒体信息;他提供按钮、热区、菜单等常见的11种人机交互方式;他采用流程图标的程序设计方式,无需编程即可实现一般多媒体软件设计;他提供exe文件,AAM文件、HTM文件的一次发布完成等。所有这些优势以...
标签: ASP
  前几天看了netnice和qzsage君的贴子,颇有启发,于是着手编了一个图像计数器,这个图像计数器没有用图像组件,也不是以前那样用几张图片拼起来,而是用了*.xbm的图片格式。这种格式可能只能在Windows+IE下才能显示。 具体的思路是这样: 图片用点阵的形式表示,比如2: 00111100 0011为3 1100为C 即0x3c 01100110 0110为6 0110为6 0x66 0...
标签: ASP
  这里所谓的复杂表单,是指表单中包含多种不同的输入类型,比如下拉列表框、单行文本、多行文本、数值等。在经常需要更换这类表单的场合,需要有一个表单的动态生成程序。本文介绍的正是这样一个系统,它以数据库保存表单定义数据,利用ASP脚本动态生成表单HTML代码以及验证表单输入的脚本。 一、定义数据库表结构 在Web上经常可...

经验教程

403

收藏

33
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部