using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
//[ToolboxBitmap(@"D:\DotnetApp\ValidateCode\ValidateCode.bmp") ] //設定控制項在工具箱上的表徵圖
public class ValidateCode : System.Web.UI.WebControls.WebControl ,INamingContainer
{
private int pCodelen=5;
private int pChartWidth=100;
private int pChartHeight=20;
//產生校正碼的變數 start
private Bitmap validateImage;
private Graphics g;
//產生校正碼的變數 End
private TextBox txt=new TextBox();
private System.Web.UI.WebControls.Image img= new System.Web.UI.WebControls.Image();
#region 定義控制項事件
public delegate void GraphicCreated(object sender, EventArgs e);
public event EventHandler GraphicOK; //在校正圖片產生結束以後觸發
protected virtual void OnGraphicOK(object sender, EventArgs e)
{
if (GraphicOK != null)
{
//Invokes the delegates.
GraphicOK(sender, e);
}
}
#endregion
#region 控制項屬性
//產生校正碼的長度
[Bindable(true),Browsable(true),Category("Appearance"),DefaultValue(true),Description("需要驗證碼的長度,建議在5~8位之間!")]
public int CodeLength
{
get
{
return pCodelen;
}
set
{
pCodelen = value;
}
}
//產生校正碼的長度
[Bindable(true),Browsable(true),Category("Appearance"),DefaultValue(true),Description("產生驗證碼圖片的臨時存放路徑,要求必須是網站下的虛擬目錄!")]
public string TempImageURLPath
{
get
{
return pTempImageURLPath;
}
set
{
pTempImageURLPath = value;
}
}
[Bindable(true),Browsable(true),Category("Appearance"),DefaultValue(GraphicType.Jpg),Description("選擇產生校正圖檔案的類型(Jpg;Gif;Png;Bmp)!")]
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;
}
validateImage = new Bitmap(pChartWidth, pChartHeight, PixelFormat.Format24bppRgb); // .Format24bppRgb);
g = Graphics.FromImage(validateImage);
g.Clear(Color.LightGray) ;
//g.DrawString(strValidateCode , new Font("宋體",16,FontStyle.Bold),new SolidBrush(Color.DarkRed),new PointF(2,2));
for(int i=0;i<strValidateCode.Length;i++)
{
Random r = new Random();
PointF startPos=new PointF(r.Next(3,6)+(r.Next(12,14)*i ),r.Next(-1,2) );
g.DrawString(strValidateCode.Substring(i,1) , new Font("宋體",14,FontStyle.Italic),new SolidBrush(Color.Blue),startPos);
}
//g.FillRectangle(new LinearGradientBrush(new Point(0,0), new Point(120,30), Color.FromArgb(0,0,0,0),Color.FromArgb(255,255,255,255)),0,0,120,30);
switch(pChartType)
{
case GraphicType.Jpg:
/// <summary>
/// 動態從數字和字母組成的元素中動態選擇產生校正碼
/// </summary>
private string GetValidateCode()
{
char[] s = new char[]{'0','1', '2','3','4','5','6','7','8','9','a'
,'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q'
,'r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G'
,'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W'
,'X','Y','Z'};
string num = "";
Random r = new Random();
//根據使用者需要的長度來定義驗證碼的位元
for(int i = 0; i < CodeLength; i++)
{
num += s[r.Next(0, s.Length)].ToString();
}