使用C#生成驗證碼圖片,然後在網頁中直接使用,很方便的用法。
System; System.Drawing; System.Drawing.Imaging; System.Web.UI; System.Drawing.Drawing2D; System.IO; { ValidateCode() { } MaxLength { { 10; } } MinLength { { 1; } } CreateValidateCode(length) { [] randMembers = [length]; [] validateNums = [length]; validateNumberStr = ; seekSeek = (().Now.Ticks); seekRand = (seekSeek); beginSeek = ()seekRand.Next(0, .MaxValue - length * 10000); [] seeks = [length]; (i = 0; i < length; i++) { beginSeek += 10000; seeks[i] = beginSeek; } (i = 0; i < length; i++) { rand = (seeks[i]); pownum = 1 * ().Pow(10, length); randMembers[i] = rand.Next(pownum, .MaxValue); } (i = 0; i < length; i++) { numStr = randMembers[i].ToString(); numLength = numStr.Length; rand = (); numPosition = rand.Next(0, numLength - 1); validateNums[i] = .Parse(numStr.Substring(numPosition, 1)); } (i = 0; i < length; i++) { validateNumberStr += validateNums[i].ToString(); } validateNumberStr; } [] CreateValidateGraphic(validateCode) { Bitmap image = Bitmap(().Ceiling(validateCode.Length * 12.0), 24); Graphics g = Graphics.FromImage(image); { random = (); g.Clear(Color.White); (i = 0; i < 25; i++) { x1 = random.Next(image.Width); x2 = random.Next(image.Width); y1 = random.Next(image.Height); y2 = random.Next(image.Height); g.DrawLine(Pen(Color.Silver), x1, y1, x2, y2); } Font font = Font(, 12, (FontStyle.Bold | FontStyle.Italic)); LinearGradientBrush brush = LinearGradientBrush(Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, ); g.DrawString(validateCode, font, brush, 3, 2); (i = 0; i < 100; i++) { x = random.Next(image.Width); y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } g.DrawRectangle(Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); stream = (); image.Save(stream, ImageFormat.Jpeg); stream.ToArray(); } { g.Dispose(); image.Dispose(); } } GetImageWidth(validateNumLength) { ()(validateNumLength * 12.0); } GetImageHeight() { 22.5; } }