欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

12306奇葩验证码引发思考之C#实现验证码程序

程序员文章站 2024-01-02 17:23:40
近日铁路订票网“12306”又出现多道另类考题,竟要订票者在8个图案中“点击图中所有美男子”、“请点击下图中所有的非智能眼镜”、“请点击下图中所有的博斯普鲁斯海峡”,网友吐...

近日铁路订票网“12306”又出现多道另类考题,竟要订票者在8个图案中“点击图中所有美男子”、“请点击下图中所有的非智能眼镜”、“请点击下图中所有的博斯普鲁斯海峡”,网友吐槽:比高考题还难,到底是什么样子的,先跟大家分享一下几个例子:

12306奇葩验证码引发思考之C#实现验证码程序

哈哈,是有点奇葩的验证码,怪不得有人会说“妈妈我已经找不到回家”,这让分秒必争的春运网上抢票者瞬间傻眼,九成网友已经被打败……

正巧小编最近也在研究验证码,参考了许多网上案例,整理了一篇文章特分享给大家。

验证码的一般编写思路为:
       1.定义验证码字符长度;
       2.根据长度随机生成验证码字符串;
       3.将验证码字符串转换成图片形式,并在图片中生成随机噪声点和声线(对验证码进行模糊识别处理);
       4.显示结果。

 ///
 /// 生成随机验证码
 ///
 /// 验证码长度
 ///
 public string createidentifyingcode(int codelen)
 {
 if (codelen < 1)
  return string.empty;
 int num;
 string checkcode = string.empty;
 random random = new random();
 for (int index = 0; index < codelen; index++)
 {
  num = random.next();
  if (num % 2 == 0)
  checkcode += (char)('0' + (char)(num % 10));
  else
  checkcode += (char)('a' + (char)(num % 26));
 }
 return checkcode;
 }
-------------------------------------------------------------------------------------------------
 ///
 /// 生成验证码图片
 ///
 ///
 /// 
 private void createcheckcodeimage(string checkcode)
 {
  if (checkcode == null || checkcode.trim() == string.empty)
  return;
  //创建图片大小
  system.drawing.bitmap image = new  system.drawing.bitmap((int)math.ceiling(checkcode.length*12.5),22);
  //创建画板
  graphics graphic = graphics.fromimage(image);
 
  try
  {
  random random = new random();
  graphic.clear(color.white);
  int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  //画图片背景噪声线
  for (int index = 0; index < 25; index++)
  {
   x1 = random.next(image.width);
   y1 = random.next(image.height);
   x2 = random.next(image.width);
   y2 = random.next(image.height);
   graphic.drawline(new pen(color.silver),x1,y1,x2,y2);
  }
  font font = new system.drawing.font("helvetica", 12, (fontstyle.bold |fontstyle.italic));
  lineargradientbrush brush = new lineargradientbrush(new rectangle(0, 0, image.width, image.height),color.blue,color.darkblue,1.2f,true);
  graphic.drawstring(checkcode,font,brush,2,2);
 
  int x = 0;
  int y = 0;
  // 画图片的前景噪声点
  for (int index = 0; index < 100; index++)
  {
   x = random.next(image.width);
   y = random.next(image.height);
   image.setpixel(x,y,color.fromargb(random.next()));
  }
  //画图片的边框线
  graphic.drawrectangle(new pen(color.silver), 0, 0, image.width - 1, image.height - 1);
  //网页响应
  system.io.memorystream ms = new system.io.memorystream();
  image.save(ms,system.drawing.imaging.imageformat.gif);
  response.clearcontent();
  response.contenttype = "image/gif";
  response.binarywrite(ms.toarray());
  }
  finally
  {
  graphic.dispose();
  image.dispose();
  }
 }

以上所生成的为简单的验证码。接下来我从其他的博客中学习了其他形式的效果。

/* 图片画线特殊效果:贝塞尔曲线 */
  graphics graph = graphics.fromimage(image);
  graph.clear(color.whitesmoke);
  point[] myarray ={
     new point(random.next(150),random.next(50)),
     new point(random.next(150),random.next(50)),
     new point(random.next(150),random.next(50)),
     new point(random.next(150),random.next(50)),
     new point(random.next(150),random.next(50)),
     new point(random.next(150),random.next(50)),
     new point(random.next(150),random.next(50)),
     new point(random.next(150),random.next(50)),
     new point(random.next(150),random.next(50)),
     new point(random.next(150),random.next(50))
    };
  pen mypen = new pen(color.blue, 1);
  graphicspath mypath = new graphicspath();
  mypath.addbeziers(myarray);
  graph.drawpath(mypen, mypath);

验证码字符颜色变换效果:实现该效果,我们首先来定义一个颜色集合,然后通过for循环使其随机改变字体颜色则可。

 #region 定义颜色数组
 color[] colors = { color.blue, color.green, color.red, color.gold, color.black, color.chocolate, color.orange, color.purple };
 public color[] colors
 {
  get { return colors; }
  set { colors = value; }
 }
 #endregion
 brush brush;
 int colornum;
 for(int i=0; i
 {
  colornum = random.next(colors.length - 1);
  brush = new system.drawing.solidbrush(colors[cindex]);
 
  //利用drawstring函数进行颜色填充就可以了。
 }

 同样的原理我们也可以定义一个字体的数组来进行验证码字体切换。代码和颜色的类似,这里就不加以累赘。
接下来看看如何使得验证码的字体进行扭曲。

private const double pi = 3.1415926535897932384626433832795;
 private const double pi2 = 6.283185307179586476925286766559;
 ///
 /// 波形滤镜效果函数
 ///
 ///
 ///
 ///
 ///
 ///
 public system.drawing.bitmap twistimage(bitmap srcbmp, double dmultvalue, double dphase)
 {
  system.drawing.bitmap destbmp = new bitmap(srcbmp.width,srcbmp.height);
  system.drawing.graphics graph = system.drawing.graphics.fromimage(destbmp);
  //填充背景图为白色
  graph.fillrectangle(new solidbrush(system.drawing.color.white), 0, 0, destbmp.width, destbmp.height);
  graph.dispose();
  double dbaselen = (double)destbmp.width;
  for (int i = 0; i < destbmp.width; i++)
  {
  for (int j = 0; j < destbmp.height; j++)
  {
   double dx = 0;
   dx = (pi2 * (double)j) / dbaselen;
   dx += dphase;
 
   double dy = math.sin(dx);
 
   int noldx = 0, noldy = 0;
   noldx = i + (int)(dy * dmultvalue);
   noldy = j + (int)(dy * dmultvalue);
   system.drawing.color color = srcbmp.getpixel(i, j);
   if (noldx >= 0 && noldx < destbmp.width && noldy >= 0 && noldy < destbmp.height)
   destbmp.setpixel(noldx, noldy, color);
  }
  }
  return destbmp;
 }

上面代码是参考了这段代码进行的学习<c#.net 好用的验证码代码 汉字-变色-扭曲-波动 >,代码如下


using system;
using system.data;
using system.configuration;
using system.collections;
using system.drawing;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

public partial class study_checkcode2 : system.web.ui.page
{
 protected void page_load(object sender, eventargs e)
 {
 string code = createverifycode();  //取随机码
 createimageonpage(code, this.context); // 输出图片
 response.cookies.add(new httpcookie("checkcode", code.toupper()));// 使用cookies取验证码的值
 }

 #region 验证码长度(默认4个验证码的长度)
 int length = 4;
 public int length
 {
 get { return length; }
 set { length = value; }
 }
 #endregion

 #region 验证码字体大小(为了显示扭曲效果,默认40像素,可以自行修改)
 int fontsize = 40;
 public int fontsize
 {
 get { return fontsize; }
 set { fontsize = value; }
 }
 #endregion

 #region 边框补(默认1像素)
 int padding = 2;
 public int padding
 {
 get { return padding; }
 set { padding = value; }
 }
 #endregion

 #region 是否输出燥点(默认不输出)
 bool chaos = true;
 public bool chaos
 {
 get { return chaos; }
 set { chaos = value; }
 }
 #endregion

 #region 输出燥点的颜色(默认灰色)
 color chaoscolor = color.lightgray;
 public color chaoscolor
 {
 get { return chaoscolor; }
 set { chaoscolor = value; }
 }
 #endregion

 #region 自定义背景色(默认白色)
 color backgroundcolor = color.white;
 public color backgroundcolor
 {
 get { return backgroundcolor; }
 set { backgroundcolor = value; }
 }
 #endregion

 #region 自定义随机颜色数组
 color[] colors = { color.black, color.red, color.darkblue, color.green, color.orange, color.brown, color.darkcyan, color.purple };
 public color[] colors
 {
 get { return colors; }
 set { colors = value; }
 }
 #endregion

 #region 自定义字体数组
 string[] fonts = { "arial", "georgia" };
 public string[] fonts
 {
 get { return fonts; }
 set { fonts = value; }
 }
 #endregion

 #region 自定义随机码字符串序列(使用逗号分隔)
 //string codeserial = "0,1,2,3,4,5,6,7,8,9,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,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 codeserial = "阿,保,持,的,法,规,和,东,三,省,问,我,惹,你,诶,没,改,变,揍,屁,股,吧";
 public string codeserial
 {
 get { return codeserial; }
 set { codeserial = value; }
 }
 #endregion

 #region 产生波形滤镜效果

 private const double pi = 3.1415926535897932384626433832795;
 private const double pi2 = 6.283185307179586476925286766559;

 /// <summary>
 /// 正弦曲线wave扭曲图片(edit by 51aspx.com)
 /// </summary>
 /// <param name="srcbmp">图片路径</param>
 /// <param name="bxdir">如果扭曲则选择为true</param>
 /// <param name="dmultvalue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
 /// <param name="dphase">波形的起始相位,取值区间[0-2*pi)</param>
 /// <returns></returns>
 public system.drawing.bitmap twistimage(bitmap srcbmp, bool bxdir, double dmultvalue, double dphase)
 {
 system.drawing.bitmap destbmp = new bitmap(srcbmp.width, srcbmp.height);

 // 将位图背景填充为白色
 system.drawing.graphics graph = system.drawing.graphics.fromimage(destbmp);
 graph.fillrectangle(new solidbrush(system.drawing.color.white), 0, 0, destbmp.width, destbmp.height);
 graph.dispose();

 double dbaseaxislen = bxdir ? (double)destbmp.height : (double)destbmp.width;

 for (int i = 0; i < destbmp.width; i++)
 {
  for (int j = 0; j < destbmp.height; j++)
  {
  double dx = 0;
  dx = bxdir ? (pi2 * (double)j) / dbaseaxislen : (pi2 * (double)i) / dbaseaxislen;
  dx += dphase;
  double dy = math.sin(dx);

  // 取得当前点的颜色
  int noldx = 0, noldy = 0;
  noldx = bxdir ? i + (int)(dy * dmultvalue) : i;
  noldy = bxdir ? j : j + (int)(dy * dmultvalue);

  system.drawing.color color = srcbmp.getpixel(i, j);
  if (noldx >= 0 && noldx < destbmp.width
   && noldy >= 0 && noldy < destbmp.height)
  {
   destbmp.setpixel(noldx, noldy, color);
  }
  }
 }
 return destbmp;
 }
 #endregion

 #region 生成校验码图片
 public bitmap createimagecode(string code)
 {
 int fsize = fontsize;
 int fwidth = fsize + padding;

 int imagewidth = (int)(code.length * fwidth) + 30 + padding * 2;
 int imageheight = fsize * 2 + padding;

 system.drawing.bitmap image = new system.drawing.bitmap(imagewidth, imageheight);

 graphics g = graphics.fromimage(image);

 g.clear(backgroundcolor);

 random rand = new random();

 //给背景添加随机生成的燥点
 if (this.chaos)
 {

  pen pen = new pen(chaoscolor, 0);
  int c = length * 10;

  for (int i = 0; i < c; i++)
  {
  int x = rand.next(image.width);
  int y = rand.next(image.height);

  g.drawrectangle(pen, x, y, 1, 1);
  }
 }

 int left = 0, top = 0, top1 = 1, top2 = 1;

 int n1 = (imageheight - fontsize - padding * 2);
 int n2 = n1 / 4;
 top1 = n2;
 top2 = n2 * 2;

 font f;
 brush b;

 int cindex, findex;

 //随机字体和颜色的验证码字符
 for (int i = 0; i < code.length; i++)
 {
  cindex = rand.next(colors.length - 1);
  findex = rand.next(fonts.length - 1);

  f = new system.drawing.font(fonts[findex], fsize, system.drawing.fontstyle.bold);
  b = new system.drawing.solidbrush(colors[cindex]);

  if (i % 2 == 1)
  {
  top = top2;
  }
  else
  {
  top = top1;
  }

  left = i * fwidth;

  g.drawstring(code.substring(i, 1), f, b, left, top);
 }

 //画一个边框 边框颜色为color.gainsboro
 g.drawrectangle(new pen(color.gainsboro, 0), 0, 0, image.width - 1, image.height - 1);
 g.dispose();

 //产生波形(add by 51aspx.com)
 image = twistimage(image, true, 8, 4);

 return image;
 }
 #endregion

 #region 将创建好的图片输出到页面
 public void createimageonpage(string code, httpcontext context)
 {
 system.io.memorystream ms = new system.io.memorystream();
 bitmap image = this.createimagecode(code);

 image.save(ms, system.drawing.imaging.imageformat.jpeg);

 context.response.clearcontent();
 context.response.contenttype = "image/jpeg";
 context.response.binarywrite(ms.getbuffer());

 ms.close();
 ms = null;
 image.dispose();
 image = null;
 }
 #endregion

 #region 生成随机字符码
 public string createverifycode(int codelen)
 {
 if (codelen == 0)
 {
  codelen = length;
 }

 string[] arr = codeserial.split(',');

 string code = "";

 int randvalue = -1;

 random rand = new random(unchecked((int)datetime.now.ticks));

 for (int i = 0; i < codelen; i++)
 {
  randvalue = rand.next(0, arr.length - 1);

  code += arr[randvalue];
 }

 return code;
 }
 public string createverifycode()
 {
 return createverifycode(0);
 }
 #endregion

}

一年一度的抢票热潮又开始了,希望大家都能顺利买到回家的火车篇,回家过年,突然感觉有点年味了,一年又一年,时间都去哪了,小小的感慨一下……

言归正传,这就是为大家分享的c#验证码程序,和12306验证码差多了,不过也是小编的学习收获吧!大家也可以结合下面这两篇文章进行学习:

《12306动态验证码启发之asp.net实现动态gif验证码(附源码)》

希望本文所述对大家学习验证码技术有所帮助。

上一篇:

下一篇: