ASP.NET 2.0中文验证码的实现

在判断的时候只要把取得的文本框的值与“Session["valicode"] ”进行判断是否一致就行了。

  //建立位图对象

  Bitmap NewbitMap = new Bitmap(90, 22, PixelFormat.Format32bppArgb);

  //根据上面创建的位置对象创建绘图面

  Graphics g = Graphics.FromImage(NewbitMap);

  //以指定的颜色填充矩形区

  g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, 90, 22));

  //创建字体对象

  Font newfont = new Font("幼圆", 14);

  //创建RectangleF结构指定一个区域

  RectangleF rectangle = new RectangleF(0, 0, 90, 22);

  //创建随机数

  Random Newrd = new Random();

  string[] abcd="我,是,没,高,天,地,聊,材,盆,浊,小,涯,尖,欠,猪,左,腿,刀,吃,渴,棍,皮,影,歇,草,营,救,税,说,坏,通,病,二,世,期,春,季,弄,刑,事,警,强,窝,菜,干,什,前,都,哭,拉,面,鱼,文,鬼,或,热,狗,蛋,毛,笔,网,件,构,试,社,帮,耐,烧,粘,苹,鞋,板,裳,花,海,题,a,e,f,r,9,0,k,2,4,7,1,3,q,w,y,u,v,x,p,s,a,d,8,5,t".Split(',');

         int RamStr=Newrd.Next(1,100);
  int Ramstr1 = Newrd.Next(1, 100);

  int Ramstr2 = Newrd.Next(1, 100);

  int Ramstr3 = Newrd.Next(1, 100);

  string ValiNum = abcd[RamStr] + abcd[Ramstr1] + abcd[Ramstr2] + abcd[Ramstr3].ToString();

  Session["valicode"] = ValiNum.ToString();

  //使用指定的颜色填充上面RectangleF结构指定的矩形区域

  g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);

  //绘制随机线条

  for(int ii =0;ii<10;ii++)

  {

  int x1=Newrd.Next(NewbitMap.Height);

  int y1=Newrd.Next(NewbitMap.Width);

  int x2 = Newrd.Next(NewbitMap.Height);

  int y2 = Newrd.Next(NewbitMap.Width);

  g.DrawLine(new Pen(Color.Azure), x1, y2, y1, x1);

  }

  //在上面填充的矩形区域中填充上面生成的随机数

  g.DrawString(ValiNum, newfont, new SolidBrush(Color.Blue), rectangle);

  for (int i = 0; i < 50; i++)

  {

  int x = Newrd.Next(NewbitMap.Width);

  int y = Newrd.Next(NewbitMap.Height);

  NewbitMap.SetPixel(x, y, Color.FromArgb(Newrd.Next()));

  }

  MemoryStream ms = new MemoryStream();

  NewbitMap.Save(ms, ImageFormat.Gif);

  Response.ClearContent();

  Response.ContentType = "image/Gif";

  Response.BinaryWrite(ms.ToArray());

posted @ 2009-03-22 10:21  网者风  阅读(173)  评论(0编辑  收藏  举报