验证码(二)

这是一个方法  只需要在前面调用就可以额! 

 

public static void DisplayPic()
    {
        string msg = GetRandomString();
        HttpContext.Current.Session["code"] = msg; ;
        Bitmap image = new Bitmap(50, 20);
        Graphics g = Graphics.FromImage(image);
        g.Clear(Color.White);

        Random random = new Random();

        for (int i = 0; i < 20; i++)
        {
            int x1 = random.Next(image.Width);
            int y1 = random.Next(image.Height);
            int x2 = random.Next(image.Width);
            int y2 = random.Next(image.Height);

            g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
        }
        Font font = new Font("宋体", 12);
        SolidBrush brush = new SolidBrush(Color.Blue);
        PointF point = new PointF(2, 2);
        g.DrawString(msg, font, brush, point);

        g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
        MemoryStream ms = new MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        HttpContext.Current.Response.BinaryWrite(ms.ToArray());
    }

 

 

 

 protected void Page_Load(object sender, EventArgs e)
    {
        Common.DisplayPic();

    }

posted @ 2012-12-03 09:42  盛开的雨季  阅读(130)  评论(0编辑  收藏  举报