验证码类源代码

以 下是一个验证码类源代码,使用方法:建立一个ValidateImg.aspx,后台代码里写:ValidateImage img = new ValidateImage();验证的地方:ValidateImage.Validate(txtCode.Text.Trim())

 
  
/// <summary>
/// 验证码类

/// </summary>
public class ValidateImage

{

/// <summary>
/// 要显示的文字

/// </summary>
public string Text

{

get { return this .text; }

}

/// <summary>
/// 图片

/// </summary>
public Bitmap Image

{

get { return this .image; }

}

/// <summary>
/// 宽度

/// </summary>
public int Width

{

get { return this .width; }

}

/// <summary>
/// 高度

/// </summary>
public int Height

{

get { return this .height; }

}



private string text;

private int width;

private int height;

private Bitmap image;



private static byte [] randb = new byte [ 4 ];

private static Random rand = new Random();



public ValidateImage()

{

text
= rand.Next( 1000 , 9999 ).ToString();

System.Web.HttpContext.Current.Session[
" CheckCode " ] = text;

this .width = ( int )Math.Ceiling(text.Length * 16.5 );

this .height = 30 ;



GenerateImage();

System.Web.HttpContext.Current.Response.ContentType
= " image/pjpeg " ;

Image.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);



}



public static bool Validate( string input)

{

return System.Web.HttpContext.Current.Session[ " CheckCode " ] != null && System.Web.HttpContext.Current.Session[ " CheckCode " ].ToString().Equals(input);

}



~ ValidateImage()

{

Dispose(
false );

}



public void Dispose()

{

GC.SuppressFinalize(
this );

this .Dispose( true );

}



protected virtual void Dispose( bool disposing)

{

if (disposing)

this .image.Dispose();

}

private FontFamily[] fonts = {

new FontFamily( " Times New Roman " ),

new FontFamily( " Georgia " ),

new FontFamily( " Arial " ),

new FontFamily( " Comic Sans MS " )

};



public int Next( int max)

{

return rand.Next(max);

}



/// <summary>
/// 生成验证码图片



/// </summary>
private void GenerateImage()

{

Bitmap bitmap
= new Bitmap( this .width, this .height, PixelFormat.Format32bppArgb);



Graphics g
= Graphics.FromImage(bitmap);

Rectangle rect
= new Rectangle( 0 , 0 , this .width, this .height);

g.SmoothingMode
= SmoothingMode.AntiAlias;



g.Clear(Color.White);



// int emSize = Next(3) + 15; // (int)((this.width - 20) * 2 / text.Length);

// int emSize = (int)((this.width - 20) * 2 / text.Length);


int emSize = 12 ;

FontFamily family
= fonts[Next(fonts.Length - 1 )];

Font font
= new Font(family, emSize, FontStyle.Bold);



SizeF measured
= new SizeF( 0 , 0 );

SizeF workingSize
= new SizeF( this .width, this .height);

while (emSize > 2 && (measured = g.MeasureString(text, font)).Width > workingSize.Width || measured.Height > workingSize.Height)

{

font.Dispose();

font
= new Font(family, emSize -= 2 );

}



SolidBrush drawBrush
= new SolidBrush(Color.FromArgb(Next( 100 ), Next( 80 ), Next( 80 )));

for ( int x = 0 ; x < 1 ; x ++ )

{

Pen linePen
= new Pen(Color.FromArgb(Next( 150 ), Next( 100 ), Next( 100 )), 1 );

g.DrawLine(linePen,
new PointF( 0.0F + Next( 2 ), 0.0F + Next( this .height)), new PointF( 0.0F + Next( this .width), 0.0F + Next( this .height - 10 )));

}



for ( int x = 0 ; x < this .text.Length; x ++ )

{

drawBrush.Color
= Color.FromArgb(Next( 150 ) + 20 , Next( 150 ) + 20 , Next( 150 ) + 20 );

PointF drawPoint
= new PointF( 0.0F + Next( 5 ) + x * 15 , 2.0F + Next( 4 ));

g.DrawString(
this .text[x].ToString(), font, drawBrush, drawPoint);

}



double distort = rand.Next( 5 , 10 ) * (Next( 10 ) == 1 ? 1 : - 1 );



using (Bitmap copy = (Bitmap)bitmap.Clone())

{

for ( int y = 0 ; y < height; y ++ )

{

for ( int x = 0 ; x < width; x ++ )

{

int newX = ( int )(x + (distort * Math.Sin(Math.PI * y / 84.0 )));

int newY = ( int )(y + (distort * Math.Cos(Math.PI * x / 54.0 )));

if (newX < 0 || newX >= width) newX = 0 ;

if (newY < 0 || newY >= height) newY = 0 ;

bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));

}

}

}





// g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);


font.Dispose();

drawBrush.Dispose();

g.Dispose();



this .image = bitmap;

}

}

转载于:https://www.cnblogs.com/gllgsoft/archive/2008/08/25/1275516.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值