验证码
Posted 5tomorrow
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证码相关的知识,希望对你有一定的参考价值。
======================================效果图=======================================
=======================================代码=======================================
/// <summary> /// 验证码生成 /// </summary> public class VerificationCodeHelper { //定义颜色 //private static Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; private static Color[] colors = { Color.Black }; //定义验证码字符及出现频次 ,避免出现0 o j i l 1 x; private static String charCollection = "ASDFGHZCVBNMKQWERTYUP"; //缓存标识 private static string cacheFlag = "CH_WebLoginVCode"; //字体大小 private static int fontSize = 25; //旋转角度 private static int angle = 38; /// <summary> /// 验证码生成 /// </summary> /// <param name="length">字符个数</param> /// <param name="width">宽度</param> /// <param name="height">高度</param> /// <returns></returns> public static byte[] GetBitmapCode(int width = 130, int height = 50, int length = 4) { string codes = ""; string code = ""; int codeX = width / (length + 1); Random random = new Random(); using (Bitmap image = new Bitmap(width, height)) { using (Graphics g = Graphics.FromImage(image)) { //清空图片背景色 g.Clear(Color.FromArgb(247, 247, 247)); g.FillRectangle(new SolidBrush(Color.FromArgb(247, 247, 247)), 0, 0, image.Width, image.Height); //画图片的背景噪音线 for (int i = 0; i < 5; i++) { g.DrawLine(new Pen(colors[random.Next(colors.Length)]), random.Next(image.Width), random.Next(image.Height), random.Next(image.Width), random.Next(image.Height)); } //文字距中 StringFormat format = new StringFormat(StringFormatFlags.NoClip); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Near; using (Font f = new Font("宋体", fontSize, System.Drawing.FontStyle.Regular)) { using (Brush b = new SolidBrush(colors[random.Next(colors.Length)])) { for (int i = 0; i < length; i++) { code = charCollection[random.Next(0, charCollection.Length)].ToString(); SizeF sizeF = g.MeasureString(code, f); PointF dot = new PointF(codeX, random.Next(image.Height / 2)); g.TranslateTransform(dot.X, dot.Y);//移动光标到指定位置 int rAngel = random.Next(-angle, angle); g.RotateTransform(rAngel); g.DrawString(code, f, b, 1, 1, format); g.RotateTransform(-rAngel);//转回去 g.TranslateTransform(2, -dot.Y);//移动光标到指定位置 codes += code; } } } //返回图片数据 using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); SaveToChache(codes); return ms.ToArray(); } } } } /// <summary> /// 贝塞尔曲线 /// </summary> /// <returns></returns> public static int CreateImages(Graphics g, int beginX, int endX, int endY, int height) { Random random = new Random(); //起始点坐标 int beginx = beginX; int endx = endX; int beginy = random.Next(height); int endy = endY; //计算中间点Y最大位置 int maxDianY = height / 2; int minDianY = 0; if (endY > maxDianY) { minDianY = maxDianY; maxDianY = height; } //中间点 int dian1x = random.Next(beginX, endX); int dian2x = random.Next(endX); int dian1y = random.Next(minDianY, maxDianY); int dian2y = random.Next(minDianY, maxDianY); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, endX, height), Color.Black, Color.Black, 1.2f, true); g.DrawBezier(new Pen(brush), beginx, beginy, dian1x, dian1y, dian2x, dian2y, endx, endy); return endy; } /// <summary> /// 保存验证码到内存中 /// </summary> /// <param name="code"></param> public static void SaveToChache(string code) { CacheHelper.Set(cacheFlag, code); } /// <summary> /// 验证验证码是否已过期 /// </summary> /// <returns></returns> public static bool CheckCodeIsExpire() { return CacheHelper.Get(cacheFlag) == null; } /// <summary> /// 验证验证码是否正确 /// </summary> /// <param name="code"></param> /// <returns></returns> public static bool CheckCodeIsOK(string code) { string cacheCode = CacheHelper.Get(cacheFlag) as string; if (cacheCode == null) { return false; } if (cacheCode.Equals(code, StringComparison.OrdinalIgnoreCase) == false) { CacheHelper.Remove(cacheFlag); return false; } return true; } }
以上是关于验证码的主要内容,如果未能解决你的问题,请参考以下文章
Android SMS Verification API 结果码始终为 0
爬虫遇到头疼的验证码?Python实战讲解弹窗处理和验证码识别