【蛤蟆】頁面驗證碼程式(C#)
在.NET中新建一個Web專案,新增一個.ASPX頁面,取名VerifyCode.aspx, 轉到其程式碼編輯狀態,將下面的程式碼Copy&Paste過去就可以用了,有什麼意見或問題歡迎提出^@^
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()
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部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-349317/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C#圖片驗證碼C#
- 驗證碼程式
- java抓取有驗證的頁面內容Java
- 輸入資訊頁面js驗證,提示資訊JS
- C# 滑動驗證碼|拼圖驗證|SlideCaptchaC#IDEAPT
- 生成驗證碼程式程式碼
- 記在dcat-admin中,登陸頁面加驗證碼操作
- 頁面常用程式碼整理
- C#驗證郵件的正規表示式的程式碼C#
- 免費404頁面程式碼分享 404錯誤頁面原始碼原始碼
- javascript - 使用者註冊頁面(表單驗證)JavaScript
- 精確驗證身份證號碼程式碼
- C#驗證身份證號C#
- JavaScript頁面跳轉程式碼JavaScript
- C#版本的混合的驗證碼 (轉)C#
- C# 常用驗證C#
- c#驗證照片C#
- C#實現驗證碼識別例項C#
- Asp.net(C#)實現驗證碼功能ASP.NETC#
- 使用 C# 識別英文數字驗證碼C#
- QQ驗證碼識別原始碼(C#/NET1.1)原始碼C#
- 郵箱格式驗證程式碼
- C#取得頁面URL資訊C#
- C#清除頁面快取C#快取
- javascript實現的身份證號碼驗證程式碼JavaScript
- jquery 驗證碼效果程式碼例項jQuery
- 驗證碼原理及驗證
- 程式碼驗證C#執行”檔案開啟關閉操作“耗時C#
- js實現父頁面獲取iframe子頁面內容程式碼JS
- Python 自用程式碼(scrapy多級頁面(三級頁面)爬蟲)Python爬蟲
- 設定頁面認證(轉)
- volatile的特性程式碼驗證
- jQuery郵箱格式驗證程式碼jQuery
- 如何實現 mysql 匯出資料,驗證頁面正確性?MySql
- 驗證碼---js重新整理驗證碼JS
- Ext實現的身份證格式驗證程式碼
- C#實現頁面載入C#
- C# Web Service 身份驗證C#Web