當前位置:名人名言大全網 - 短信平臺 - 如何在ASP.NET中實現驗證碼?

如何在ASP.NET中實現驗證碼?

1.知識點介紹驗證碼其實是隨機產生的壹些數字,在System命名空間下提供的Random類可以用來產生隨機性的非負數字.在ASP.NET中的System.Drawing命名空間中,提供了Bitmap,Graphics類,其中Bitmap類封裝了GDI+位圖,繼承自Image類,用於處理由像素數據定義的圖象.Graphics類封裝了GDI和繪圖面,也就是相當於畫板.下面就使用Random,Bitmap和Graphics類把生成的隨即數繪制到創建的位圖中.並用Image控件顯示創建的位圖.2.示例新建"WebForm1.aspx"頁面,在該頁面中添加壹個Image控件,並設置其ImageUrl屬性為生成的位圖所要存放的路徑.(1)WebForm1.aspx.cs代碼:private void Page_Load(object sender, System.EventArgs e){//建立位圖對象 Bitmap newBitmap=new Bitmap(36,16,System.Drawing.Imaging.PixelFormat.Format32bppArgb);//根據上面創建的位圖對象創建繪圖層 Graphics g=Graphics.FromImage(newBitmap);//以指定的顏色填寵矩形區 g.FillRectangle(new SolidBrush(Color.White),new Rectangle(0,0,36,16));//創建字體對象 Font textFont=new Font("Times new Roman",10);//創建RectangleF結構指定壹個區域 RectangleF rectangle=new RectangleF(0,0,36,16);//創建隨機數對象 Random rd=new Random();