最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#生成驗(yàn)證碼圖片的方法

 更新時(shí)間:2018年10月04日 09:55:01   作者:薛定諤家的貓  
這篇文章主要為大家詳細(xì)介紹了C#生成驗(yàn)證碼圖片的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#生成驗(yàn)證碼圖片的具體代碼,供大家參考,具體內(nèi)容如下

/// <summary>
    /// 生成驗(yàn)證碼圖片
    /// </summary>
    /// <returns></returns>
    public byte[] GetVerifyCode()
    {
      int codeW = 80;
      int codeH = 40;
      int fontSize = 18;
      string chkCode = string.Empty;
      //顏色列表,用于驗(yàn)證碼、噪線、噪點(diǎn) 
      Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
      //字體列表,用于驗(yàn)證碼 
      string[] font = { "Times New Roman" };
      //驗(yàn)證碼的字符集,去掉了一些容易混淆的字符 
      char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
      Random rnd = new Random();
      //生成驗(yàn)證碼字符串 
      for (int i = 0; i < 4; i++)
      {
        chkCode += character[rnd.Next(character.Length)];
      }

      //創(chuàng)建畫布
      Bitmap bmp = new Bitmap(codeW, codeH);
      Graphics g = Graphics.FromImage(bmp);
      g.Clear(Color.White);
      //畫噪線 
      for (int i = 0; i < 1; i++)
      {
        int x1 = rnd.Next(codeW);
        int y1 = rnd.Next(codeH);
        int x2 = rnd.Next(codeW);
        int y2 = rnd.Next(codeH);
        Color clr = color[rnd.Next(color.Length)];
        g.DrawLine(new Pen(clr), x1, y1, x2, y2);
      }
      //畫驗(yàn)證碼字符串 
      for (int i = 0; i < chkCode.Length; i++)
      {
        string fnt = font[rnd.Next(font.Length)];
        Font ft = new Font(fnt, fontSize);
        Color clr = color[rnd.Next(color.Length)];
        g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0);
      }
      //將驗(yàn)證碼圖片寫入內(nèi)存流,并將其以 "image/Png" 格式輸出 
      MemoryStream ms = new MemoryStream();
      try
      {
        bmp.Save(ms, ImageFormat.Png);
        return ms.ToArray();
      }
      catch (Exception)
      {
        return null;
      }
      finally
      {
        g.Dispose();
        bmp.Dispose();
      }
    }

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

乐昌市| 凉山| 土默特右旗| 嘉善县| 黎城县| 长宁区| 思南县| 奇台县| 紫阳县| 闻喜县| 大石桥市| 广汉市| 临洮县| 绥宁县| 巴彦淖尔市| 安图县| 铅山县| 长丰县| 上栗县| 西平县| 措美县| 会同县| 吴川市| 南靖县| 鄂托克前旗| 景德镇市| 金寨县| 大安市| 博野县| 宜川县| 潼关县| 恩平市| 南和县| 都匀市| 岱山县| 东丽区| 三穗县| 嵊州市| 报价| 区。| 长子县|