可按Ctrl+D收藏 蚂蚁资源网

蚂蚁资源网

验证码源码(搭建验证码平台源码分享)

  • 全部评论(3)
  • 亚亚
  • #region 生成随机数 public static string RandomNum(int n) { string strchar = "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"; string[] VcArray = strchar.Split(','); string VNum = ""; int temp = -1; Random rand = new Random(); for (int i = 1; i < n + 1; i++) { if (temp != -1) { rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks)); } int t = rand.Next(61); if (temp != -1 && temp == t) { return RandomNum(n); } temp = t; VNum += VcArray[t]; } return VNum; } #endregion #region 验证码图片 public static void CheckCodes(string M_CheckCode) { Random rand = new Random(); int iwidth = (int)(M_CheckCode.Length * 15); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22); Graphics g = Graphics.FromImage(image); g.Clear(Color.White); //画图片的背景噪音线20条 for (int i = 0; i < 20; i++) { int x1 = rand.Next(image.Width); int x2 = rand.Next(image.Width); int y1 = rand.Next(image.Height); int y2 = rand.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2); } //定义颜色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple, Color.YellowGreen }; //定义字体 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体", "新宋体" }; //随机输出噪点 for (int i = 0; i < 50; i++) { int x = rand.Next(image.Width); int y = rand.Next(image.Height); g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1); } //输出不同字体和颜色的验证码字符 for (int i = 0; i < M_CheckCode.Length; i++) { int cindex = rand.Next(7); int findex = rand.Next(6); Font f = new System.Drawing.Font(font[findex], 12, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush(c[cindex]); int ii = 4; if ((i + 1) % 2 == 0) { ii = 2; } g.DrawString(M_CheckCode.Substring(i, 1), f, b, 3 + (i * 12), ii); } //画一个边框 g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1); //输出到浏览器 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); HttpContext.Current.Response.ClearContent(); //Response.ClearContent(); HttpContext.Current.Response.ContentType = "image/gif"; HttpContext.Current.Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); } #endregion
  • 2021-02-20 01:35:01
  • LouisWang
  • 我举个.net的验证码产生的代码,你可以看看: 新建yanzhengma1.aspx.cs文件,写入一下代码: using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Drawing; public partial class yanzhengma : System.Web.UI.Page { // private System.IO.MemoryStream ms = newSystem.IO.MemoryStream(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { yanzhengmayzm = new yanzhengma(); Session["code1"] = yzm.CreateImage(5, ValidType.Numeric); } } /// <summary> /// 验证码的类型 /// </summary> public enum ValidType { /// <summary> /// 只有数字 /// </summary> Numeric, /// <summary> /// 数字和英文字符 /// </summary> NumericAndEnglishChar, /// <summary> /// 中文字符 /// </summary> ChineseChar } /// <summary> /// 生成一个随机文字图片,保存在 Session["code1"] /// </summary> /// <param name="count">图片中字的个数</param> /// <returns>生成的文字</returns> public string CreateImage(int count, ValidType type) { string ValidCode = GenCode(count,type); switch (type) { caseValidType.Numeric: CreateCheckCodeImage(ValidCode, 13.5); break; caseValidType.NumericAndEnglishChar: CreateCheckCodeImage(ValidCode, 14); break; caseValidType.ChineseChar: CreateCheckCodeImage(ValidCode, 22.5); break; default: break; } return ValidCode; } /// <summary> /// 产生随机字符串 /// </summary> /// <param name="num">随机出几个字符</param> /// <returns>随机出的字符串</returns> private string GenCode(int num, ValidType type) { string str; switch (type) { caseValidType.Numeric: str = "0123456789"; break; caseValidType.NumericAndEnglishChar: str ="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; break; caseValidType.ChineseChar: //常用498个汉字 写不完了,字数限制,你可以留下邮箱我给你完整的参考代码
  • 2021-02-11 17:10:05
  • 万岳科技
  • 求ASP验证码代码,<%Option Explicit ' 显示声明Class Com_GifCode_ClassPublic Noisy, Count, Width, Height, Angle, Offset, BorderPrivate Graph(), Margin(3)Private Sub Class_Initialize()RandomizeNoisy = 6 ' 干扰点出现的概率Count = 4 ' 字符数量Width = 80 ' 图片宽度Height = 20 ' 图片高度Angle = 2 ' 角度随机变化量Offset = 20 ' 偏移随机变化量Border = 1 ' 边框大小End SubPublic Function Create()Const cCharSet = "123456789"Dim i, x, yDim vValidCode : vValidCode = ""Dim vIndexReDim Graph(Width-1, Height-1)For i = 0 To Count - 1vIndex = Int(Rnd * Len(cCharSet))vValidCode = vValidCode + Mid(cCharSet, vIndex+1 , 1)SetDraw vIndex, iNextCreate = vValidCodeEnd FunctionSub SetDot(pX, pY)If pX * (Width-pX-1) >= 0 And pY * (Height-pY-1) >= 0 ThenGraph(pX, pY) = 1End IfEnd SubPublic Sub SetDraw(pIndex, pNumber)' 字符数据Dim DotData(8)DotData(0) = Array(30, 15, 50, 1, 50, 100)DotData(1) = Array(1 ,34 ,30 ,1 ,71, 1, 100, 34, 1, 100, 93, 100, 100, 86)DotData(2) = Array(1, 1, 100, 1, 42, 42, 100, 70, 50, 100, 1, 70)DotData(3) = Array(100, 73, 6, 73, 75, 6, 75, 100)DotData(4) = Array(100, 1, 1, 1, 1, 50, 50, 35, 100, 55, 100, 80, 50, 100, 1, 95)DotData(5) = Array(100, 20, 70, 1, 20, 1, 1, 30, 1, 80, 30, 100, 70, 100, 100, 80, 100, 60, 70, 50, 30, 50, 1, 60)DotData(6) = Array(6, 26, 6, 6, 100, 6, 53, 100)DotData(7) = Array(100, 30, 100, 20, 70, 1, 30, 1, 1, 20, 1, 30, 100, 70, 100, 80, 70, 100, 30, 100, 1, 80, 1, 70, 100, 30)DotData(8) = Array(1, 80, 30, 100, 80, 100, 100, 70, 100, 20, 70, 1, 30, 1, 1, 20, 1, 40, 30, 50, 70, 50, 100, 40)Dim vExtent : vExtent = Width / CountMargin(0) = Border + vExtent * (Rnd * Offset) / 100 + Margin(1)Margin(1) = vExtent * (pNumber + 1) - Border - vExtent * (Rnd * Offset) / 100Margin(2) = Border + Height * (Rnd * Offset) / 100Margin(3) = Height - Border - Height * (Rnd * Offset) / 100Dim vStartX, vEndX, vStartY, vEndYDim vWidth, vHeight, vDX, vDY, vDeltaTDim vAngle, vLengthvWidth = Int(Margin(1) - Margin(0))vHeight = Int(Margin(3) - Margin(2))' 起始坐标vStartX = Int((DotData(pIndex)(0)-1) * vWidth / 100)vStartY = Int((DotData(pIndex)(1)-1) * vHeight / 100)Dim i, jFor i = 1 To UBound(DotData(pIndex), 1)/2If DotData(pIndex)(2*i-2) <> 0 And DotData(pIndex)(2*i) <> 0 Then' 终点坐标vEndX = (DotData(pIndex)(2*i)-1) * vWidth / 100vEndY = (DotData(pIndex)(2*i+1)-1) * vHeight / 100' 横向差距vDX = vEndX - vStartX' 纵向差距vDY = vEndY - vStartY' 倾斜角度If vDX = 0 ThenvAngle = Sgn(vDY) * 3.14/2ElsevAngle = Atn(vDY / vDX)End If' 两坐标距离If Sin(vAngle) = 0 ThenvLength = vDXElsevLength = vDY / Sin(vAngle)End If' 随机转动角度vAngle = vAngle + (Rnd - 0.5) * 2 * Angle * 3.14 * 2 / 100vDX = Int(Cos(vAngle) * vLength)vDY = Int(Sin(vAngle) * vLength)If Abs(vDX) > Abs(vDY) Then vDeltaT = Abs(vDX) Else vDeltaT = Abs(vDY)For j = 1 To vDeltaTSetDot Margin(0) + vStartX + j * vDX / vDeltaT, Margin(2) + vStartY + j * vDY / vDeltaTNextvStartX = vStartX + vDXvStartY = vStartY + vDYEnd IfNextEnd SubPublic Sub Output()Response.Expires = -9999Response.AddHeader "pragma", "no-cache"Response.AddHeader "cache-ctrol", "no-cache"Response.ContentType = "image/gif"' 文件类型Response.BinaryWrite ChrB(Asc("G")) & ChrB(Asc("I")) & ChrB(Asc("F"))' 版本信息Response.BinaryWrite ChrB(Asc("8")) & ChrB(Asc("9")) & ChrB(Asc("a"))' 逻辑屏幕宽度Response.BinaryWrite ChrB(Width Mod 256) & ChrB((Width \ 256) Mod 256)' 逻辑屏幕高度Response.BinaryWrite ChrB(Height Mod 256) & ChrB((Height \ 256) Mod 256)Response.BinaryWrite ChrB(128) & ChrB(0) & ChrB(0)' 全局颜色列表Response.BinaryWrite ChrB(255) & ChrB(255) & ChrB(255)Response.BinaryWrite ChrB(0) & ChrB(85) & ChrB(255)' 图象标识符Response.BinaryWrite ChrB(Asc(","))Response.BinaryWrite ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0)' 图象宽度Response.BinaryWrite ChrB(Width Mod 256) & ChrB((Width \ 256) Mod 256)' 图象高度Response.BinaryWrite ChrB(Height Mod 256) & ChrB((Height \ 256) Mod 256)Response.BinaryWrite ChrB(0) & ChrB(7) & ChrB(255)Dim x, y, i : i = 0For y = 0 To Height - 1For x = 0 To Width - 1If Rnd < Noisy / 100 ThenResponse.BinaryWrite ChrB(1-Graph(x, y))ElseIf x * (x-Width) = 0 or y * (y-Height) = 0 ThenResponse.BinaryWrite ChrB(Graph(x, y))ElseIf Graph(x-1, y) = 1 or Graph(x, y) or Graph(x, y-1) = 1 ThenResponse.BinaryWrite ChrB(1)ElseResponse.BinaryWrite ChrB(0)End IfEnd IfEnd IfIf (y * Width + x + 1) Mod 126 = 0 ThenResponse.BinaryWrite ChrB(128)i = i + 1End IfIf (y * Width + x + i + 1) Mod 255 = 0 ThenIf (Width*Height - y * Width - x - 1) > 255 ThenResponse.BinaryWrite ChrB(255)ElseResponse.BinaryWrite ChrB(Width * Height Mod 255)End IfEnd IfNextNextResponse.BinaryWrite ChrB(128) & ChrB(0) & ChrB(129) & ChrB(0) & ChrB(59)End SubEnd ClassDim mCodeSet mCode = New Com_GifCode_ClassSession("GetCode") = mCode.Create()mCode.Output()Set mCode = Nothing%>调用方法:保存以上代码为checkGIF.asp 程序代码<input name="CheckCode" type="text" class="InputTxtLogin" id="CheckCode" onMouseOver="this.style.background='#ffffff';" onMouseOut="this.style.background='#f3f3f3'" size="4" maxlength="4"><img src="checkGIF.asp">
  • 2021-02-11 17:10:05
  • 商品推荐