的設計方式和設計理念和其他的如Asp
在實際項目中運行使用良好
ValidateCode控件的使用方法:
第一步
編譯我提供的原代碼
第二步
工具欄上就會多一個Web控件ValidateCode
第三步
在該控件的GraphicOK事件中獲取
最後一步
在網站的根目錄下
圖片
原代碼如下
using System;
using System
using System
using System
using System
using System
using System
using System
namespace WebValidateCode
{
/// <summary>
/// ValidateCode 的摘要說明
/// 設計者:王海波
/// </summary>
///
public enum GraphicType
{
Jpg =
Gif =
Png =
Bmp =
}
//[ToolboxBitmap(@
public class ValidateCode : System
{
private int pCodelen=
private int pChartWidth=
private int pChartHeight=
private GraphicType pChartType;
private string pAuthenCode;
private string pTempImageURLPath=
private string pAuthenImageFullname;
private string pAuthenImageFullURL;
//生成校驗碼的變量 start
private Bitmap validateImage;
private Graphics g;
//生成校驗碼的變量 End
private TextBox txt=new TextBox();
private System
#region 定義控件事件
public delegate void GraphicCreated(object sender
public event EventHandler GraphicOK; //在校驗圖片生成結束以後觸發
protected virtual void OnGraphicOK(object sender
{
if (GraphicOK != null)
{
//Invokes the delegates
GraphicOK(sender
}
}
#endregion
#region 控件屬性
//生成校驗碼的長度
[Bindable(true)
public int CodeLength
{
get
{
return pCodelen;
}
set
{
pCodelen = value;
}
}
//生成校驗碼的長度
[Bindable(true)
public string TempImageURLPath
{
get
{
return pTempImageURLPath;
}
set
{
pTempImageURLPath = value;
}
}
[Bindable(true)
public GraphicType ChartType
{
get
{
return pChartType;
}
set
{
pChartType = value;
}
}
//生成校驗碼圖片的寬度
public int ChartWidth
{
get
{
return pChartWidth;
}
set
{
pChartWidth = value;
}
}
//生成校驗碼圖片的高度
public int ChartHeight
{
get
{
return pChartHeight;
}
set
{
pChartHeight = value;
}
}
//需要生成的校驗碼
public string AuthenCode
{
get
{
return pAuthenCode;
}
set
{
pAuthenCode = value;
}
}
#endregion
/// <summary>
/// 將此控件呈現給指定的輸出參數
/// </summary>
/// <param name=
protected override void Render(HtmlTextWriter output)
{
System
//TextBox objTxt;
//繪制包含的控件
objImage = (System
//objTxt = (TextBox) Controls[
if(pAuthenCode==null)
pAuthenCode=GetValidateCode();
OnGraphicOK(this
GetRandomImage(pAuthenCode);
objImage
objImage
}
/// <summary>
/// 給控件添加子控件
/// </summary>
protected override void CreateChildControls( )
{
//Controls
Controls
//Controls
}
/// <summary>
/// 控件Load時候屬性的初始化
/// </summary>
/// <param name=
protected override void OnLoad(System
{
EraseOldGraphic(); //刪除過期的圖片
}
/// <summary>
/// 生成隨機的
/// </summary>
private void MakeRandomFileName()
{
string strRandName=DateTime
pAuthenImageFullname=this
pAuthenImageFullURL=TempImageURLPath+
}
private void GetRandomImage(string strValidateCode)
{
//生成隨即圖片的全名
MakeRandomFileName();
validateImage = new Bitmap(pChartWidth
g = Graphics
g
//g
for(int i=
{
Random r = new Random();
PointF startPos=new PointF(r
g
}
//g
switch(pChartType)
{
case GraphicType
validateImage
break;
case GraphicType
validateImage
break;
case GraphicType
validateImage
break;
case GraphicType
validateImage
break;
default:
validateImage
break;
}
validateImage
g
}
/// <summary>
/// 動態從數字和字母組成的元素中動態選擇生成校驗碼
/// </summary>
private string GetValidateCode()
From:http://tw.wingwit.com/Article/program/net/201311/13574.html