Playing with Images in ASP.NET 3.5 AJAX Applications - A Few words about the HTTP Handler
(Page 3 of 5 )
Since the HTTP Handler is almost necessary in all the following sample applications, let's say a few words about it.
HTTP handlers and modules are basic components of Microsoft's ASP.NET architecture. Any request for ASP.NET-managed resources will be resolved using an HTTP handler, and then transmitted through an HTTP module. For example, ASP.NET will map, per incoming HTTP request, to a special HTTP handler.
ASP.NET has built into it several ready-made HTTP handlers. There is one special HTTP handler to serve the ASP.NET pages, one to serve the .NET Web services and one to resolve the .NET remote requests for the IIS managed remote objects. There are also many other auxiliary handlers, such as trace.axd to trace the page life cycle, webresource.axd to inject assembly resources and scripts into the images, etc.
In other conditions, you may ask ASP.NET to process some request in the non-standard means when you can write your custom HTTP handlers. In fact, you can rely on the custom HTTP handlers to do any jobs. For example, via an elaborate custom HTTP handler, you can let the users make nearly any invocation through the web, such as to write a click counter, to deal with any image related tasks, etc.
Now, we've talked enough about theory. Let us roll up our sleeves to construct some interesting sample applications.
Using a .aspx Page to Render Images
From the point of view of a browser, a web page is merely a series of characters waiting in line to be processed. When the browser finds an <img> mark it comes to realize that it needs to open another related download channel and sends out a request for the specified URL. To the browser, it does not matter much what kinds of URL and protocol they are; what really concerns it is that the expected output will match a given format- the MIME type.
Let's examine a trivial example to demonstrate how to specify the proper MIME type using the Content-Type header to render an image onto the page indirectly.
First, let us look at the main page ( AspxMode.aspx ) to undertake the task of rendering the image. The following gives the key piece of code:
<div>
<asp:Image ID="Image1" runat="server" ImageUrl="pic.aspx" />
</div>
Here, when the browser finds the <img> mark (which is translated from the ASP.NET server control <Image>), it will open another download channel as well as send out the related request. Then, it expects the MIME type to be matched. If so, an image will be output; otherwise, some exception will possibly be thrown.
Now, let's see what hides within the other page pic.aspx. The HTML elements' definitions are listed as below:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="pic.aspx.cs" Inherits="pic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Anything peculiar? Nothing, but Visual Studio 2008 automatically generated stuff! In fact, what really plays the crucial role lies in the code-behind file-pic.aspx.cs:
public partial class pic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string s = Server.MapPath(@"~/imgs/lord.bmp");
Response.ContentType = "image/bmp";
Response.WriteFile(s);
}
}
Here, we first get the path to the image file lord.bmp, and after we set the ContentType attribute to "image/bmp," we simply write the image data bits to the response (output) stream. Note, in later samples, we will do something with the image bytes before writing them to the stream. Figure 1 shows the running-time snapshot of the AspxMode.aspx page.
Figure 1-the running-time snapshot for AspxMode.aspx

In fact, if you try to start up the pic.aspx page independently, you will still catch sight of the same picture. In the latter case, the web server produces the correct image bytes, sets the proper content type, and finally writes all data into the output stream. While in the former case, the output stream in relation to the pic.aspx page is further redirected to the src attribute of the <img> element in the first AspxMode.aspx page, and as a result, the AspxMode.aspx page takes the responsibilities of rendering the image onto the screen.
Author's Note: an aspx page is in fact a complex HTTP handler. Although this sample has achieved the aim of rendering an image via another aspx page acting as an agency, this is not the recommended way to output an image dynamically. The mostly recommended way to accomplish such tasks is to define a custom HTTP handler. In later samples, we will delve into it.
Next, let us see another way to load images.
Next: Loading Images from an Assembly >>
More ASP.NET Articles
More By Xianzhong Zhu