Beacher-

asp.net之图片验证码生成

网站中使用图片验证分三步

第一生成验证码字符串

第二根据字符串创建图片

第三对比验证码

cs文件

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Drawing;


namespace WebApplication1
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler1 : IHttpHandler,System.Web.SessionState.IRequiresSessionState
    {
        //验证码的字符集
        public string charSet = "2,3,4,5,6,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,R,S,U,W,X,Y";

        public void ProcessRequest(HttpContext context)
        {
            string validateCode = CreateRandomCode(5);
            context.Session["validateCode"] = validateCode;
            CreateImage(context, validateCode);
           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="n">位数</param>
        /// <returns>验证码字符串</returns>
        public string CreateRandomCode(int n)
        {
            string[] CharArray = charSet.Split(',');
            string randomCode = string.Empty;
            int temp = -1;

            //随机数实例
            Random rand = new Random();
            for (int i = 0; i < n; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * (int)DateTime.Now.Ticks);
                }
                int t = rand.Next(CharArray.Length - 1);
                if (temp == t)
                {
                    return CreateRandomCode(n);
                }
                temp = t;
                randomCode += CharArray[t];
               
            }
            return randomCode;
        }

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="context"></param>
        /// <param name="checkCode"></param>
        private void CreateImage(HttpContext context,string checkCode)
        {
            //宽度,高度
            int iwidth = checkCode.Length * 13;
            int iheight = 23;

            //创建一个空白的图片对象
            System.Drawing.Bitmap image = new Bitmap(iwidth, iheight);
            //画布 创建绘画图层
            Graphics g = Graphics.FromImage(image);

            //定义字体,颜色等fontfamily字体类型
            Font f = new Font("Arial", 12, (System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Bold|System.Drawing.FontStyle.Strikeout));

            //前景色 创建画笔
            Brush b = new System.Drawing.SolidBrush(Color.Black);

            //背景色
            g.Clear(Color.White);

           
            //填充文字
            g.DrawString(checkCode, f, b,0,1);

            //随机线条
            Pen linePen = new Pen(Color.Gray, 0);
            Random rand = new Random();
            for (int i = 0; i < 5; i++)
            {
                int x1 = rand.Next(image.Width);
                int y1 = rand.Next(image.Height);
                int x2 = rand.Next(image.Width);
                int y2 = rand.Next(image.Height);

                g.DrawLine(linePen, x1, y1, x2, y2);
            }

            //随机干扰点
            for (int i = 0; i < 30; i++)
            {
                int x1 = rand.Next(image.Width);
                int y1 = rand.Next(image.Height);
                image.SetPixel(x1, y1, Color.Gray);
               
            }


            //加边框
            g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1);

            //生成图片
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            //输入图片
            context.Response.ContentType = "image/Jpeg";
            context.Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();

           

        }
    }
}

 

aspx页面--

<asp:Image ID="Image1" runat="server" ImageUrl="~/Handler1.ashx" />

 

对比直接跟session[”validateCode“]就行了

 效果图

posted on 2012-02-23 14:44  Beacher-  阅读(284)  评论(0编辑  收藏  举报

导航