图形验证码

2/11/2016来源:ASP.NET技巧人气:2120

新建一个.cs文件用来写生成图形验证码的方法,返回一个MemoryStream的参数。在Default.aspx中接收输出就可以了。

1.下面是.cs的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

/// <summary>
///BitmapImage 的摘要说明
/// </summary>
public class BitmapImage
{

    public static MemoryStream ImageCheck()
    {
        Random random = new Random();
        //随机产生4位随机数
        string checkCode = random.Next(1000, 9999).ToString();
        //Bitmap是用来指定初始化时文本的大小
        Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 20.5)), 22);
        Graphics g = Graphics.FromImage(image);
        try
        {

            g.Clear(Color.White);//清空图片背景色
            //画图片的背景噪音线这里是4条线
            for (int i = 0; i < 4; 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, x2, y1, y2);
            }
            Font font = new Font("Arial", 12, FontStyle.Bold);
            //设置颜色,起始位置等参数
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 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);
            return ms;
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }

    }
}

2.以下是Default.aspx.cs中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    PRotected void Page_Load(object sender, EventArgs e)
    {
        MemoryStream ms= BitmapImage.ImageCheck();
        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());
    }
}

 3.截图