Creating a Generic Handler : HTTP Handlers « Development « ASP.NET Tutorial

When you create a Generic Handler, you create a file that ends with the extension .ashx. Whenever you request the .ashx file, the Generic Handler executes.

A Generic Handler is like an ASP.NET page that contains a single method that renders content to the browser. 
You can't add any controls declaratively to a Generic Handler. 
A Generic Handler doesn't support events such as the Page Load or Page PreRender events.

File: ImageTextHandler.ashx

<%@ WebHandler Language="C#" Class="ImageTextHandler" %>
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;

public class ImageTextHandler : IHttpHandler
    public void ProcessRequest(HttpContext context)
        string text = context.Request.QueryString["text"];
        string font = context.Request.QueryString["font"];
        string size = context.Request.QueryString["size"];

        Font fntText = new Font(font, float.Parse(size));

        Bitmap bmp = new Bitmap(10, 10);
        Graphics g = Graphics.FromImage(bmp);
        SizeF bmpSize = g.MeasureString(text, fntText);
        int width = (int)Math.Ceiling(bmpSize.Width);
        int height = (int)Math.Ceiling(bmpSize.Height);
        bmp = new Bitmap(bmp, width, height);

        g = Graphics.FromImage(bmp);
        g.DrawString(text, fntText, Brushes.Black, new PointF(0, 0));
        bmp.Save(context.Response.OutputStream, ImageFormat.Gif);

    public bool IsReusable
            return true;

You specify the image that you want to return from the handler by making a request that looks like this:


The IsReusable property indicates whether the same handler can be reused over multiple requests. 

The following page uses the ImageTextHandler.ashx file. 

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
<head id="Head1" runat="server">
    <title>Show ImageTextHandler</title>
    <form id="form1" runat="server">

    <img src="ImageTextHandler.ashx?text=Some Text&font=WebDings&size=42" />
    <br />
    <img src="ImageTextHandler.ashx?text=Some Text&font=Comic Sans MS&size=42" />
    <br />
    <img src="ImageTextHandler.ashx?text=Some Text&font=Courier New&size=42" />


9.24.HTTP Handlers
9.24.1.Creating HTTP Handlers
9.24.2.Creating a Generic Handler
9.24.3.HelloWorld HttpHandler (VB)
9.24.4.Implementing the IHttpHandler Interface
9.24.5.RSS Handler
9.24.6.Creating a Custom HTTP Handler
9.24.7.Log user in HttpModule
9.24.8.Source viewer Http Handler