新建一个一般处理程序 Handler.ashx
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/jpeg";
using (System.Drawing.Bitmap map = new System.Drawing.Bitmap(100, 30))
{
using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(map))
{
string strArray = "1234567890ABCDEFGHIJKLMNUVWXYZ";
Random rand = new Random();
string str = "";
for (int i = 0; i < 4; i++)
{
int j = rand.Next(0, 35);
str += strArray[j];
}
HttpCookie cookie = new HttpCookie("ckkd");
cookie.Value = str;
cookie.Expires = DateTime.MaxValue;
context.Response.Cookies.Add(cookie);
gr.DrawString(str, new System.Drawing.Font("Courier New", 20, System.Drawing.FontStyle.Italic), System.Drawing.Brushes.Blue, new System.Drawing.PointF(2, 2));
map.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
使用方法:
<asp:TextBox ID="txtCoder" runat="server"></asp:TextBox><img src="Handler.ashx" />