Asp.net(C#)實(shí)現(xiàn)驗(yàn)證碼功能代碼
更新時(shí)間:2008年10月22日 14:36:40 作者:
asp.net驗(yàn)證碼的實(shí)現(xiàn)方法
新建一個(gè)專門用來(lái)創(chuàng)建驗(yàn)證碼圖片的頁(yè)面ValidateCode.aspx
它的后臺(tái)cs文件代碼如下:
PageLoad
private void Page_Load(object sender, System.EventArgs e)
{
string checkCode = CreateRandomCode(4);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}
其中CreateRandomCode是自定義的函數(shù),參數(shù)代表驗(yàn)證碼位數(shù)
private string CreateRandomCode(int codeCount)
{
string allChar = "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,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1; Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
CreateImage也是一個(gè)自定義的函數(shù),用于生成圖
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 11.5);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.White);
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.Blue);
g.DrawString(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0);
Random rand = new Random();
for (int i=0;i<5;i++)
{
int y = rand.Next(image.Height);
g.DrawLine(blackPen,0,y,image.Width,y);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.Blue);
這兩種方法都可以改變生成圖片的背景顏色。下面那個(gè)for循環(huán)用來(lái)生成一些隨機(jī)的水平線
在需要用到驗(yàn)證碼的頁(yè)面添加一個(gè)<asp:Image>控件即可,但是要把ImageUrl指向生成驗(yàn)證碼的頁(yè)面
<asp:Image Runat="server" ID="ImageCheck" ImageUrl="ValidateCode.aspx"></asp:Image>
它的后臺(tái)cs文件代碼如下:
PageLoad
復(fù)制代碼 代碼如下:
private void Page_Load(object sender, System.EventArgs e)
{
string checkCode = CreateRandomCode(4);
Session["CheckCode"] = checkCode;
CreateImage(checkCode);
}
其中CreateRandomCode是自定義的函數(shù),參數(shù)代表驗(yàn)證碼位數(shù)
復(fù)制代碼 代碼如下:
private string CreateRandomCode(int codeCount)
{
string allChar = "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,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1; Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
CreateImage也是一個(gè)自定義的函數(shù),用于生成圖
復(fù)制代碼 代碼如下:
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 11.5);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.White);
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.Blue);
g.DrawString(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0);
Random rand = new Random();
for (int i=0;i<5;i++)
{
int y = rand.Next(image.Height);
g.DrawLine(blackPen,0,y,image.Width,y);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.Blue);
在需要用到驗(yàn)證碼的頁(yè)面添加一個(gè)<asp:Image>控件即可,但是要把ImageUrl指向生成驗(yàn)證碼的頁(yè)面
復(fù)制代碼 代碼如下:
<asp:Image Runat="server" ID="ImageCheck" ImageUrl="ValidateCode.aspx"></asp:Image>
相關(guān)文章
Asp.Net平臺(tái)下的圖片在線裁剪功能的實(shí)現(xiàn)代碼(源碼打包)
最近項(xiàng)目中有個(gè)圖片在線裁剪功能,本人查找資料,方法如下:前臺(tái)展現(xiàn)用jquery.Jcrop實(shí)現(xiàn),后臺(tái)使用 System.Drawing.Image類來(lái)進(jìn)行裁剪2011-10-10.NET Core使用EF生成數(shù)據(jù)庫(kù)出錯(cuò)的解決方法
這篇文章介紹了.NET Core使用EF生成數(shù)據(jù)庫(kù)出錯(cuò)的解決方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01asp.net MVC實(shí)現(xiàn)簡(jiǎn)單的上傳功能
MVC中上傳變得越來(lái)越容易,可是對(duì)于新手這個(gè)也還是不知道如何實(shí)現(xiàn),以下方式實(shí)現(xiàn)MVC的上傳功能,以下2種方法都是可以實(shí)現(xiàn)的,其中的代碼參考了藍(lán)色小鋪和重典的文章。2009-11-11在.net中用CheckBoxList實(shí)現(xiàn)單選
用CheckBoxList實(shí)現(xiàn)單選的原因是我覺(jué)得CheckBoxList控件頁(yè)面展示效果要好看一些,需要的朋友可以參考下2014-02-02asp.net 文件上傳 實(shí)時(shí)進(jìn)度
在swfupload的基礎(chǔ)上增加一些個(gè)性化東西.附圖2張.2009-11-11asp.net 簡(jiǎn)單驗(yàn)證碼驗(yàn)證實(shí)現(xiàn)代碼
網(wǎng)站開(kāi)發(fā)一般登錄注冊(cè)的時(shí)候都要用到了 所以寫下來(lái)給大家參考參考2009-09-09記錄asp.net網(wǎng)站是什么原因?qū)е峦V惯\(yùn)行的代碼
這篇文章主要介紹了記錄asp.net網(wǎng)站是什么原因?qū)е峦V惯\(yùn)行的具體實(shí)現(xiàn)2014-03-03