public void ProcessRequest (HttpContext context) { context.Response.ContentType = "image/jpeg"; //获得验证码符号 string code = GetCode(); Image img = GetImage(code); //保存生成的验证码图片 img.Save(context.Response.OutputStream, ImageFormat.Jpeg); } /// <summary> /// 生成验证码图片 /// <summary> private Image GetImage(string code) { Bitmap bitmap = new Bitmap(85, 40); //画板 Graphics g = Graphics.FromImage(bitmap); //在画板上输出符号 g.DrawString(code, new Font("楷体", 24), Brushes.Green, 5, 5); return bitmap; } /// <summary> /// 获取验证码 /// </summary> /// <returns></returns> private string GetCode() { string str = "0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ"; string code = ""; Random r = new Random(); for (int i = 0; i < 4; i++) { //存储验证码符号 code += str[r.Next(0, str.Length)]; } return code; }
下面是我用一般处理程序生成的10张验证码图片(给大家看看效果)