Sunday, October 08, 2006

Those of you who actually visit my blog rather than just consuming my RSS feed may have noticed my new header images.  I couldn't decide which one to use, so I threw together a random image HttpHandler to serve up a random one for each request.  There are several solutions for this available, but I thought my simplistic approach might be useful to someone else.

I just named the images header-XXXX.jpg, where XXXX is a 4 digit number and threw them into a directory, and then created an .ashx file with the following content:

<%@ WebHandler Class="HeaderRotator" Language="C#" %>

using System;
using System.IO;

public class HeaderRotator : System.Web.IHttpHandler {

   static int _HighestHeader = 12;
   static Random _Rand = new Random();

   #region IHttpHandler Members

   public bool IsReusable {
      get { return true; }
   }

   public void ProcessRequest(System.Web.HttpContext context) {
      int next = _Rand.Next(_HighestHeader + 1);
     context.Response.WriteFile(context.Server.MapPath(string.Format("~/headers/header-{0:0000}.jpg", next)));
   }

   #endregion
}

And that's it. Then you just reference the .ashx file in the img tag and you get a random image every time.  The _HighestHeader number needs to reflect the highest-numbered header image.  There's lots of different ways to do that, but I felt that simplicity was important here.

I had a lot of fun creating the images.  Some of them were carefully crafted, while others were more or less "happy accidents". I'm open to more suggestions for a good "tag-line".

[UPDATE] A few of you wanted a way to see them all without wasting time fighting against statistics.  So, here's a link that will show them all.

posted on Sunday, October 08, 2006 12:23:34 PM (Pacific Standard Time, UTC-08:00)  #    Comments [8]