1、 v.SetRandomAngle = 4; /文字大小 v.SetFontSize = 15; /背景色 /v.SetBackgroundColor /前景噪点数量 /v.SetForeNoisePointCount = 3; /v.SetFontColor =Color.Red; /.还有更多设置不介绍了 var questionList = new Dictionary() 1+1=?,2,喜羊羊主角叫什么名字?喜羊羊 ,【我爱你】中间的那个字?爱 ; var questionItem = v.GetQuestion();/不赋值为随机验证码 例如: 1*2=? 这种 /指定验证文本 /
2、v.SetVerifyCodeText v.SetVerifyCodeText = questionItem.Key; SessionVerifyCode = questionItem.Value; /输出图片 v.OutputImage(System.Web.HttpContext.Current.Response); 2、前台设用action或者pageload页面地址,js给src更换url添加随机参数 达到刷新验证码的功能 3、完整封装类using System;using System.Collections.Generic;using System.Drawing;using Sy
3、stem.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;using System.Linq;using System.Text;using System.Web;namespace Idea.Models / / 验证码类/summary public class VerifyCodeSugar private Random objRandom = new Random(); #region setting / /验证码长度 public int SetLength = 4; / 验证码字符串 public str
4、ing SetVerifyCodeText get; set; / 是否加入小写字母 public bool SetAddLowerLetter = false; / 是否加入大写字母 public bool SetAddUpperLetter = false; / 字体大小 public int SetFontSize = 18; / /字体颜色 public Color SetFontColor = Color.Blue; / 字体类型 public string SetFontFamily = Verdana; / 背景色 public Color SetBackgroundColor
5、= Color.AliceBlue; / 前景噪点数量 public int SetForeNoisePointCount = 2; / 随机码的旋转角度 public int SetRandomAngle = 40; / 是否随机字体颜色 public bool SetIsRandomColor = false; / 图片宽度 private int SetWith get return this.SetVerifyCodeText.Length * SetFontSize; / 图片高度 private int SetHeight return Convert.ToInt32(60.0 /
6、 100) * SetFontSize + SetFontSize); #endregion #region Constructor Method public VerifyCodeSugar() this.GetVerifyCodeText(); #region Private Method / 得到验证码字符串 private void GetVerifyCodeText() /没有外部输入验证码时随机生成 if (String.IsNullOrEmpty(this.SetVerifyCodeText) StringBuilder objStringBuilder = new String
7、Builder(); /加入数字1-9 for (int i = 1; i private void AddForeNoisePoint(Bitmap objBitmap) objBitmap.Width * this.SetForeNoisePointCount; objBitmap.SetPixel(objRandom.Next(objBitmap.Width), objRandom.Next(objBitmap.Height), this.SetFontColor); / 添加背景噪点objGraphics private void AddBackgroundNoisePoint(Bit
8、map objBitmap, Graphics objGraphics) using (Pen objPen = new Pen(Color.Azure, 0) objBitmap.Width * 2; objGraphics.DrawRectangle(objPen, objRandom.Next(objBitmap.Width), objRandom.Next(objBitmap.Height), 1, 1); / 获取随机颜色returns/returns private Color GetRandomColor() Random RandomNum_First = new Random
9、(int)DateTime.Now.Ticks); / 对于C#的随机数,没什么好说的 System.Threading.Thread.Sleep(RandomNum_First.Next(50); Random RandomNum_Sencond = new Random(int)DateTime.Now.Ticks); / 为了在白色背景上显示,尽量生成深色 int int_Red = RandomNum_First.Next(256); int int_Green = RandomNum_Sencond.Next(256); int int_Blue = (int_Red + int_G
10、reen 400) ? 0 : 400 - int_Red - int_Green; int_Blue = (int_Blue 255) ? 255 : int_Blue; return Color.FromArgb(int_Red, int_Green, int_Blue); #region Public Method / 输出验证码图片objHttpResponseHttp响应实例输出是否成功 public bool OutputImage(HttpResponse objHttpResponse) bool result = false; if (this.SetIsRandomColo
11、r) this.SetFontColor = GetRandomColor(); ; using (Bitmap objBitmap = this.GetVerifyCodeImage() if (objBitmap != null) using (MemoryStream objMS = new MemoryStream() objBitmap.Save(objMS, ImageFormat.Jpeg); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ContentType = image/
12、Jpeg HttpContext.Current.Response.BinaryWrite(objMS.ToArray(); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); result = true; / 获取问题questionList默认数字加减验证 public KeyValuePair GetQuestion(Dictionary questionList = null) if (questionList = null) questionList = new Dictionary();
13、 var operArray = new string +, *num var left = objRandom.Next(0, 10); var right = objRandom.Next(0, 10); var oper = operArrayobjRandom.Next(0, operArray.Length); if (oper = ) string key = string.Format(0+1=?, left, right); string val = (left + right).ToString(); questionList.Add(key, val); else if (oper = 01=? string val = (left * right).ToString(); else var num = objRandom.Next(1000, 9999); questionList.Add(num.ToString(), num.ToString(); return questionList.ToList()objRandom.Next(0, questionList.Count);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1