[實戰]MVC5+EF6+MySql企業網盤實戰(2)——驗證碼

來源:互聯網
上載者:User

標籤:噪點   code   單擊   function   font   com   reading   label   and   

寫在前面

斷斷續續,今天算是把驗證碼的東東弄出來了。

系列文章

[EF]vs15+ef6+mysql code first方式

[實戰]MVC5+EF6+MySql企業網盤實戰(1)

[實戰]MVC5+EF6+MySql企業網盤實戰(2)——使用者註冊

[實戰]MVC5+EF6+MySql企業網盤實戰(2)——驗證碼

驗證碼類
using System;using System.Collections.Generic;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Wolfy.NetDisk.Utilities{    /// <summary>    /// 驗證碼類    /// </summary>    public static class VerifyCode    {        /// <summary>        /// 產生驗證碼        /// </summary>        /// <param name="len">驗證碼長度</param>        /// <param name="strCode">驗證碼</param>        /// <returns>驗證碼圖片</returns>        public static byte[] Create(int len, out string strCode)        {            MemoryStream stream = new MemoryStream();            byte[] buffer = null;            //噪線 噪點            Color[] colors = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };            //驗證碼字型            string[] fonts = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };            //驗證碼內容            char[] charactars = { ‘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‘, ‘S‘, ‘Y‘, ‘Z‘ };            //產生驗證碼字串            StringBuilder sb = new StringBuilder();            Random r = new Random();            for (int i = 0; i < len; i++)            {                char codeChar = charactars[r.Next(0, charactars.Length)];                sb.Append(codeChar);            }            strCode = sb.ToString();            using (Bitmap bitmap = new Bitmap(len * 25, 35))            {                using (Graphics g = Graphics.FromImage(bitmap))                {                    g.Clear(Color.White);                    for (int i = 0; i < 10; i++)                    {                        int x1 = r.Next(100);                        int y1 = r.Next(40);                        int x2 = r.Next(100);                        int y2 = r.Next(40);                        Color color = colors[r.Next(colors.Length)];                        g.DrawLine(new Pen(color), x1, y1, x2, y2);                    }                    //將字串畫入圖片                    for (int i = 0; i < strCode.Length; i++)                    {                        string font = fonts[r.Next(fonts.Length)];                        Font fnt = new Font(font, 18);                        Color color = colors[r.Next(colors.Length)];                        g.DrawString(strCode[i].ToString(), fnt, new SolidBrush(color), (float)i * 20 + 8, (float)8);                    }                    //畫噪點                    for (int i = 0; i < 100; i++)                    {                        int x = r.Next(bitmap.Width);                        int y = r.Next(bitmap.Height);                        Color color = colors[r.Next(colors.Length)];                        bitmap.SetPixel(x, y, color);                    }                }                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);                buffer = stream.ToArray();            }            return buffer;        }    }}

 

在UserInfoController中添加Action

  [HttpGet]        public ActionResult VerifyCodeImage()        {            string strCode = string.Empty;            byte[] buffer = VerifyCode.Create(6, out strCode);            Session["code"] = strCode;            return File(buffer, @"image/jpeg");        }

 

註冊頁面上添加驗證碼的表單輸入框

    <div class="form-group">            <label class="control-label col-md-2">驗證碼</label>            <div class="col-md-10">                <input type="text" name="name" value=" " />  <img id="imgCode" style="cursor:pointer;" title="切換下一張" src="/UserInfo/VerifyCodeImage" alt="驗證碼" />            </div>        </div>

 

為驗證碼圖片添加單擊事件,切換下一張,為了避免緩衝,添加隨機數參數,保證每次請求是一次新的請求。

<script>    $(function () {        $("#imgCode").click(function () {            var ran = Math.random();            //加上隨機數ran,避免驗證碼緩衝。            this.src = "/UserInfo/VerifyCodeImage?_r=" + ran;        });    });   </script>

頁面測試

總結

今天比平時早回來會兒,就趁著做了一個功能,進度有點慢啊。

[實戰]MVC5+EF6+MySql企業網盤實戰(2)——驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.