標籤:asp.net日記
先要建立validate aspx驗證頁面。然後產生驗證碼控制項imagebutton,再產生picture表單。
代碼如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.IO;
public partial class Picture : System.Web.UI.Page
{
Random ran = new Random();
protected void Page_Load(object sender, EventArgs e)
{
string str = getRandomValidate(4);
Session["CheckCode"] = str;//這一部是Wie了驗證碼寫入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();public partial class Picture : System.Web.UI.Page
{
Random ran = new Random();
protected void Page_Load(object sender, EventArgs e)
{
string str = getRandomValidate(4);
Session["CheckCode"] = str;//這一部是Wie了驗證碼寫入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();public partial class Picture : System.Web.UI.Page
{
Random ran = new Random();
protected void Page_Load(object sender, EventArgs e)
{
string str = getRandomValidate(4);
Session["CheckCode"] = str;//這一部是Wie了驗證碼寫入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);
gfc.Clear(Color.White);
drawLine(gfc,img);
//寫驗證碼,要定義Font
Font font = new Font("arial",12,FontStyle.Bold);
//Font font = new Font("宋體",12,FontStyle.Bold|FontStyle.Italic);
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)
//{
//}
private void drawPoint(Bitmap img)
{
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));
}
}
}
這就是全部使用驗證視窗原始碼,
asp.net如何?產生驗證碼的登入介面