c# asp.net实现验证码

建立ValidateImg.aspx文件,里面代码如下:

  
  
<% @ Page Language = " c# " %>

<% @ Import Namespace = " System.Web " %>
<% @ Import Namespace = " System.Drawing " %>
<% @ Import Namespace = " System.Drawing.Imaging " %>

< script runat = " server " language = " C# " >
void Page_Load(Object sender, EventArgs e)
{
int charsNo = 4 ;
int fontSize = 12 ;
int bgWidth = 60 ;
int bgHeight = 20 ;
float x = (bgWidth - (charsNo * (fontSize + 0.5F ))) / 2 ; // TODO: optimize
float y = (bgHeight - (fontSize * 1.7F )) / 2 ; // TODO: optimize

// Generate the text
//
// string genText = Globals.CreateTemporaryPassword(charsNo).ToUpper();
string genText = CreateTemporaryPassword(charsNo).ToUpper();

// Add the generate text to a session variable
//
Session.Add( " ValidateNumber " , genText);

// Create the memory map
//
Bitmap raster;

// Select an memory image from file of 290x80 px
// in the current dir, NoSpamBgImgs folder named bg_X.jpg
//
Graphics graphicObj;
string bgFilePath = Server.MapPath( @" ./AntiSpamBgImgs/bg_ " + new Random().Next( 1 ) + " .jpg " );
System.Drawing.Image imgObj
= System.Drawing.Image.FromFile(bgFilePath);

// Creating the raster image & graphic objects
//
raster = new Bitmap(imgObj, bgWidth, bgHeight);
graphicObj
= Graphics.FromImage(raster);

// Creating an array for most readable yet cryptic fonts for OCR's
// This is entirely up to developer's discretion
// CAPTCHA recomandation
//
String[] crypticFonts = new String[ 13 ];
crypticFonts[
0 ] = " Arial " ;
crypticFonts[
1 ] = " Verdana " ;
crypticFonts[
2 ] = " Comic Sans MS " ;
crypticFonts[
3 ] = " Impact " ;
crypticFonts[
4 ] = " Haettenschweiler " ;
crypticFonts[
5 ] = " Lucida Sans Unicode " ;
crypticFonts[
6 ] = " Garamond " ;
crypticFonts[
7 ] = " Courier New " ;
crypticFonts[
8 ] = " Book Antiqua " ;
crypticFonts[
9 ] = " Arial Narrow " ;
crypticFonts[
10 ] = " Fixedsys " ;
crypticFonts[
11 ] = " 宋体 " ;
crypticFonts[
12 ] = " 仿体 " ;

// Instantiate object of brush with black color
//
Random r = new Random();
SolidBrush brushes
= new SolidBrush(Color.FromArgb(r.Next( 150 ), r.Next( 150 ), r.Next( 150 )));

// Loop to write the characters on image with different fonts
// CAPTCHA method
//
for ( int a = 0 ; a < genText.Length; a ++ )
{
Font fontObj
= new Font(crypticFonts[r.Next(crypticFonts.Length)], fontSize);

graphicObj.DrawString(genText.Substring(a,
1 ), fontObj, brushes, x + (a * fontSize), y);
}

// Flush again
//
graphicObj.Flush();

// Setting the response header content MIME type
//
Response.ContentType = " image/gif " ;

// Saving the file to output stream to be displayed in browser
//
raster.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

// Flushing to the Response
//
Response.Flush();
}

public static String CreateTemporaryPassword( int length)
{

string str = "" ;
Random r
= new Random();
for ( int i = 0 ; i < length; i ++ )
{
str
+= r.Next( 0 , 9 ).ToString();
}

return str;
}

</ script >
 

 


 

使用方法:
 

 <img src="/ValidateImg.aspx" align="absmiddle" id="code" width="66" height="30" alt="验证码" /><a href="javascript:document.getElementById('code').src='/ValidateImg.aspx?'+Math.random();void(0);">看不清楚,换一个验证码</a>

后台:Session["ValidateNumber"] == input

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值