如何在asp.net中動態產生驗證碼)

來源:互聯網
上載者:User

現在越來越多的網站喜歡搞個驗證碼出來,而且各個語言基本上都能做到,今天我來一個C#寫的!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

//建立位元影像對象

public void randomNumber()

{
   Bitmap newBitmap = new Bitmap(36,16,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();
   //取得隨機數
   int valationNo = 1000 + rd.Next(8999);
   //使用指定的顏色填充上面RectangleF結構指定的矩形地區
   g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);
   //在上面填充的矩形地區中填充上面產生的隨機數
   g.DrawString(valationNo.ToString(), textFont, new SolidBrush(Color.Blue), rectangle);
   //把建立的位元影像儲存到指定的路徑
   newBitmap.Save(Server.MapPath("img")+"\\Img.gif", ImageFormat.Gif);

}

產生以後在前台頁面裡引入這個圖片的地址就可以了!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.