验证码的生成

C# CODE
 
 
public partial class Include_VisualVerification : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { CS.Common.VisualImage vi = new CS.Common.VisualImage(); Response.ClearContent(); Response.ContentType = " image/JPEG " ; if (Session[ " VisualVerification " ] == null ) Session.Add( " VisualVerification " , string .Empty); string vcode = ( string )Session[ " VisualVerification " ]; Response.BinaryWrite(vi.GeneralImage( 40 , 4 , out vcode)); Session[ " VisualVerification " ] = vcode; } }

C# CODE
 
 
#region VisualImage 验证码 public class VisualImage { /// <summary> /// 产生扭曲效果的随机文本图象JPEG /// </summary> /// <param name="length"> 随机文本长度 </param> /// <param name="strRnd"> 随机产生的字符串 </param> /// <returns> JPEG格式的文件流 </returns> public byte [] GeneralImage( int fontSize, int length, out string strRnd) { int bmpWidth = fontSize * length + 10 ; int bmpHeigh = fontSize + 30 ; Bitmap bmp = new Bitmap(bmpWidth, bmpHeigh); Font font = new Font( " Courier New " , fontSize, FontStyle.Bold); Graphics g = Graphics.FromImage(bmp); g.FillRectangle(Brushes.White, 0 , 0 , bmpWidth, bmpHeigh); // 填充背景色, 否则默认为 black strRnd = GeneralString(length); g.DrawString(strRnd, font, Brushes.Black, 10 , 10 ); Random random = new Random(); bmp = TwistImage(bmp, true , random.Next( 10 , 23 ), random.Next( 1 , 8 )); // 22.33, 0.03); System.IO.MemoryStream bstream = new System.IO.MemoryStream(); bmp.Save(bstream, System.Drawing.Imaging.ImageFormat.Jpeg); bmp.Dispose(); g.Dispose(); font.Dispose(); byte [] byteReturn = bstream.ToArray(); bstream.Close(); return byteReturn; } public static string GeneralString( int length) { System.Text.StringBuilder sbRnd = new System.Text.StringBuilder(); Random random = new Random(); for ( int i = 0 ; i < length; ++ i) { sbRnd.Append(Convert.ToChar(random.Next( 97 , 122 ))); } return sbRnd.ToString(); } /// <summary> /// 正弦曲线Wave扭曲图片 /// </summary> /// <param name="srcBmp"></param> /// <param name="bXDir"></param> /// <param name="nMultValue"> 波形的幅度倍数 </param> /// <param name="dPhase"> 波形的起始相位,取值区间[0-2*PI) </param> /// <returns></returns> private Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase) { double PI = 6.283185307179586476925286766559 ; Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height); // 将位图背景填充为白色 Graphics graph = Graphics.FromImage(destBmp); graph.FillRectangle( new SolidBrush(Color.White), 0 , 0 , destBmp.Width, destBmp.Height); graph.Dispose(); double dBaseAxisLen = bXDir ? ( double )destBmp.Height : ( double )destBmp.Width; for ( int i = 0 ; i < destBmp.Width; i ++ ) { for ( int j = 0 ; j < destBmp.Height; j ++ ) { double dx = 0 ; dx = bXDir ? (PI * ( double )j) / dBaseAxisLen : (PI * ( double )i) / dBaseAxisLen; dx += dPhase; double dy = Math.Sin(dx); // 取得当前点的颜色 int nOldX = 0 , nOldY = 0 ; nOldX = bXDir ? i + ( int )(dy * dMultValue) : i; nOldY = bXDir ? j : j + ( int )(dy * dMultValue); Color color = srcBmp.GetPixel(i, j); if (nOldX >= 0 && nOldX < destBmp.Width && nOldY >= 0 && nOldY < destBmp.Height) { destBmp.SetPixel(nOldX, nOldY, color); } } } srcBmp.Dispose(); return destBmp; } } #endregion
 转
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值