现在的位置: 首页 > 综合 > 正文

很不完备的验证码控件

2013年10月27日 ⁄ 综合 ⁄ 共 3984字 ⁄ 字号 评论关闭
今天要用到验证码,找了几个控件,都写的很完备的说,安全加密一应俱全。。。。
还是改一个简单点的吧,就用COOKIE实现,而加密那块就先搁着吧~呵呵
以后再加~

要另外加上的命名空间:
using System.Drawing;

using System.Drawing.Drawing2D;

using System.IO;

using System.Drawing.Imaging;

using System.Drawing;

 

public partial class DiyControls_ValidationCode : System.Web.UI.UserControl

{

    protected void Page_Load(object sender, EventArgs e)

    {

        CreateValidationImage(GenerateValidationCode());

    }

/// <summary>

/// 生成验证码

/// </summary>

/// <returns>返回生成的验证码</returns>

    private string GenerateValidationCode()

    {

        int number;

        string strCode = String.Empty;

        //随机种子的生成,没什么好说的。

        Random randomSeed = new Random();

 

        for (int i = 0; i < 4; i++)

        {

            number = randomSeed.Next();

 

            //这个算法真的巧妙阿,我要认真学习才行!

            //大家都知道在ASCII码中,0-9A-Z分别是48-5765-90

            //而这里用36求余,呵呵,36=0-9的数)+A-Z的数)=10+26,这下明白了吧,呵呵。

            number = number % 36;

 

            if (number < 10)

            {

                number += 48;

            }

            else

            {

                number += 55;

            }

            strCode += ((char)number).ToString();

        }

        //这里用了Cookie来保存验证码,那么当你调用的时候,记得要验证一下浏览器支不支持哦,不然是用不了的阿

        Response.Cookies.Add( new HttpCookie ("XiaoLing_ValidationCode", strCode));

        return strCode;

    }

    /// <summary>

    /// 生成验证码的图片

    /// </summary>

    /// <param name="validationCode">图片中的验证码</param>

    private void CreateValidationImage(string validationCode)

    {

        if (validationCode == null||validationCode.Trim()==String.Empty)

        {

            return;

        }

 

        Bitmap validationImage = new Bitmap((int)Math.Ceiling((double)(validationCode.Length * 15)), 20);

 

        Graphics g = Graphics.FromImage(validationImage);

 

        try

        {

            Random randomSeed = new Random();

 

            //把位图的底色清空,这里把它设为白色

            g.Clear(Color.White);

 

 

            //加上N条干扰线,感觉十来廿条就够了吧

            for (int i = 0; i < 20; i++)

            {

 

                int x1 = randomSeed.Next(validationImage.Width);

                int x2 = randomSeed.Next(validationImage.Width);

                int y1 = randomSeed.Next(validationImage.Height);

                int y2 = randomSeed.Next(validationImage.Height);

 

                int colorNumber = randomSeed.Next(3);

 

                Color nColor = new Color();

 

                switch (colorNumber)

                {

                    case 0:

                        nColor = Color.Silver;

                        break;

                    case 1:

                        nColor = Color.SpringGreen;

                        break;

                    case 2:

                        nColor = Color.Tomato;

                        break;

                    case 3:

                        nColor = Color.Violet;

                        break;

                    default:

                        nColor = Color.SeaGreen;

                        break;

 

 

                }

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

 

            }

            Font nFont = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));

 

            LinearGradientBrush nBrush = new LinearGradientBrush(new Rectangle(0, 0, validationImage.Width, validationImage.Height), Color.Blue, Color.Purple, 1.2f, true);

 

            //在图片中写上验证码

            g.DrawString(validationCode, nFont, nBrush, 2, 2);

 

            //再在上面加N个干扰点

            for (int i = 0; i < 100; i++)

            {

                int x = randomSeed.Next(validationImage.Width);

                int y = randomSeed.Next(validationImage.Height);

 

                validationImage.SetPixel(x, y, Color.FromArgb(randomSeed.Next()));

            }

 

            //再画个边框

            g.DrawRectangle(new Pen(Color.SaddleBrown), 0, 0, validationImage.Width - 1, validationImage.Height - 1);

 

            //弄完了当然要把图片输出了,输出完就给他销毁咯

            using (MemoryStream ms = new MemoryStream())

            {

                validationImage.Save(ms, ImageFormat.Png);

                Response.ClearContent();

                Response.ContentType = "image/Png";

                Response.BinaryWrite(ms.ToArray());

            }

        }

        catch (Exception ex)

        {

            throw new Exception("这样都有错,真奇怪~", ex);

        }

        finally

        {

            g.Dispose();

            validationImage.Dispose();

        }

    }

}

 

抱歉!评论已关闭.