asp.net登录验证码

上一篇 / 下一篇  2008-01-07 18:57:39

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;
public partial class code : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string t = GenerateCheckCode();
        Session["code"] = t;
        Session.Timeout = 1;
        CreateCheckCodeImage(t);

    }
    public string GenerateCheckCode()
    {
        int number;
        char code;
        string checkCode = string.Empty;//字符串的空值
        Random random = new Random();
        for (int i = 0; i < 4; i++)
        {
            number = random.Next();//非负随机数
            if (number % 2 == 0)
            {
                code = (char)('0' + (char)(number % 10));//生成0--9的数字
            }
            else
            {
                code = (char)('a' + (char)(number % 26));//生成a--q的字母
            }
            checkCode += code.ToString();
        }
        Response.Cookies.Add(new HttpCookie("CheckCode",checkCode));//创建Cookies
        return checkCode;
    }
    public void CreateCheckCodeImage(string checkCode)
    {
        if (checkCode == null || checkCode.Trim() == string.Empty)
            return;
        Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);//定义一个画板
        Graphics g = Graphics.FromImage(image);//在画板上定义绘图的实例
        try
        {
            //生成随机生成器
            Random random = new Random();
            //清空图片背景色
            g.Clear(Color.White);
            //画图片的背景噪点音线
            for (int i = 0; i < 2; 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.Black), x1, y1, x2, y2);
            }
            Font font = new Font("Arial", 12, (FontStyle.Bold));//设置字体
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Gray, Color.CornflowerBlue, 1.2f, true);//设置定义一个矩形
            g.DrawString(checkCode, font, brush, 2, 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);
            MemoryStream ms = new MemoryStream();//内存缓冲区
            image.Save(ms, ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }
        catch
        {
            g.Dispose();
            image.Dispose();
        }
    }
}


TAG:

引用 删除 Guest   /   2008-11-05 20:22:39
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的栏目

日历

« 2008-11-22  
      1
2345678
9101112131415
16171819202122
23242526272829
30      

数据统计

  • 访问量: 429
  • 日志数: 6
  • 书签数: 24
  • 建立时间: 2008-01-07
  • 更新时间: 2008-06-09

RSS订阅

Open Toolbar