您的位置:首页 > 编程语言 > ASP

ASP.NET之绘制带背景图的图形验证码方法

2012-08-24 16:04 330 查看
本文转载(我增加了“每个字符不同字体”的功能),首先向原作者致敬:http://www.cnblogs.com/ziyiFly/archive/2008/09/04/1283815.html

新建WebSite后,在default.aspx.cs文件中写入如下代码:

protected void Page_Load(object sender, EventArgs e)
{

    string checkCode = CreateRandomCode(4);
    DrawImage(checkCode);
   
}

//产生随机代码
private string CreateRandomCode(int codeCount)
{
    string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
    string[] allCharArray = allChar.Split(',');
    string randomCode = "";
    int temp = -1;

    Random rand = new Random();
    for (int i = 0; i < codeCount; i++)
    {
        if (temp != -1)
        {
            rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
        }
        int t = rand.Next(35);
        if (temp == t)
        {
            return CreateRandomCode(codeCount);
        }
        temp = t;
        randomCode += allCharArray[t];
    }
    return randomCode;
}

private void DrawImage(string checkCode)
{
    if (checkCode == null || checkCode.Trim() == String.Empty)
        return;

    //根据验证码checkCode的长度调整图片的宽度
    Bitmap image = new Bitmap(checkCode.Length * 20 + 10, 35);

    Graphics g = Graphics.FromImage(image);

    try
    {
        //生成随机生成器
        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);
        }

        //定义一个含10种字体的数组
        String[] fontFamily = { "Arial", "Verdana", "Comic Sans MS",
                                  "Book Antiqua","Lucida Sans Unicode",
                                  "Garamond", "Courier New", "Book Antiqua",
                                  "Gungsuh",  "MingLiU"
                              };

        //利用for循环将checkCode的字符用随机的字体书写
        for (int i = 0; i < checkCode.Length; i++)
        {
            Font font = new Font(fontFamily[random.Next(9)], 20, (FontStyle.Bold | FontStyle.Italic));
            LinearGradientBrush brush = new LinearGradientBrush(
                                                new Rectangle(0, 0, image.Width, image.Height),
                                                Color.BlueViolet,
                                                Color.Crimson,
                                                1.2f,
                                                true);

            //每次用不同字体写出一个字符
            g.DrawString(checkCode.Substring(i, 1), font, brush, (2 + (i * 20)), 2);
        }               

        //画图片的前景噪音点
        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);

        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());
    }
    finally
    {
        g.Dispose();
        image.Dispose();
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: