当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET 2.0中文验证码的实现

ASP.NET
ASP.NET中Session丢失原因与解决方案小结
.net开发中的一些注意事项及小技巧
学习Asp.Net经常会用到的函数集
在.net App中集成COM组件的一些简单技巧
彻底放弃IIS让Apache也支持ASP.NET
[JS.IntelliSense]VS2007(Orcas) So Cool
Asp.net 2.0 ViewState原理
asp.net ajax 使用updatepanel进行更新后的提示
动态代理DynamicProxy 介绍
您可能不知道的.Net2.0小技巧
Asp.Net2.0技巧(续)
“黑盒测试管理”以外的编程经验片断
实例开发:ASP.NET创建网络相册
封装stream,在读写stream时提供事件通知
GIS开发随笔--GIS技术的一点理解和MapNet控件试验
利用隐藏帧打印url的方法比较
无刷新仿google波形扭曲彩色Asp.net验证码
存储过程编写经验和优化措施
编程技巧:.Net Framework
编程技巧OOPs:复制构造函数

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 244 ::
收藏到网摘: n/a

       在判断的时候只要把取得的文本框的值与“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());