MVC 一款简洁漂亮的登录验证码


    一款简洁漂亮的验证码,代码也简单明了,首先写个生成验证码方法的类然后在Action中调用,在页面中以图片的方式显示即可,最终效果

1.用到的生成验证码的类

using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web; namespace PageDemo.Models { public class ValidateCode { /// <summary>
        /// 生成简洁漂亮的图形验证码 /// </summary>
        /// <param name="Code">传出验证码</param>
        /// <param name="CodeLength">验证码长度</param>
        /// <param name="Width">生成验证码图片的宽度</param>
        /// <param name="Height">生成验证码图片的高度</param>
        /// <param name="FontSize">验证码字符大小</param>
        /// <returns></returns>
        public static byte[] CreateValidateGraphic(out String Code, int CodeLength, int Width, int Height, int FontSize) { String sCode = String.Empty; //颜色列表,用于验证码、噪线、噪点
            Color[] oColors ={ System.Drawing.Color.Black, System.Drawing.Color.Red, System.Drawing.Color.Blue, System.Drawing.Color.Green, System.Drawing.Color.Orange, System.Drawing.Color.Brown, System.Drawing.Color.Brown, System.Drawing.Color.DarkBlue }; //字体列表,用于验证码
            string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" }; //验证码的字元集,去掉了一些容易混淆的字符
            char[] oCharacter = { '2','3','4','5','6','8','9', 'A','B','C','D','E','F','G','H','J','K', 'L','M','N','P','R','S','T','W','X','Y' }; Random oRnd = new Random(); Bitmap oBmp = null; Graphics oGraphics = null; int N1 = 0; System.Drawing.Point oPoint1 = default(System.Drawing.Point); System.Drawing.Point oPoint2 = default(System.Drawing.Point); string sFontName = null; Font oFont = null; Color oColor = default(Color); //生成验证码字串
            for (N1 = 0; N1 <= CodeLength - 1; N1++) { sCode += oCharacter[oRnd.Next(oCharacter.Length)]; } oBmp = new Bitmap(Width, Height); oGraphics = Graphics.FromImage(oBmp); oGraphics.Clear(System.Drawing.Color.White); try { for (N1 = 0; N1 <= 4; N1++) { //画噪线
                    oPoint1.X = oRnd.Next(Width); oPoint1.Y = oRnd.Next(Height); oPoint2.X = oRnd.Next(Width); oPoint2.Y = oRnd.Next(Height); oColor = oColors[oRnd.Next(oColors.Length)]; oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2); } float spaceWith = 0, dotX = 0, dotY = 0; if (CodeLength != 0) { spaceWith = (Width - FontSize * CodeLength - 10) / CodeLength; } for (N1 = 0; N1 <= sCode.Length - 1; N1++) { //画验证码字串
                    sFontName = oFontNames[oRnd.Next(oFontNames.Length)]; oFont = new Font(sFontName, FontSize, FontStyle.Italic); oColor = oColors[oRnd.Next(oColors.Length)]; dotY = (Height - oFont.Height) / 2 + 2;//中心下移2像素
                    dotX = Convert.ToSingle(N1) * FontSize + (N1 + 1) * spaceWith; oGraphics.DrawString(sCode[N1].ToString(), oFont, new SolidBrush(oColor), dotX, dotY); } for (int i = 0; i <= 30; i++) { //画噪点
                    int x = oRnd.Next(oBmp.Width); int y = oRnd.Next(oBmp.Height); Color clr = oColors[oRnd.Next(oColors.Length)]; oBmp.SetPixel(x, y, clr); } Code = sCode; //保存图片数据
                MemoryStream stream = new MemoryStream(); oBmp.Save(stream, ImageFormat.Jpeg); //输出图片流
                return stream.ToArray(); } finally { oGraphics.Dispose(); } } } }

2.Action中的调用

 public ActionResult GetImg() { int width = 100; int height = 40; int fontsize = 20; string code = string.Empty; byte[] bytes = ValidateCode.CreateValidateGraphic(out code, 4, width, height, fontsize); Session["ValidateCode"] = code; return File(bytes, @"image/jpeg"); }

3.视图中的显示

<img id="GL_StandardCode" style="cursor: pointer;" src="@Url.Action("GetImg", "Home")?t=@DateTime.Now.Ticks" title="看不清,点击换一张" />

js刷新图片验证码

$("#GL_StandardCode").click(function () { var newSrc = "@Url.Action("GetImg", "ValidateCode")" + "?t=" + (new Date()).getTime(); this.src=newSrc; return false; });

4提交前的验证

 public bool CheckValidateCode(string num) { string cnum = Session["ValidateCode"] == null ? "" : Session["ValidateCode"].ToString(); if (num.ToLower() == cnum.ToLower()&& !string.IsNullOrEmpty(num)) { return true; } else { return false; } }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM