首页 > Linux操作系统 > Linux操作系统 > 【蛤蟆】页面验证码程序(C#)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Text;
namespace VerifyCode

{
/**////
/// 页面验证码程序
/// 使用:在页面中加入HTML代码
///
public class VerifyCode : System.Web.UI.Page
{

Private Members#region Private Members
static string[] FontItems = new string[]
{ "Arial",
"Helvetica",
"Geneva",
"sans-serif",
"Verdana"
};

static Brush[] BrushItems = new Brush[]
{ Brushes.OliveDrab,
Brushes.ForestGreen,
Brushes.DarkCyan,
Brushes.LightSlateGray,
Brushes.RoyalBlue,
Brushes.SlateBlue,
Brushes.DarkViolet,
Brushes.MediumVioletRed,
Brushes.IndianRed,
Brushes.Firebrick,
Brushes.Chocolate,
Brushes.Peru,
Brushes.Goldenrod
};

static string[] BrushName = new string[]
{ "OliveDrab",
"ForestGreen",
"DarkCyan",
"LightSlateGray",
"RoyalBlue",
"SlateBlue",
"DarkViolet",
"MediumVioletRed",
"IndianRed",
"Firebrick",
"Chocolate",
"Peru",
"Goldenrod"
};
private static Color BackColor = Color.White;
private static Pen BorderColor = Pens.DarkGray;
private static int Width = 52;
private static int Height = 21;
private Random _random;
private string _code;
private int _brushNameIndex;
#endregion // end of Private Members


Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/**////
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion


PageLoad Event#region PageLoad Event
public void Page_Load(object sender, System.EventArgs e)
{
//
// TODO : initialize
//
this._random = new Random();
this._code = GetRandomCode();
//
// TODO : use Session["code"] save the VerifyCode
//
Session["code"] = this._code;
//
// TODO : output Image
//
this.SetPageNoCache();
this.OnPaint();
}
#endregion // end of PageLoad Event


Helper Methods#region Helper Methods
/**////
/// 设置页面不被缓存
///
private void SetPageNoCache()
{
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma","No-Cache");
}

/**////
/// 取得一个 4 位的随机码
///
///
private string GetRandomCode()
{
return Guid.NewGuid().ToString().Substring(0, 4);
}

/**////
/// 随机取一个字体
///
///
private Font GetFont()
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-349317/ ,如需转载,请注明出处,否则将追究法律责任。