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

C# winform登陆框验证码的实现方法

程序员文章站 2023-12-09 17:26:27
本文实例为大家分享了c# winform登陆框验证码的具体代码,供大家参考,具体内容如下 1、  新建一个简单的 windows 应用项目 在默认的 form...

本文实例为大家分享了c# winform登陆框验证码的具体代码,供大家参考,具体内容如下

1、  新建一个简单的 windows 应用项目

在默认的 form1 中添加如下控件:

1)  label : text = “ 输入验证码 :”

2)  textbox : name=” txtvalidcode” 输入验证码的文本框

3)  image : name=” picvalidcode” 显示验证码的图片控件

4)  button :单击事件进行验证码验证

 2、  新建一个产生验证码的核心类,并构建其一个实例:

validcode validcode = new validcode (5,validcode .codetype .numbers)

using system;
using system.collections.generic;
using system.text;
using system.io;
using system.drawing;
using system.drawing.drawing2d;

 

namespace bighorselib.security

{

  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

  }

}

 

3、  图片加载验证码、验证码验证

// 图片加载验证码
picvalidcode.image = bitmap .fromstream(validcode.createcheckcodeimage())

// 验证
  if (!this .txtvalidcode.text.equals(_validcode.checkcode))

   {

     messagebox .show(" 请输入正确的验证码!" , this .text);

     this .txtvalidcode.focus();

     return ;

 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。