关闭

.net 图片验证码,最终完成版

799人阅读 评论(0) 收藏 举报

昨天试了一下书上的例子,可是在.net 2005下面老是出错误,提示无法找到方法重写,然后试了试另一种方法。

建立一个类,用来绘制随机字符,然后建立a.aspx,在pageload里面调用这个类,然后将登录页面的img 的src属性,设置为这个a.aspx,运行即可。

类的代码。

 using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Web;
using System.Web.SessionState;

namespace WebHelper.Picture
{
    /**/
    /// <summary>
    /// 中文图片验证码
    /// </summary>
    public class Valids : System.Web.UI.Page
    {
        /**/
        /// <summary>
        /// 生成一个随机文字图片,保存在 Session["ValidCode"]
        /// </summary>
        /// <param name="count">图片中字的个数</param>
        /// <returns>生成的文字</returns>
        public static void CreateImage(int count)
        {
            string ValidCode = GenCode(count);
            CreateCheckCodeImage(ValidCode);
            HttpContext.Current.Session["ValidCode"] = ValidCode;
        }

        /**/
        /// <summary>
        /// 产生随机字符串
        /// </summary>
        /// <param name="num">随机出几个字符</param>
        /// <returns>随机出的字符串</returns>
        private static string GenCode(int num)
        {
            string validateCode = "";

            // 随机数对象
            Random random = new Random();

            for (int i = 0; i < num; i++)
            {
                // 26: a - z
                int n = random.Next(26);

                // 将数字转换成大写字母
                validateCode += (char)(n + 65);
            }

                       

            return validateCode;


        }

        /**/
        /// <summary>
        /// 生成图片(增加背景噪音线、前景噪音点)
        /// </summary>
        /// <param name="checkCode">随机出字符串</param>
        private static void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode.Trim() == "" || checkCode == null)
                return;
            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)(checkCode.Length * 21.5), 22);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成随机生成器
                Random random = new Random();

                //清空图片背景色
                g.Clear(Color.White);

                // 画图片的背景噪音线
                int i;
                for (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 System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true);
                g.DrawString(checkCode, font, brush, 2, 2);

                //画图片的前景噪音点
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ContentType = "image/Gif";
                HttpContext.Current.Response.BinaryWrite(ms.ToArray());
            }
            catch
            {
                g.Dispose();
                image.Dispose();
            }
        }
    }
}

 

在a.aspx  的pageload中加入

  WebHelper.Picture.Valids.CreateImage(4);

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:35896次
    • 积分:551
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:11篇
    • 译文:0篇
    • 评论:11条
    文章分类