ASP.NET2.0中文驗證碼的實現代碼

來源:互聯網
上載者:User
  這裡只是一個簡單的例子,字型也只是單一的一種,如果想有更好的效果那就把每一個字都設一種單獨的字型,這樣一來效果會好很多。希望對各位有所協助

  在判斷的時候只要把取得的文字框的值與“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());

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.