当前位置: 移动技术网 > IT编程>开发语言>c# > WinForm生成验证码图片的方法

WinForm生成验证码图片的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了winform生成验证码图片的方法。分享给大家供大家参考,具体如下:

1、创建validcode类:

public class validcode
{
  #region private fields
  private const double pi = 3.1415926535897932384626433832795;
  private const double pi2 = 6.283185307179586476925286766559;
  //private readonly int _wordslen = 4;
  private int _len;
  private codetype _codetype;
  private readonly single _jianju = (float)18.0;
  private readonly single _height = (float)24.0;
  private string _checkcode;
  #endregion
  #region public property
  public string checkcode
  {
   get
   {
    return _checkcode;
   }
  }
  #endregion
  #region constructors
  /// <summary>
  /// public constructors
  /// </summary>
  /// <param name="len"> 验证码长度 </param>
  /// <param name="ctype"> 验证码类型:字母、数字、字母+ 数字 </param>
  public validcode(int len, codetype ctype)
  {
   this._len = len;
   this._codetype = ctype;
  }
  #endregion
  #region public field
  public enum codetype { words, numbers, characters, alphas }
  #endregion
  #region private methods
  private string generatenumbers()
  {
   string strout = "";
   system.random random = new random();
   for (int i = 0; i < _len; i++)
   {
    string num = convert.tostring(random.next(10000) % 10);
    strout += num;
   }
   return strout.trim();
  }
  private string generatecharacters()
  {
   string strout = "";
   system.random random = new random();
   for (int i = 0; i < _len; i++)
   {
    string num = convert.tostring((char)(65 + random.next(10000) % 26));
    strout += num;
   }
   return strout.trim();
  }
  //
  private string generatealphas()
  {
   string strout = "";
   string num = "";
   system.random random = new random();
   for (int i = 0; i < _len; i++)
   {
    if (random.next(500) % 2 == 0)
    {
     num = convert.tostring(random.next(10000) % 10);
    }
    else
    {
     num = convert.tostring((char)(65 + random.next(10000) % 26));
    }
    strout += num;
   }
   return strout.trim();
  }
  private 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 methods
  public stream createcheckcodeimage()
  {
   string checkcode;
   switch (_codetype)
   {
    case codetype.alphas:
     checkcode = generatealphas();
     break;
    case codetype.numbers:
     checkcode = generatenumbers();
     break;
    case codetype.characters:
     checkcode = generatecharacters();
     break;
    default:
     checkcode = generatealphas();
     break;
   }
   this._checkcode = checkcode;
   memorystream ms = null;
   //
   if (checkcode == null || checkcode.trim() == string.empty)
    return null;
   bitmap image = new system.drawing.bitmap((int)math.ceiling((checkcode.length * _jianju)), (int)_height);
   graphics g = graphics.fromimage(image);
   try
   {
    random random = new random();
    g.clear(color.white);
    // 画图片的背景噪音线
    for (int i = 0; i < 18; i++)
    {
     int x1 = random.next(image.width);
     int x2 = random.next(image.width);
     int y1 = random.next(image.height);
     int y2 = random.next(image.height);
     g.drawline(new pen(color.fromargb(random.next()), 1), x1, y1, x2, y2);
    }
    font font = new system.drawing.font("times new roman", 14, system.drawing.fontstyle.bold);
    lineargradientbrush brush = new lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true);
    if (_codetype != codetype.words)
    {
     for (int i = 0; i < checkcode.length; i++)
     {
      g.drawstring(checkcode.substring(i, 1), font, brush, 2 + i * _jianju, 1);
     }
    }
    else
    {
     g.drawstring(checkcode, font, brush, 2, 2);
    }
    // 画图片的前景噪音点
    for (int i = 0; i < 150; i++)
    {
     int x = random.next(image.width);
     int y = random.next(image.height);
     image.setpixel(x, y, color.fromargb(random.next()));
    }
    // 画图片的波形滤镜效果
    if (_codetype != codetype.words)
    {
     image = twistimage(image, true, 3, 1);
    }
    // 画图片的边框线
    g.drawrectangle(new pen(color.silver), 0, 0, image.width - 1, image.height - 1);
    ms = new system.io.memorystream();
    image.save(ms, system.drawing.imaging.imageformat.gif);
   }
   finally
   {
    g.dispose();
    image.dispose();
   }
   return ms;
  }
  #endregion
}

2、产生验证码图片代码:

//参数一:产生几个字符的验证码图片 参数二:验证码的形式(数字、字母、数字字母混合都有)
validcode validcode = new validcode(5, validcode.codetype.alphas);
this.picturebox2.image = bitmap.fromstream(validcode.createcheckcodeimage());

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网