婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 12306動態驗證碼啟發之ASP.NET實現動態GIF驗證碼(附源碼)

12306動態驗證碼啟發之ASP.NET實現動態GIF驗證碼(附源碼)

熱門標簽:南昌自動外呼系統線路 云南外呼系統代理 海東防封電銷卡 上海市三維地圖標注 寧德防封版電銷卡 辦公用地圖標注網點怎么操作 西寧電銷外呼系統公司 聊城智能電銷機器人電話 安陸市地圖標注app

12306網站推出“彩色動態驗證碼機制”,新版驗證碼不但經常出現字符疊壓,還不停抖動,不少人大呼“看不清”,稱“那個驗證碼,是畢加索的抽象畫么!”鐵總客服則表示:為了能正常購票只能這樣。而多家搶票軟件接近“報廢”,引發不少網友不滿的吐槽稱“太抽象太藝術了”。
以前做項目有時候也會用到驗證碼,但基本都是靜態的,這次也想湊湊12306的熱鬧。閑言少續,切入正題,先上代碼。

實現方法:

 public void ShowCode()
    {
      //對象實例化
      Validate GifValidate = new Validate();

      #region 對驗證碼進行設置(不進行設置時,將以默認值生成)
      //驗證碼位數,不小于4位
      GifValidate.ValidateCodeCount = 4;
      //驗證碼字體型號(默認13)
      GifValidate.ValidateCodeSize = 13;
      //驗證碼圖片高度,高度越大,字符的上下偏移量就越明顯
      GifValidate.ImageHeight = 23;
      //驗證碼字符及線條顏色(需要參考顏色類)
      GifValidate.DrawColor = System.Drawing.Color.BlueViolet;
      //驗證碼字體(需要填寫服務器安裝的字體)
      GifValidate.ValidateCodeFont = "Arial";
      //驗證碼字符是否消除鋸齒
      GifValidate.FontTextRenderingHint = false;
      //定義驗證碼中所有的字符(","分離),似乎暫時不支持中文
      GifValidate.AllChar = "1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
      #endregion

      //輸出圖像(Session名稱)
      GifValidate.OutPutValidate("GetCode");
    }

調用主要方法:

public class Validate
  {
    public string AllChar = "1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
    public Color DrawColor = Color.BlueViolet;
    public bool FontTextRenderingHint = false;
    public int ImageHeight = 0x17;
    private byte TrueValidateCodeCount = 4;
    protected string ValidateCode = "";
    public string ValidateCodeFont = "Arial";
    public float ValidateCodeSize = 13f;

    private void CreateImageBmp(out Bitmap ImageFrame)
    {
      char[] chArray = this.ValidateCode.ToCharArray(0, this.ValidateCodeCount);
      int width = (int) (((this.TrueValidateCodeCount * this.ValidateCodeSize) * 1.3) + 4.0);
      ImageFrame = new Bitmap(width, this.ImageHeight);
      Graphics graphics = Graphics.FromImage(ImageFrame);
      graphics.Clear(Color.White);
      Font font = new Font(this.ValidateCodeFont, this.ValidateCodeSize, FontStyle.Bold);
      Brush brush = new SolidBrush(this.DrawColor);
      int maxValue = (int) Math.Max((float) ((this.ImageHeight - this.ValidateCodeSize) - 3f), (float) 2f);
      Random random = new Random();
      for (int i = 0; i  this.TrueValidateCodeCount; i++)
      {
        int[] numArray = new int[] { (((int) (i * this.ValidateCodeSize)) + random.Next(1)) + 3, random.Next(maxValue) };
        Point point = new Point(numArray[0], numArray[1]);
        if (this.FontTextRenderingHint)
        {
          graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
        }
        else
        {
          graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
        }
        graphics.DrawString(chArray[i].ToString(), font, brush, (PointF) point);
      }
      graphics.Dispose();
    }

    private void CreateImageGif()
    {
      AnimatedGifEncoder encoder = new AnimatedGifEncoder();
      MemoryStream stream = new MemoryStream();
      encoder.Start();
      encoder.SetDelay(5);
      encoder.SetRepeat(0);
      for (int i = 0; i  10; i++)
      {
        Bitmap bitmap;
        this.CreateImageBmp(out bitmap);
        this.DisposeImageBmp(ref bitmap);
        bitmap.Save(stream, ImageFormat.Png);
        encoder.AddFrame(Image.FromStream(stream));
        stream = new MemoryStream();
      }
      encoder.OutPut(ref stream);
      HttpContext.Current.Response.ClearContent();
      HttpContext.Current.Response.ContentType = "image/Gif";
      HttpContext.Current.Response.BinaryWrite(stream.ToArray());
      stream.Close();
      stream.Dispose();
    }

    private void CreateValidate()
    {
      this.ValidateCode = "";
      string[] strArray = this.AllChar.Split(new char[] { ',' });
      int index = -1;
      Random random = new Random();
      for (int i = 0; i  this.ValidateCodeCount; i++)
      {
        if (index != -1)
        {
          random = new Random((i * index) * ((int) DateTime.Now.Ticks));
        }
        int num3 = random.Next(0x23);
        if (index == num3)
        {
          this.CreateValidate();
        }
        index = num3;
        this.ValidateCode = this.ValidateCode + strArray[index];
      }
      if (this.ValidateCode.Length > this.TrueValidateCodeCount)
      {
        this.ValidateCode = this.ValidateCode.Remove(this.TrueValidateCodeCount);
      }
    }

    private void DisposeImageBmp(ref Bitmap ImageFrame)
    {
      Graphics graphics = Graphics.FromImage(ImageFrame);
      Pen pen = new Pen(this.DrawColor, 1f);
      Random random = new Random();
      Point[] pointArray = new Point[2];
      for (int i = 0; i  15; i++)
      {
        pointArray[0] = new Point(random.Next(ImageFrame.Width), random.Next(ImageFrame.Height));
        pointArray[1] = new Point(random.Next(ImageFrame.Width), random.Next(ImageFrame.Height));
        graphics.DrawLine(pen, pointArray[0], pointArray[1]);
      }
      graphics.Dispose();
    }

    public void OutPutValidate(string ValidateCodeSession)
    {
      this.CreateValidate();
      this.CreateImageGif();
      HttpContext.Current.Session[ValidateCodeSession] = this.ValidateCode;
    }

    public byte ValidateCodeCount
    {
      get
      {
        return this.TrueValidateCodeCount;
      }
      set
      {
        if (value > 4)
        {
          this.TrueValidateCodeCount = value;
        }
      }
    }
  }

驗證碼效果:       -----下載源碼-----

以上就是實現ASP.NET的全部過程,還附有源碼,希望可以幫到大家更好地了解ASP.NET驗證碼的生成方法。

您可能感興趣的文章:
  • .NET生成動態驗證碼的完整步驟
  • .NET Core 2.0如何生成圖片驗證碼完整實例
  • ASP.NET Core使用SkiaSharp實現驗證碼的示例代碼
  • 一個簡單的ASP.NET驗證碼
  • .Net Core 下使用ZKWeb.System.Drawing實現驗證碼功能(圖形驗證碼)
  • .net實現動態驗證碼功能

標簽:崇左 平涼 南寧 贛州 汕尾 青海 衢州 洛陽

巨人網絡通訊聲明:本文標題《12306動態驗證碼啟發之ASP.NET實現動態GIF驗證碼(附源碼)》,本文關鍵詞  12306,動態,驗證,碼啟,發之,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《12306動態驗證碼啟發之ASP.NET實現動態GIF驗證碼(附源碼)》相關的同類信息!
  • 本頁收集關于12306動態驗證碼啟發之ASP.NET實現動態GIF驗證碼(附源碼)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 昌乐县| 阿合奇县| 宁德市| 凌海市| 思茅市| 伽师县| 牟定县| 日喀则市| 苍梧县| 周口市| 旬邑县| 司法| 民县| 阜南县| 靖江市| 阳信县| 永寿县| 上林县| 崇仁县| 永嘉县| 黔南| 宝鸡市| 崇仁县| 凤庆县| 汝城县| 通渭县| 崇义县| 思南县| 桦南县| 广南县| 调兵山市| 江阴市| 拜泉县| 江永县| 临夏县| 大邑县| 兰溪市| 武乡县| 新乐市| 乐亭县| 托克逊县|