如何在asp.net中动态生成验证码
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);
}
生成以后在前臺頁面里引入這個圖片的地址就可以了!?
總結
以上是生活随笔為你收集整理的如何在asp.net中动态生成验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 象 DEV-Club 那样的彩色校验码
- 下一篇: QQ验证码识别源代码(C#/NET1.1