随机验证码------WEB开发(登录提交 或避免重复提交数据)常用

来源:互联网 发布:矩阵列向量归一化例题 编辑:程序博客网 时间:2024/05/22 17:49

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.Collections.Generic;

public partial class CodeImage : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        this.CreateCheckCodeImage(Generate4NumRandomString());
    }
  
    private string Generate4NumRandomString()
    {
        int number;
        char code;
        string checkCode = string.Empty;
        Random random = new Random();
        for (int i = 0; i < 4; i++)
        {
            number = random.Next();
            code = (char)('0' + (char)(number % 10));
            checkCode += code.ToString();
        }
        Session["CheckCode"] = checkCode;//用于客户端校验码比较
        return checkCode;
    }

    private void CreateCheckCodeImage(string checkCode)
    {  //将验证码生成图片显示
        if (checkCode == null || checkCode.Trim() == string.Empty)
            return;
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(55, 20);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        try
        {          
            Random random = new Random();
            //清空图片背景色
            g.Clear(Color.White);
            //画图片的背景噪音线
            for (int i = 0; i < 8; 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 System.Drawing.Pen(System.Drawing.Color.FromArgb(random.Next(255), random.Next(255), random.Next(255))
                    ), x1, y1, x2, y2);
            }
            System.Drawing.StringFormat FormatString = new System.Drawing.StringFormat();
            FormatString.Alignment = System.Drawing.StringAlignment.Center;
            FormatString.LineAlignment = System.Drawing.StringAlignment.Center;
            List<FontStyle> FontStyleList = GetFontStyleList;
            for (int i = 0; i < checkCode.Length; i++)
            {
                FontStyle RandomFontStyle = GetRandomFontStyle(FontStyleList);
                System.Drawing.Font font = new System.Drawing.Font("Verdana",RandomFontStyle.FontSize,(System.Drawing.FontStyle.Bold));
                System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(RandomFontStyle.FontColor);
                g.DrawString(checkCode.Substring(i, 1), font, brush, GetCodeRect(i), FormatString);
            }
            //画图片的边框线
            g.DrawRectangle(new System.Drawing.Pen(System.Drawing.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();
        }
    }

    private FontStyle GetRandomFontStyle(List<FontStyle> FontStyleList)
    {
        Random random = new Random();
        int i = random.Next(0, FontStyleList.Count);
        FontStyle RandomFontStyle = FontStyleList[i];
        FontStyleList.RemoveAt(i);
        return RandomFontStyle;
    }
    private List<FontStyle> GetFontStyleList
    {
        get
        {
            List<FontStyle> FontStyleList = new List<FontStyle>(4);
            FontStyleList.Add(new FontStyle(Color.Red, 12));
            FontStyleList.Add(new FontStyle(Color.Green, 12));
            FontStyleList.Add(new FontStyle(Color.Blue, 12));
            FontStyleList.Add(new FontStyle(Color.Black, 12));
            return FontStyleList;
        }
    }

    /// <summary>
    /// 获取单个字符的绘制区域
    /// </summary>
    public Rectangle GetCodeRect(int index)
    {
        // 计算一个字符应该分配有多宽的绘制区域(等分为CodeLength份)
        int subWidth = 55 / 4;
        // 计算该字符左边的位置
        int subLeftPosition = subWidth * index;

        return new Rectangle(subLeftPosition + 1, 1, subWidth, 20);
    }
}

public class FontStyle
{
    private Color _FontColor;
    private int _FontSize;
    public FontStyle(Color FontColor,int FontSize)
    {
        _FontColor = FontColor;
        _FontSize = FontSize;
    }
    public Color FontColor
    {
        get{ return _FontColor; }
        set{ _FontColor = value; }
    }
    public int FontSize
    {
        get{ return _FontSize; }
        set{ _FontSize = value;}
    }
  
}

 

原创粉丝点击