国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

理解C#生成驗(yàn)證碼的過程

2019-10-29 21:30:44
字體:
供稿:網(wǎng)友
這篇文章主要介紹了C#生成驗(yàn)證碼的過程,通過實(shí)例分析C#驗(yàn)證碼的生成原理,感興趣的小伙伴們可以參考一下
 

本文實(shí)例為大家介紹了生成驗(yàn)證碼的詳細(xì)過程,供大家參考,具體內(nèi)容如下

生成驗(yàn)證碼的類:

using System;using System.Collections.Generic;using System.Drawing;using System.Text;namespace Controllers.Core.Util{  /// <summary>  /// 驗(yàn)證碼  /// </summary>  public class VerifyCodeHelper : AdminBaseController  {    #region 變量    /// <summary>    /// 顏色表    /// </summary>    private static Color[] colors = new Color[]{      Color.FromArgb(220,20,60),      Color.FromArgb(128,0,128),      Color.FromArgb(65,105,225),      Color.FromArgb(70,130,180),      Color.FromArgb(46,139,87),      Color.FromArgb(184,134,11),      Color.FromArgb(255,140,0),      Color.FromArgb(139,69,19),      Color.FromArgb(0,191,255),      Color.FromArgb(95,158,160),      Color.FromArgb(255,20,147),      Color.FromArgb(255,165,0)};    /// <summary>    /// 字體表    /// </summary>    private static string[] fonts = new string[] {       "Arial",      "Verdana",       "Georgia",       "黑體" };    /// <summary>    /// 字體大小    /// </summary>    private static int fontSize = 22;    #endregion    #region 生成驗(yàn)證碼圖片    /// <summary>    /// 生成驗(yàn)證碼圖片    /// </summary>    public static Bitmap CreateVerifyCodeBmp(out string code)    {      int width = 120;      int height = 40;      Bitmap bmp = new Bitmap(width, height);      Graphics g = Graphics.FromImage(bmp);      Random rnd = new Random();      //背景色      g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, width, height));      //文字      StringBuilder sbCode = new StringBuilder();      for (int i = 0; i < 4; i++)      {        string str = GetChar(rnd);        Font font = GetFont(rnd);        Color color = GetColor(rnd);        g.DrawString(str, font, new SolidBrush(color), new PointF((float)(i * width / 4.0), 0));        sbCode.Append(str);      }      code = sbCode.ToString();      //噪音線      for (int i = 0; i < 10; i++)      {        int x1 = rnd.Next(bmp.Width);        int x2 = rnd.Next(bmp.Width);        int y1 = rnd.Next(bmp.Height);        int y2 = rnd.Next(bmp.Height);        Pen p = new Pen(GetColor(rnd), 1);        g.DrawLine(p, x1, y1, x2, y2);      }      //扭曲      bmp = TwistImage(bmp, true, 3, rnd.NextDouble() * Math.PI * 2);      g = Graphics.FromImage(bmp);      //噪點(diǎn)      for (int i = 0; i < 100; i++)      {        int x1 = rnd.Next(bmp.Width);        int y1 = rnd.Next(bmp.Height);        Pen p = new Pen(GetColor(rnd), 1);        g.DrawRectangle(p, x1, y1, 1, 1);      }      //邊框      g.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(153, 153, 153))), new Rectangle(0, 0, width - 1, height - 1));      return bmp;    }    #endregion    #region 獲取隨機(jī)字符    /// <summary>    /// 獲取隨機(jī)字符    /// </summary>    private static string GetChar(Random rnd)    {      int n = rnd.Next(0, 61);      if (n <= 9)      {        return ((char)(48 + n)).ToString();      }      else if (n <= 35)      {        return ((char)(65 + n - 10)).ToString();      }      else      {        return ((char)(97 + n - 36)).ToString();      }    }    #endregion    #region 獲取隨機(jī)字體    /// <summary>    /// 獲取隨機(jī)字體    /// </summary>    private static Font GetFont(Random rnd)    {      return new Font(fonts[rnd.Next(0, fonts.Length)], fontSize, FontStyle.Bold);    }    #endregion    #region 獲取隨機(jī)顏色    /// <summary>    /// 獲取隨機(jī)顏色    /// </summary>    private static Color GetColor(Random rnd)    {      return colors[rnd.Next(0, colors.Length)];    }    #endregion    #region 正弦曲線Wave扭曲圖片    /// <summary>      /// 正弦曲線Wave扭曲圖片(Edit By 51aspx.com)      /// </summary>      /// <param name="srcBmp">圖片路徑</param>      /// <param name="bXDir">如果扭曲則選擇為True</param>      /// <param name="nMultValue">波形的幅度倍數(shù),越大扭曲的程度越高,一般為3</param>      /// <param name="dPhase">波形的起始相位,取值區(qū)間[0-2*PI)</param>      private static 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 ? (Math.PI * 2 * (double)j) / dBaseAxisLen : (Math.PI * 2 * (double)i) / dBaseAxisLen;          dx += dPhase;          double dy = Math.Sin(dx);          // 取得當(dāng)前點(diǎn)的顏色            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  }}

驗(yàn)證碼頁面Action:

public ActionResult VerifyCode(){  string code;  Bitmap bmp = VerifyCodeHelper.CreateVerifyCodeBmp(out code);  Bitmap newbmp = new Bitmap(bmp, 108, 36);  HttpContext.Session["VerifyCode"] = code;  Response.Clear();  Response.ContentType = "image/bmp";  newbmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Bmp);  return View();}

說明:前臺頁面為空的cshtml頁面,驗(yàn)證碼的值放在Session中。

使用驗(yàn)證碼的頁面:

顯示驗(yàn)證碼的img:

<img id="verifyCode" src="" style="margin: 0px; padding: 0px; line-height: 25.2px; width: 660px; overflow: hidden; clear: both;">

$(function () {  //刷新驗(yàn)證碼  $("#refreshVerifyCode").click(function () {    refreshVerifyCode(); //刷新驗(yàn)證碼  });  $("#verifyCode").click(function () {    refreshVerifyCode(); //刷新驗(yàn)證碼  });  refreshVerifyCode();});

刷新驗(yàn)證碼:

//刷新驗(yàn)證碼function refreshVerifyCode() {  $("#verifyCode").attr("src", "VerifyCode?t=" + new Date().valueOf());} 

判斷用戶輸入的文本是否與驗(yàn)證碼相同的Action:

public ActionResult CheckVCode(string vcode){  if (HttpContext.Session["VerifyCode"].ToString().ToLower() == vcode.ToLower())  {    Dictionary<string, object> dic = new Dictionary<string, object>();    dic["ok"] = true;    return Content(JsonConvert.SerializeObject(dic));  }  else  {    Dictionary<string, object> dic = new Dictionary<string, object>();    dic["ok"] = false;    return Content(JsonConvert.SerializeObject(dic));  }}

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)C#生成驗(yàn)證碼的方法有所幫助。



上一篇:基于C#實(shí)現(xiàn)網(wǎng)頁爬蟲

下一篇:C#進(jìn)階系列 WebApi身份認(rèn)證解決方案推薦:Basic基礎(chǔ)認(rèn)證

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
學(xué)習(xí)交流
熱門圖片

新聞熱點(diǎn)

疑難解答

圖片精選

網(wǎng)友關(guān)注

主站蜘蛛池模板: 任丘市| 扬中市| 崇信县| 太湖县| 搜索| 平安县| 桂平市| 乐昌市| 绥滨县| 沁水县| 东丰县| 陇西县| 寿阳县| 威宁| 桂林市| 屏山县| 内黄县| 南召县| 齐河县| 诏安县| 临漳县| 改则县| 甘孜县| 余江县| 绍兴县| 青铜峡市| 江安县| 黄骅市| 浦江县| 越西县| 武定县| 阳信县| 荆州市| 普格县| 古田县| 宜春市| 祁连县| 齐齐哈尔市| 大田县| 邛崃市| 鹤岗市|