在判斷的時候只要把取得的文本框的值與“Session["valicode"] ”進行判斷是否一致就行了。
//建立位圖對象
Bitmap NewbitMap = new Bitmap(90, 22, PixelFormat.Format32bppArgb);
//根據上面創建的位置對象創建繪圖面
Graphics g = Graphics.FromImage(NewbitMap);
//以指定的顏色填充矩形區
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, 90, 22));
//創建字體對象
Font newfont = new Font("幼圓", 14);
//創建RectangleF結構指定一個區域
RectangleF rectangle = new RectangleF(0, 0, 90, 22);
//創建隨機數
Random Newrd = new Random();
string[] abcd="我,是,沒,高,天,地,聊,材,盆,濁,小,涯,尖,欠,豬,左,腿,刀,吃,渴,棍,皮,影,歇,草,營,救,稅,說,壞,通,病,二,世,期,春,季,弄,刑,事,警,強,窩,菜,干,什,前,都,哭,拉,面,魚,文,鬼,或,熱,狗,蛋,毛,筆,網,件,構,試,社,幫,耐,燒,粘,蘋,鞋,板,裳,花,海,題,a,e,f,r,9,0,k,2,4,7,1,3,q,w,y,u,v,x,p,s,a,d,8,5,t".Split(',');
int RamStr=Newrd.Next(1,100); int Ramstr1 = Newrd.Next(1, 100);
int Ramstr2 = Newrd.Next(1, 100);
int Ramstr3 = Newrd.Next(1, 100);
string ValiNum = abcd[RamStr] + abcd[Ramstr1] + abcd[Ramstr2] + abcd[Ramstr3].ToString();
Session["valicode"] = ValiNum.ToString();
//使用指定的顏色填充上面RectangleF結構指定的矩形區域
g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);
//繪制隨機線條
for(int ii =0;ii<10;ii++)
{
int x1=Newrd.Next(NewbitMap.Height);
int y1=Newrd.Next(NewbitMap.Width);
int x2 = Newrd.Next(NewbitMap.Height);
int y2 = Newrd.Next(NewbitMap.Width);
g.DrawLine(new Pen(Color.Azure), x1, y2, y1, x1);
}
//在上面填充的矩形區域中填充上面生成的隨機數
g.DrawString(ValiNum, newfont, new SolidBrush(Color.Blue), rectangle);
for (int i = 0; i < 50; i++)
{
int x = Newrd.Next(NewbitMap.Width);
int y = Newrd.Next(NewbitMap.Height);
NewbitMap.SetPixel(x, y, Color.FromArgb(Newrd.Next()));
}
MemoryStream ms = new MemoryStream();
NewbitMap.Save(ms, ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());