C#验证码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | /// <summary> ///验证码 /// </summary> public class VerifyCode { /// <summary> /// 验证码字体大小 ( 为了显示扭曲效果,默认 40 像素,可以自行修改 ) /// </summary> int FontSize = 40; /// <summary> /// 文字间距 /// </summary> int Padding = 3; /// <summary> /// 自定义背景色 ( 默认白色 ) /// </summary> Color BackgroundColor = Color.White; /// <summary> /// 自定义随机颜色数组 /// </summary> Color[] Colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; /// <summary> /// 自定义字体数组 /// </summary> string [] Fonts = { "Arial" , "Georgia" , "courier new" }; /// <summary> /// 自定义随机码字符串序列 ( 使用逗号分隔 ) /// </summary> string CodeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z" ; #region 产生波形滤镜效果 private const double PI2 = 6.283185307179586476925286766559; /// <summary> /// 正弦曲线 Wave 扭曲图片( Edit By 51aspx.com ) /// </summary> /// <param name="srcBmp"> 图片路径 </param> /// <param name="bXDir"> 如果扭曲则选择为 True </param> /// <param name="dMultValue"> 波形的幅度倍数,越大扭曲的程度越高,一般为 3 </param> /// <param name="dPhase"> 波形的起始相位,取值区间 [0-2*PI) </param> /// <returns></returns> public byte [] TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase) { using (Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height)) { // 将位图背景填充为白色 using (Graphics graph = Graphics.FromImage(destBmp)) { graph.FillRectangle( new SolidBrush(Color.White), 0, 0, destBmp.Width, destBmp.Height); 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 = (bXDir ? (PI2 * ( double )j) / dBaseAxisLen : (PI2 * ( double )i) / dBaseAxisLen) + dPhase; double dy = Math.Sin(dx); // 取得当前点的颜色 int nOldX = bXDir ? i + ( int )(dy * dMultValue) : i; int 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); } } } } using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { destBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return ms.GetBuffer(); } } } #endregion #region 生成校验码图片 public byte [] CreateImageCode( int codeLen = 6) { string code = CreateVerifyCode(codeLen); int fSize = FontSize; int fWidth = fSize + Padding; int imageWidth = ( int )(code.Length * fWidth) + 4 + Padding * 2; int imageHeight = fSize * 2 + Padding; using (Bitmap image = new Bitmap(imageWidth, imageHeight)) { using (Graphics g = Graphics.FromImage(image)) { g.Clear(BackgroundColor); Random rand = new Random(); int left = 0, top = 0; int n1 = (imageHeight - FontSize - Padding * 2); int n2 = n1 / 4; Font f; Brush b; int cindex, findex; // 随机字体和颜色的验证码字符 for ( int i = 0; i < code.Length; i++) { cindex = rand.Next(Colors.Length - 1); findex = rand.Next(Fonts.Length - 1); f = new Font(Fonts[findex], fSize, FontStyle.Bold); b = new SolidBrush(Colors[cindex]); if (i % 2 == 1) { top = n2 * 2; } else { top = n2; } left = i * fWidth; g.DrawString(code.Substring(i, 1), f, b, left, top); } } // 产生波形 return TwistImage(image, true , 8, 4); } } #endregion #region 生成随机字符码 public string CreateVerifyCode( int codeLen) { string [] arr = CodeSerial.Split( ',' ); string code = "" ; Random rand = new Random( unchecked (( int )DateTime.Now.Ticks)); for ( int i = 0; i < codeLen; i++) { code += arr[rand.Next(0, arr.Length - 1)]; } return code; } #endregion } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构