Color.Gainsboro 0 0 0 image.Width - 1 image.Height - 1g.Dispose//产生波形(Add By 51aspx.com)image TwistImageimage true 4 4return imageendregionregion 将创建好的图片输出到页面public void CreateImageOnPagestring code HttpContext contextResponse.BufferOutput true //特别注意Response.Cache.SetExpiresDateTime.Now.AddMilliseconds-1//特别注意Response.Cache.SetCacheabilityHttpCacheability.NoCache//特别注意Response.AppendHeaderPragma No-Cache //特别注意MemoryStream ms new MemoryStreamBitmap image this.CreateImageCodecodeimage.Savems ImageFormat.JpegResponse.ClearContentResponse.ContentType image/JPEGResponse.BinaryWritems.ToArrayResponse.Endms.Closems nullimage.Disposeimage nullendregionregion 生成随机字符码public string CreateVerifyCodeint codeLenif codeLen 0codeLen Lengthstring arr CodeSerial.Splitstring code int randValue -1Random rand new RandomuncheckedintDateTime.Now.Ticksfor int i 0 i codeLen irandValue rand.Next0 arr.Length - 1code arrrandValuereturn codepublic string CreateVerifyCodereturn CreateVerifyCode0endregionregion 另一种验证码样式 GenerateVerifyImageint length////// 将创建好的图片输出到页面///public void GenerateVerifyImageint nLenstring validateCode //生成的验证码int nBmpWidth GetImagewidthnLenint nBmpHeight GetImageHeightSystem.Drawing.Bitmap bmp new System.Drawing.BitmapnBmpWidth nBmpHeight//对图像进行弯曲TwistImagebmp true 12 2// 1. 生成随机背景颜色int nRed nGreen nBlue // 背景的三元色System.Random rd new RandomintSystem.DateTime.Now.TicksnRed rd.Next255 128 128nGreen rd.Next255 128 128nBlue rd.Next255 128 128// 2. 填充位图背景System.Drawing.Graphics graph System.Drawing.Graphics.FromImagebmpgraph.FillRectanglenew SolidBrushSystem.Drawing.Color.FromArgbnRed nGreen nBlue00 nBmpWidth nBmpHeight// 3. 绘制干扰线条,采用比背景略深一些的颜色int nLines 3System.Drawing.Pen pen new System.Drawing.PenSystem.Drawing.Color.FromArgbnRed -17 nGreen - 17 nBlue - 17 2for int a 0 a nLines aint x1 rd.Next nBmpWidthint y1 rd.Next nBmpHeightint x2 rd.Next nBmpWidthint y2 rd.Next nBmpHeightgraph.DrawLinepen x1 y1 x2 y2// 采用的字符集,可以随即拓展,并可以控制字符出现的几率string strCode 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ// 4. 循环取得字符,并绘制for int i 0 i nLen iint x i 13 rd.Next3int y rd.Next4 1// 确定字体System.Drawing.Font font new System.Drawing.FontCourier New//文字字体类型12 rd.Next 4//文字字体大小System.Drawing.FontStyle.Bold//文字字体样式char c strCoderd.NextstrCode.Length // 随机获取字符validateCode c.ToString// 绘制字符graph.DrawStringc.ToStringfontnew SolidBrushSystem.Drawing.Color.FromArgbnRed - 60 y 3 nGreen - 60 y 3 nBlue -40 y 3xySessionValidateCode validateCode//对图像进行弯曲TwistImagebmp true 4 4Response.BufferOutput true //特别注意Response.Cache.SetExpiresDateTime.Now.AddMilliseconds-1//特别注意Response.Cache.SetCacheabilityHttpCacheability.NoCache//特别注意Response.AppendHeaderPragma No-Cache //特别注意// 5. 输出字节流MemoryStream bstream new MemoryStreambmp.Savebstream ImageFormat.JpegResponse.ClearContentResponse.ContentType image/JPEGResponse.BinaryWritebstream.ToArrayResponse.Endbstream.Closebstream nullbmp.Disposebmp nullgraph.Dispose//////得到验证码图片的宽度//////验证码的长度///public static int GetImagewidthint validateNumLengthreturn int13 validateNumLength 5//////得到验证码的高度//////public static int GetImageHeightreturn 25endregion
上一篇:
使用强类型DataSet简化ASP_NET程序开发
下一篇:
群体性突发事件论文:“边界冲突”:农村群体性事件的县域分析