生成图片验证码
首先需要在網(wǎng)站下面添加窗體。在窗體里面生成包含數(shù)字的圖片。
?Random rd = new Random(); //隨機數(shù)
??????? int i = rd.Next(1000, 9999);
??????? Session["code"] = i; //將生成的隨機數(shù)放到Session中方便對比
??????? Bitmap bitmap = new Bitmap(80, 20);//創(chuàng)建圖片對象
??????? Graphics gh = Graphics.FromImage(bitmap);//將圖片對象創(chuàng)建為繪圖對象
??????? gh.Clear(Color.White);
??????? gh.DrawString(i.ToString(), new Font("宋體", 15), Brushes.Blue, new PointF(0, 0));
??????? gh.DrawLine(Pens.AliceBlue, new Point(0, 10), new Point(80, 10));
??????? gh.DrawLine(Pens.AliceBlue, new Point(0, 5), new Point(80, 10));
??????? bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //用圖片對象,將對象本身保存為服務(wù)端向客戶端瀏覽器的響應(yīng)輸出流,并指定文件類型。
然后在登錄頁面添加一張圖片:
<img src="ImageCode.aspx" border="0" />它的圖片源為剛才創(chuàng)建的繪制圖片的.aspx頁面。這樣一個簡單的圖片驗證碼就實現(xiàn)了。也可以寫個通用類去實現(xiàn)驗證。
下面是一個簡單的驗證碼方法:
?public string CreateValidate(int count)
??? {
??????? string allchar = "1,2,3,4,5,6,7,8,9,0,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[] allchararray = allchar.Split(',');
??????? string randomcode = "";
??????? int temp = -1;
??????? Random rd = new Random();
??????? for (int i = 0; i < count; i++)
??????? {
??????????? if (temp != -1)
??????????? {
??????????????? rd = new Random(i * temp * ((int)DateTime.Now.Ticks));
??????????? }
??????????? int t = rd.Next(35);
??????????? if (temp == t)
??????????? {
??????????????? return CreateValidate(count);
??????????? }
??????????? temp = t;
??????????? randomcode += allchararray[t];
??????? }
??????? Session["code"] = randomcode;
??????? return randomcode;
??? }
?Random rd = new Random(); //隨機數(shù)
??????? int i = rd.Next(1000, 9999);
??????? Session["code"] = i; //將生成的隨機數(shù)放到Session中方便對比
??????? Bitmap bitmap = new Bitmap(80, 20);//創(chuàng)建圖片對象
??????? Graphics gh = Graphics.FromImage(bitmap);//將圖片對象創(chuàng)建為繪圖對象
??????? gh.Clear(Color.White);
??????? gh.DrawString(i.ToString(), new Font("宋體", 15), Brushes.Blue, new PointF(0, 0));
??????? gh.DrawLine(Pens.AliceBlue, new Point(0, 10), new Point(80, 10));
??????? gh.DrawLine(Pens.AliceBlue, new Point(0, 5), new Point(80, 10));
??????? bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //用圖片對象,將對象本身保存為服務(wù)端向客戶端瀏覽器的響應(yīng)輸出流,并指定文件類型。
然后在登錄頁面添加一張圖片:
<img src="ImageCode.aspx" border="0" />它的圖片源為剛才創(chuàng)建的繪制圖片的.aspx頁面。這樣一個簡單的圖片驗證碼就實現(xiàn)了。也可以寫個通用類去實現(xiàn)驗證。
下面是一個簡單的驗證碼方法:
?public string CreateValidate(int count)
??? {
??????? string allchar = "1,2,3,4,5,6,7,8,9,0,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[] allchararray = allchar.Split(',');
??????? string randomcode = "";
??????? int temp = -1;
??????? Random rd = new Random();
??????? for (int i = 0; i < count; i++)
??????? {
??????????? if (temp != -1)
??????????? {
??????????????? rd = new Random(i * temp * ((int)DateTime.Now.Ticks));
??????????? }
??????????? int t = rd.Next(35);
??????????? if (temp == t)
??????????? {
??????????????? return CreateValidate(count);
??????????? }
??????????? temp = t;
??????????? randomcode += allchararray[t];
??????? }
??????? Session["code"] = randomcode;
??????? return randomcode;
??? }
轉(zhuǎn)載于:https://blog.51cto.com/zhjjzhjj/403240
總結(jié)