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

作者:袖梨 2022-07-02
Y'know, there's this really cool library in .NET for dynamically creating images on the fly. However, this article has nothing to do with that, so if that's what you're 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 server's 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, let's 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, let's 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" %>

相关文章

精彩推荐