您的位置:时时app平台注册网站 > 编程知识 > 图表验证码的变化(数字和Turkey语大小写卡塔尔

图表验证码的变化(数字和Turkey语大小写卡塔尔

2019-12-06 10:58

        //纯数字
        //number = random.Next(10);
        //number = oCharacter[random.Next(oCharacter.Length)];
        //checkCode = number.ToString();
      }
      return checkCode;
    }

图片验证码的浮动(数字和克罗地亚语大小写卡塔尔国和交给认证,验证码大小写

中期接触时,认为很有趣,然后就和好研究了下,做了个demo,然后收拾下,后一次得以直接使用啊,匈牙利(Magyarország卡塔尔语大小写,点击能够切换

上代码了。。。。

页面代码:

  <img id="Img" src="/Login/GetCheckCode" />

  本条是页面JQuery点击改动方法

  $("#Img").click(function () {
    //this.src = "/Login/GetCheckCode?time=" (new Date(卡塔尔国卡塔尔(英语:State of Qatar).getTime(卡塔尔国;   //那一个也能够的

    //$("#Img").attr("src", "/Login/GetCheckCode?" Math.random());

    $("#Img").attr("src", "/Login/GetCheckCode?time=" (new Date()).getTime());

    注意:src前边加个time的时刻参数,是为了重新加载验证码,不然加载不了。

    英特网的表达:服务器端编写好生成验证码的措施,由于客商端浏览器会缓存UPAJEROL相仿的能源,能够动用随机数/可能时间来重新央浼:
  });

后台代码:

    public ActionResult GetCheckCode()
    {
      string code = GenerateCheckCodes(4);
      Session["ValidateCode"] = code;
      byte[] bytes = CreateCheckCodeImage(code);
      return File(bytes, @"image/jpeg");
    }

    private string GenerateCheckCodes(int iCount)
    {
      char[] oCharacter = {'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'
              };
      //int number;
      string checkCode = String.Empty;
      int iSeed = DateTime.Now.Millisecond;
      System.Random random = new Random(iSeed);
      for (int i = 0; i < iCount; i )
      {
        checkCode = oCharacter[random.Next(oCharacter.Length)];

        //纯数字
        //number = random.Next(10);
        //number = oCharacter[random.Next(oCharacter.Length)];
        //checkCode = number.ToString();
      }
      return checkCode;
    }

    private byte[] CreateCheckCodeImage(string checkCode)
    {
      if (checkCode == null || checkCode.Trim() == String.Empty)
      {
        return null;
      }
      int iWordWidth = 20;
      int iImageWidth = checkCode.Length * iWordWidth;
      Bitmap image = new Bitmap(iImageWidth, 30);
      Graphics g = Graphics.FromImage(image);
      try
      {
        //生成随机生成器
        Random random = new Random();
        //清空图片背景观
        g.Clear(Color.White);

        //画图片的背景噪音点
        for (int i = 0; i < 20; 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.Silver), x1, y1, x2, y2);
        }

        //画图片的背景噪音线
        for (int i = 0; i < 2; i )
        {
          int x1 = 0;
          int x2 = image.Width;
          int y1 = random.Next(image.Height);
          int y2 = random.Next(image.Height);
          if (i == 0)
          {
            g.DrawLine(new Pen(Color.Gray, 2), x1, y1, x2, y2);
          }

        }
        for (int i = 0; i < checkCode.Length; i )
        {

           string Code = checkCode[i].ToString();
           int xLeft = iWordWidth * (i);
          random = new Random(xLeft);
          int iSeed = DateTime.Now.Millisecond;
          int iValue = random.Next(iSeed) % 4;
          if (iValue == 0)
          {
            Font font = new Font("Arial", 16, (FontStyle.Bold | System.Drawing.FontStyle.Italic));
            Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
            LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5f, true);
            g.DrawString(Code, font, brush, xLeft, 2);
          }
          else if (iValue == 1)
          {
            Font font = new System.Drawing.Font("楷体", 16, (FontStyle.Bold));
            Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
            LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3f, true);
            g.DrawString(Code, font, brush, xLeft, 2);
          }
          else if (iValue == 2)
          {
            Font font = new System.Drawing.Font("宋体", 16, (System.Drawing.FontStyle.Bold));
            Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
            LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2f, true);
            g.DrawString(Code, font, brush, xLeft, 2);
          }
          else if (iValue == 3)
          {
            Font font = new System.Drawing.Font("黑体", 16, (System.Drawing.FontStyle.Bold |       

            System.Drawing.FontStyle.Bold));
            Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
            LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8f, true);
            g.DrawString(Code, font, brush, xLeft, 2);
          }
        }
        ////画图片的前程噪音点 ---有无这段代码 貌似没啥变化
        for (int i = 0; i < 8; i )
        {
          int x = random.Next(image.Width);
          int y = random.Next(image.Height);
          image.SetPixel(x, y, Color.FromArgb(random.Next()));
        }
        //画图片的边框线
        g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width

  • 1, image.Height - 1);
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
            //Response.ClearContent();
            //Response.ContentType = "image/jpeg";
            //Response.BinaryWrite(ms.ToArray());
          }
          finally
          {
            g.Dispose();
            image.Dispose();
        }
      }

好了,就到此处了,有标题要么不科学的可以指正,笔者每一日都会看三遍。感谢,希望对你有扶植。

说一下,这些代码的格式化 ,太难为了,每便都要风流倜傥行大器晚成行的tab键。

 

最先接触时,认为很有意思,然后就协应用研商究了下,做了个demo,然后收拾下...

        }
        for (int i = 0; i < checkCode.Length; i )
        {

    private string GenerateCheckCodes(int iCount)
    {
      char[] oCharacter = {'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'
              };
      //int number;
      string checkCode = String.Empty;
      int iSeed = DateTime.Now.Millisecond;
      System.Random random = new Random(iSeed);
      for (int i = 0; i < iCount; i )
      {
        checkCode = oCharacter[random.Next(oCharacter.Length)];

        //画图片的背景噪音线
        for (int i = 0; i < 2; i )
        {
          int x1 = 0;
          int x2 = image.Width;
          int y1 = random.Next(image.Height);
          int y2 = random.Next(image.Height);
          if (i == 0)
          {
            g.DrawLine(new Pen(Color.Gray, 2), x1, y1, x2, y2);
          }

    //$("#Img").attr("src", "/Login/GetCheckCode?" Math.random());

    互连网的演讲:服务器端编写好生成验证码的章程,由于客商端浏览器会缓存U陆风X8L相似的能源,能够应用随机数/也许时间来再度诉求:
  });

            System.Drawing.FontStyle.Bold));
            Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
            LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8f, true);
            g.DrawString(Code, font, brush, xLeft, 2);
          }
        }
        ////画图片的前程噪音点 ---有无这段代码 貌似没啥变化
        for (int i = 0; i < 8; i )
        {
          int x = random.Next(image.Width);
          int y = random.Next(image.Height);
          image.SetPixel(x, y, Color.FromArgb(random.Next()));
        }
        //画图片的边框线
        g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width

  • 1, image.Height - 1);
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
            //Response.ClearContent();
            //Response.ContentType = "image/jpeg";
            //Response.BinaryWrite(ms.ToArray());
          }
          finally
          {
            g.Dispose();
            image.Dispose();
        }
      }

  $("#Img").click(function () {
    //this.src = "/Login/GetCheckCode?time=" (new Date(卡塔尔(英语:State of Qatar)卡塔尔(قطر‎.getTime(卡塔尔;   //那么些也足以的

    private byte[] CreateCheckCodeImage(string checkCode)
    {
      if (checkCode == null || checkCode.Trim() == String.Empty)
      {
        return null;
      }
      int iWordWidth = 20;
      int iImageWidth = checkCode.Length * iWordWidth;
      Bitmap image = new Bitmap(iImageWidth, 30);
      Graphics g = Graphics.FromImage(image);
      try
      {
        //生成随机生成器
        Random random = new Random();
        //清空图片背景象
        g.Clear(Color.White);

    public ActionResult GetCheckCode()
    {
      string code = GenerateCheckCodes(4);
      Session["ValidateCode"] = code;
      byte[] bytes = CreateCheckCodeImage(code);
      return File(bytes, @"image/jpeg");
    }

  <img id="Img" src="/Login/GetCheckCode" />

后台代码:

上代码了。。。。

        //画图片的背景噪音点
        for (int i = 0; i < 20; 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.Silver), x1, y1, x2, y2);
        }

说一下,那么些代码的格式化 ,太辛苦了,每趟都要后生可畏行业作风流倜傥行的tab键。

           string Code = checkCode[i].ToString();
           int xLeft = iWordWidth * (i);
          random = new Random(xLeft);
          int iSeed = DateTime.Now.Millisecond;
          int iValue = random.Next(iSeed) % 4;
          if (iValue == 0)
          {
            Font font = new Font("Arial", 16, (FontStyle.Bold | System.Drawing.FontStyle.Italic));
            Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
            LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5f, true);
            g.DrawString(Code, font, brush, xLeft, 2);
          }
          else if (iValue == 1)
          {
            Font font = new System.Drawing.Font("楷体", 16, (FontStyle.Bold));
            Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
            LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3f, true);
            g.DrawString(Code, font, brush, xLeft, 2);
          }
          else if (iValue == 2)
          {
            Font font = new System.Drawing.Font("宋体", 16, (System.Drawing.FontStyle.Bold));
            Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
            LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2f, true);
            g.DrawString(Code, font, brush, xLeft, 2);
          }
          else if (iValue == 3)
          {
            Font font = new System.Drawing.Font("黑体", 16, (System.Drawing.FontStyle.Bold |       

 

好了,就到此处了,有标题或然不正确的能够指正,作者天天都会看三遍。谢谢,希望对你有帮扶。

    注意:src后边加个time的时辰参数,是为珍视新加载验证码,不然加载不了。

  其一是页面JQuery点击更改方法

    $("#Img").attr("src", "/Login/GetCheckCode?time=" (new Date()).getTime());

前期接触时,感到很有趣,然后就融洽斟酌了下,做了个demo,然后收拾下,下一次得以一直运用啊,德文大小写,点击能够切换

页面代码:

本文由时时app平台注册网站发布于编程知识,转载请注明出处:图表验证码的变化(数字和Turkey语大小写卡塔尔

关键词:

  • 上一篇:没有了
  • 下一篇:没有了