Code.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace NationalUnion.Member.Models
{
    /// <summary>
    /// 生成验证码的类
    /// </summary>
    public class ValidateCode
    {

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="length">指定验证码的长度</param>
        /// <returns></returns>
        public string CreateValidateCode(int length)
        {
            StringBuilder strCodeBuilder = new StringBuilder();
            string strCode = "0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";

            List<char> vCharList = strCode.ToList<char>();
            int maxValule = vCharList.Count() - 1;
            for (int i = 0; i < length; i++)
            {
                Random rd = new Random();
                int number=rd.Next(0, maxValule);
                string strValue = vCharList[number].ToString();
                strCodeBuilder.Append(strValue);
            }
            return strCodeBuilder.ToString();
        }

        public byte[] CreateValidateGraphicMatrix(string validateCode)
        {
            Bitmap image = new Bitmap(80, 30);
            Graphics g = Graphics.FromImage(image);
            try
            {
                Matrix matrix = new Matrix();//Matrix封装表示几何变换的 3 x 3 仿射矩阵

                //生成随机生成器
                Random random = new Random();
                //清空图片背景色
                g.Clear(Color.White);
                //画图片的干扰线
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
                 Color.Blue, Color.DarkRed, 1.2f, true);

                char[] strValue = validateCode.ToCharArray();

                for (int i = 0; i < strValue.Length; i++)
                {
                    int y = 2;
                    if (i % 2 == 0)
                    {
                        y = -15 - i * 5;
                        matrix.Rotate(30);//对此 Matrix 沿原点顺时针旋转指定角度
                    }
                    else
                    {
                        matrix.Rotate(-30);//对此 Matrix 沿原点顺时针旋转指定角度
                    }  
                    g.Transform = matrix;
                    g.DrawString(strValue[i].ToString(), font, brush, 20 + i * 15, y);
                }

                //画图片的前景干扰点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                //保存图片数据
                MemoryStream stream = new MemoryStream();
                image.Save(stream, ImageFormat.Jpeg);
                //输出图片流
                return stream.ToArray();
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }  
        }
    }
}
View Code

Controller

using NationalUnion.Member.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NationalUnion.Member.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ValidateCode()
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(4);
            Session["ValidateCode"] = code;
            byte[] bytes = vCode.CreateValidateGraphicMatrix(code);
            return File(bytes, @"image/jpeg");

        }
    }
}
View Code

Index.cshml

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
    <div>
        <a href="@Url.Action("UserBasesicInfo","UserCenter")">进入用户中心</a>
        <img id="valiCode" style="cursor: pointer;" src="@Url.Action("ValidateCode","Home")" alt="验证码" />
    </div>
View Code

 

生成的效果:

  

posted on 2014-06-12 15:05  xuxu_dragon  阅读(1364)  评论(0编辑  收藏  举报