<%@ WebHandler Language="C#" Class="MyHandler" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
public class MyHandler : IHttpHandler, IRequiresSessionState {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/gif";
Bitmap basemap = new Bitmap(150, 45);
Graphics graph = Graphics.FromImage(basemap);
graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 150, 45);
Font font = new Font(FontFamily.GenericSerif, 38, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789abcdefghijklmnpqrstuvwxyz";
string letter = string.Empty;
StringBuilder sb = new StringBuilder();
Color c1 = Color.FromArgb(17, 129, 190);
Color c2 = Color.FromArgb(115, 17, 190);
Color c3 = Color.FromArgb(3, 106, 76);
Color c4 = Color.FromArgb(98, 80, 15);
Color c5 = Color.FromArgb(94, 8, 102);
Color[] color = new Color[] { c1, c2, c3, c4, c5 };
for (int x = 0; x < 5; x++)
{
letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
sb.Append(letter);
graph.DrawString(letter, font, new SolidBrush(color[r.Next(0, 4)]), x * 26, r.Next(0, 3));
}
for (int x = 0; x < 3; x++)
{
Pen linePen = new Pen(new SolidBrush(color[r.Next(0, 4)]), 2);
graph.DrawLine(linePen, new Point(r.Next(0, 149), r.Next(0, 44)), new Point(r.Next(0, 149), r.Next(0, 44)));
}
basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
graph.Dispose();
basemap.Dispose();
context.Session["CheckCode"] = sb.ToString();
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}