//第一种
using System;
using System.Collections.Generic;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.SessionState;
namespace sizhengwang.BLL
{
/// <summary>
/// GetValImage 的摘要说明
/// </summary>
public class GetValImage : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
using (Bitmap bitmap = new Bitmap(80, 20))//创建图片
{
using (Graphics gr = Graphics.FromImage(bitmap))//创建图片的画布
{
Random rand = new Random();
string code = yzm(4);
gr.Clear(Color.White);
gr.DrawString(" " + code, new Font("宋体", 14), Brushes.Blue, new PointF(0, 0));
bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg); //返回一个jpeg图片对象
HttpContext.Current.Session["code"] = code.ToLower(); //保存到session
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
public static string yzm(int len) //生成验证码
{
string str = "0,1,2,3,4,5,6,7,8,9,";
str += "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";
str += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] s = str.Split(new char[] { ',' });
string strnum = "";
int tag = -1;
Random rnd = new Random();
for (int i = 1; i <= len; i++)
{
if (tag == -1)
{
rnd = new Random(i * tag * unchecked((int)DateTime.Now.Ticks));