马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

ASP.NET自定义控件制作验证码代码示例

  [复制链接]
查看5647 | 回复2 | 2012-12-25 15:30:55 | 显示全部楼层 |阅读模式
上节课讲解了asp.net自定义控件的概述
这次给大家共享一个ASP.NET自定义控件制作的算术验证码。

首先封装好变量
[C#] 纯文本查看 复制代码
 
        /// <summary>
        /// 加数
        /// </summary>
        private int number1;

        private int Number1
        {
            get { return number1; }
            set { number1 = value; }
        }
        /// <summary>
        /// 被加数
        /// </summary>
        private int number2;

        private int Number2
        {
            get { return number2; }
            set { number2 = value; }
        }

        /// <summary>
        /// 结果
        /// </summary>
        private int result;

        private int Result
        {
            get { return result; }
            set { result = value; }
        }



开始制作验证码

[C#] 纯文本查看 复制代码
 /// <summary>
        /// 生成验证码
        /// </summary>
        private void CreateCode()
        {

            HttpResponse resp = this.Page.Response;

            //用来存取验证码
            String chkCode = string.Empty;

            //颜色列表
            Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };

            //字体列表
            String[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };

            Random rnd = new Random();
            
            //阿拉伯数字
            int[] num1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            //汉字数字
            Char[] num2 = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };
            //汉字数字2
            Char[] num3 = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };

            int[] suiJi = { 1, 2, 3 };

            int nn = suiJi[rnd.Next(suiJi.Length)];

            if (nn == 1)
            {
                int i3 = rnd.Next(num3.Length);
                Char c3 = num3[i3];
                int i2 = rnd.Next(num2.Length);
                Char c2 = num2[i2];
                Number1 = i3;
                Number2 = i2;

                Result = Number1 + Number2;

                chkCode = c3 + "+" + c2 + "=" + "?";
            }
            if (nn == 2)
            {
                int i3 = rnd.Next(num3.Length);
                Char c3 = num3[i3];
                int i2 = rnd.Next(num2.Length);
                Char c2 = num2[i2];
                Number1 = i3;
                Number2 = i2;

                if (Number1 < Number2)
                {
                    this.CreateCode();
                }

                Result = Number1 - Number2;

                chkCode = "?" + "+" + c2 + "=" + c3;
            }
            if (nn == 3)
            {
                int i3 = rnd.Next(num3.Length);
                Char c3 = num3[i3];
                int i2 = rnd.Next(num2.Length);
                Char c2 = num2[i2];
                Number1 = i3;
                Number2 = i2;


                if (Number1 > Number2)
                {
                    this.CreateCode();
                }
                Result = Number2 - Number1;

                chkCode = c3 + "+" + "?" + "=" + c2;
            }


            //保存验证码到Session对象
            this.Page.Session["Code"] = Result;

            //创建一个位图
            Bitmap bmp = new Bitmap(200, 30);

            //绘制涂层
            Graphics g = Graphics.FromImage(bmp);

            //填充背景色为白色


            //画噪线
            for (int i = 0; i < 6; i++)
            {
                int x1 = rnd.Next(100);

                int y1 = rnd.Next(30);

                int x2 = rnd.Next(100);

                int y2 = rnd.Next(30);

                Color clr = color[rnd.Next(color.Length)];

                g.DrawLine(new Pen(clr), x1, y1, x2, y2);
            }

            //画噪点
            for (int i = 0; i < 100; i++)
            {
                int x = rnd.Next(bmp.Width);

                int y = rnd.Next(bmp.Height);

                Color clr = color[rnd.Next(color.Length)];

                bmp.SetPixel(x, y, clr);
            }

            //画验证码
            for (int i = 0; i < chkCode.Length; i++)
            {
                String fnt = font[rnd.Next(font.Length)];

                Font ft = new Font(fnt, 16);

                Color clr = color[rnd.Next(color.Length)];

                g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 20, (float)6);

            }

            //将验证码写入内存流

            MemoryStream ms = new MemoryStream();
            try
            {
                bmp.Save(ms, ImageFormat.Png);

                resp.ClearContent();

                resp.ContentType = "image/Png";

                resp.BinaryWrite(ms.ToArray());

                resp.Flush();

                resp.End();
            }
            finally
            {
                bmp.Dispose();

                g.Dispose();
            }

        }


代码仅供参考 其他的还需调用函数



C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
草甘膦好 | 2013-7-22 23:30:57 | 显示全部楼层
交流交流好啊!楼主真厉害












http://shop103534827.taobao.com
wibc | 2014-5-13 08:21:36 | 显示全部楼层
学习了,谢谢
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则