Now you know what HttpHandlers are and how to setup them, we
will write our own httpHandler to read the images from a database. Remember,
this article explains creating httpHandler using the generic handler (.ashx).
Since the .ashx file extension is already mapped in the IIS to the ASP.NET
ISAPI filter, you do not have to configure your web.config file.
Create a new website in Visual Studio 2005. Right-click on
the project in the solution explorer and select Add New Item.
From the available templates, add the new file of type Generic
Handler and name it getImage.ashx
Figure 1 - Create new file of type Generic Handler

Visual Studio 2005 opens a new file with the following
content.
Listing 3 - getImage.ashx
<%@ WebHandler Language="C#" Class="getImage" %>
using System;
using System.Web;
public class getImage : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
public bool IsReusable {
get {
return false;
}
}
}
By default, the IHttpHandler is implemented. We will modify
the code in ProcessRequest to read images from database. We are going to use
the TableAdapters to read the data from the database.
Listing 4 - Complete getImage.ashx code
<%@ WebHandler Language="C#" Class="getImage"%>
<span style='background:yellow'> </span>
using System;
using System.Web;
using dsImagesTableAdapters;
public class getImage : IHttpHandler {
public void ProcessRequest (HttpContext context) {
int _imageId;
if (context.Request.QueryString["id"] != null)
_imageId = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
SampleImagesTableAdapter _adapter = new SampleImagesTableAdapter();
dsImages.SampleImagesDataTable _imgTable =
_adapter.GetImageDataById(_imageId);
if (_imgTable.Rows.Count > 0)
{
if (_imgTable[0]["Image"] != DBNull.Value)
{
context.Response.ContentType =
_imgTable[0]["ImageType"].ToString();
context.Response.BinaryWrite((byte[])_imgTable[0]["Image"]);
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
The above code is straightforward and self explanatory. We
pass the imageId parameter as a querystring parameter to the getImage.ashx
file. If the querystring parameter is null, we throw an ArgumentException as
our database method expects an imageId parameter. After that, we are creating a
new instance of the tableadapters and call the GetImageDataById method which
takes the imageId as a parameter. If the record is found, we write the image
content to the response stream using the BinaryWrite method.
The dsImagesTableAdapters comes from the dsImages dataset
which is included in the source code.
The first line of the code <%@ WebHandler
Language="C#" Class="getImage" %> is a directive syntax
which defines the attributes and compilation options for the HTTP Handlers
(.ashx) files. This directive is valid only in files used as HTTPHandlers and,
by default, ASP.NET treats them as handlers.
For more information check @WebHandler.
Now in your ASP.NET pages, to display the image, we use <img
alt="httpHandler" src="getImage.ashx?id=1" > and
the image is served through the handler.
Listing 5 - Default.aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<span style='background:yellow'> </span>
<!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>
<img alt="httpHandler" src="getImage.ashx?id=1">
</div>
</form>
</body>
</html>
Now run your application and the page (Default.aspx) displays the image served
through HttpHandler.