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

ok__我们太年轻

ok__我们太年轻

2016-01-29 12:06

.NET中的动态生成图像组件,.NET中的动态生成图像组件
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://m.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://m.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

展开更多 50%)
分享

猜你喜欢

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

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

在.NET中开发组件

电脑网络
在.NET中开发组件

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

用Visual C#动态生成组件

编程语言 网络编程
用Visual C#动态生成组件

不能ASP图像组件来生成图像的ASP计数器程序(三)

ASP
不能ASP图像组件来生成图像的ASP计数器程序(三)

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

不能ASP图像组件来生成图像的ASP计数器程序(二)

ASP
不能ASP图像组件来生成图像的ASP计数器程序(二)

BCB中实现动态创建组件

编程语言 网络编程
BCB中实现动态创建组件

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

树控件的应用 -- 求子树节点的集

树控件的应用 -- 求子树节点的集

使用JavaBean创建您的网上日历本(2)

使用JavaBean创建您的网上日历本(2)
下拉加载更多内容 ↓