ASP.NET 圖文 驗證碼
雖然我認為圖片驗證碼沒有什麼用,並且也特別的煩人(每次上移動的網站都要頻頻地輸入驗證碼),但人家要求,也只好弄一個。
產生圖片驗證碼頁面createImg.aspx,驗證頁面Default.aspx。
CreateImg.aspx頁面使用的各個函數如下:
string getRandomValidate(int len) 得到隨機長度為len的字串
drawLine(Graphics gfc,Bitmap img) 在圖片中畫底線
drawPoint(Bitmap img) 在圖片中畫雜點(移動畫的雜點挺好不錯)
getImageValidate(string strValue) 使用getRandomValidate函數返回的字串產生圖片
其具體實現代碼如下所示:
using System.Drawing;
using System.IO;
public partial class createImg : System.Web.UI.Page
{
Random ran = new Random();
protected void Page_Load(object sender, EventArgs e)
{
string str = getRandomValidate(4);
Session["check"] = str; //這一步是為了將驗證碼寫入Session,進行驗證,不能預設,也可一使用cookie
getImageValidate(str);
}
//得到隨機字串,長度自己定義
private string getRandomValidate(int len)
{
int num;
int tem;
string rtuStr = "";
for (int i = 0; i < len; i++)
{
num = ran.Next();
/*
* 這裡可以選擇產生字元和數字組合的驗證碼
*/
tem = num % 10 + '0';//產生數字
//tem = num % 26 + 'A';//產生字元
rtuStr += Convert.ToChar(tem).ToString();
}
return rtuStr;
}
//產生映像
private void getImageValidate(string strValue)
{
//string str = "OO00"; //前兩個為字母O,後兩個為數字0
int width = Convert.ToInt32(strValue.Length * 12); //計算映像寬度
Bitmap img = new Bitmap(width, 23);
Graphics gfc = Graphics.FromImage(img); //產生Graphics對象,進行畫圖
gfc.Clear(Color.White);
drawLine(gfc, img);
//寫驗證碼,需要定義Font
Font font = new Font("arial", 12, FontStyle.Bold);
System.Drawing.Drawing2D.LinearGradientBrush brush =
new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.DarkOrchid, Color.Blue, 1.5f, true);
gfc.DrawString(strValue, font, brush, 3, 2);
drawPoint(img);
gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1);
//將映像添加到頁面
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
//更改Http頭
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
//Dispose
gfc.Dispose();
img.Dispose();
Response.End();
}
private void drawLine(Graphics gfc,Bitmap img)
{
//選擇畫10條線,也可以增加,也可以不要線,只要隨機雜點即可
for (int i = 0; i < 10; i++)
{
int x1 = ran.Next(img.Width);
int y1 = ran.Next(img.Height);
int x2 = ran.Next(img.Width);
int y2 = ran.Next(img.Height);
gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); //注意畫筆一定要淺顏色,否則驗證碼看不清楚
}
}
private void drawPoint(Bitmap img)
{
/*
//選擇畫100個點,可以根據實際情況改變
for (int i = 0; i < 100; i++)
{
int x = ran.Next(img.Width);
int y = ran.Next(img.Height);
img.SetPixel(x,y,Color.FromArgb(ran.Next()));//雜點顏色隨機
}
*/
int col = ran.Next();//在一次的圖片中雜店顏色相同
for (int i = 0; i < 100; i++)
{
int x = ran.Next(img.Width);
int y = ran.Next(img.Height);
img.SetPixel(x, y, Color.FromArgb(col));
}
}
}
如何使用:
建立一個頁面,至少需要一個文字框、image控制項和一個Button控制項。需要注意的是要將image控制項的imageUrl指定為createImg.aspx,如下所示
<asp:Image ID="Image1" runat="server" ImageUrl="~/createImg.aspx" />
其測試就很簡單了,就像平時使用Session一樣,餓,還記得createImg頁面中有一個Session["check"]嗎?該頁面已經註冊了一個會話,那麼在Default頁面中只需簡單的比較以下就可以了。
protected void Page_Load(object sender, EventArgs e)
{
if (Session["check"] == null)
message.Text = "sorry,the image is wrong!";
}
protected void checkButton_Click(object sender, EventArgs e)
{
if (check.Text.ToString() == Session["check"].ToString())
message.Text = "Yes,you pass it";
else
message.Text = "I'm sorry!try again...";
}
測試效果如所示:
物流配貨網http://wlphw.com/ QQ線上:471226865