From 6964a121f04288cca128b031887d6ae179f9c85b Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 27 Nov 2019 10:31:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=99=A8=EF=BC=8C=E9=85=8D=E7=BD=AE=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.ValidateCode/IValidateCode.cs | 15 +++- Falcon.ValidateCode/ValidateCode.cs | 95 +++++++++++++++++++++++ Falcon.ValidateCode/ValidateCodeOption.cs | 21 +++++ 3 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 Falcon.ValidateCode/ValidateCode.cs create mode 100644 Falcon.ValidateCode/ValidateCodeOption.cs diff --git a/Falcon.ValidateCode/IValidateCode.cs b/Falcon.ValidateCode/IValidateCode.cs index 0c08a1a..68d4255 100644 --- a/Falcon.ValidateCode/IValidateCode.cs +++ b/Falcon.ValidateCode/IValidateCode.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.IO; namespace Falcon.ValidateCode { @@ -9,6 +7,17 @@ namespace Falcon.ValidateCode /// public interface IValidateCode { + /// + /// 生成随机代码 + /// + /// + string GenerateCode(); + /// + /// 根据代码生成验证图片 + /// + /// + /// + Stream GenerateImgStream(string code); } } diff --git a/Falcon.ValidateCode/ValidateCode.cs b/Falcon.ValidateCode/ValidateCode.cs new file mode 100644 index 0000000..f162f99 --- /dev/null +++ b/Falcon.ValidateCode/ValidateCode.cs @@ -0,0 +1,95 @@ +using System; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using System.Text; + +namespace Falcon.ValidateCode +{ + /// + /// 图片验证码 + /// + public class VerificationCodeServices:IValidateCode + { + /// + /// 生成选项 + /// + public ValidateCodeOption Options { get; set; } + private static Random _ran = new Random(unchecked((int)DateTime.Now.Ticks)); + /// + /// 实例化验证码生成器 + /// + public VerificationCodeServices() : this(new ValidateCodeOption()) { + + } + /// + /// 通过提供配置实例化验证码生成器 + /// + /// + public VerificationCodeServices(ValidateCodeOption option) { + this.Options = option; + } + /// + /// 生成随机验证码 + /// + public string GenerateCode() { + var charLen = this.Options.CodeChars.Length; + StringBuilder code = new StringBuilder(charLen); + for(int i = 0;i < this.Options.CodeLength;i++) { + var nextC = _ran.Next(charLen); + var c = this.Options.CodeChars.Substring(nextC,1); + code.Append(c); + } + return code.ToString(); + } + /// + /// 生成图片数据流 + /// + /// + /// + public Stream GenerateImgStream(string code) { + if(string.IsNullOrEmpty(code)) { + throw new ArgumentNullException(nameof(code)); + } + Bitmap Img = null; + Graphics graphics = null; + MemoryStream ms = null; + Random random = new Random(); + //颜色集合 + Color[] color = { Color.Black,Color.Red,Color.DarkBlue,Color.Green,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple }; + //字体集合 + string[] fonts = { "Verdana","Microsoft Sans Serif","Comic Sans MS","Arial","宋体" }; + //定义图像的大小,生成图像的实例 + Img = new Bitmap((int)code.Length * 18,32); + graphics = Graphics.FromImage(Img);//从Img对象生成新的Graphics对象 + graphics.Clear(Color.White);//背景设为白色 + + //在随机位置画背景点 + + for(int i = 0;i < 100;i++) { + int x = random.Next(Img.Width); + int y = random.Next(Img.Height); + graphics.DrawRectangle(new Pen(Color.LightGray,0),x,y,1,1); + } + + //验证码绘制在graphics中 + for(int i = 0;i < code.Length;i++) { + int colorIndex = random.Next(7);//随机颜色索引值 + int fontIndex = random.Next(4);//随机字体索引值 + Font font = new Font(fonts[fontIndex],15,FontStyle.Bold);//字体 + Brush brush = new SolidBrush(color[colorIndex]);//颜色 + int y = 4; + if((i + 1) % 2 == 0)//控制验证码不在同一高度 + { + y = 2; + } + graphics.DrawString(code.Substring(i,1),font,brush,3 + (i * 12),y);//绘制一个验证字符 + } + ms = new MemoryStream();//生成内存流对象 + Img.Save(ms,ImageFormat.Png);//将此图像以Png图像文件的格式保存到流中 + graphics.Dispose(); + Img.Dispose(); + return ms; + } + } +} \ No newline at end of file diff --git a/Falcon.ValidateCode/ValidateCodeOption.cs b/Falcon.ValidateCode/ValidateCodeOption.cs new file mode 100644 index 0000000..895cbb8 --- /dev/null +++ b/Falcon.ValidateCode/ValidateCodeOption.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Falcon.ValidateCode +{ + /// + /// 生成验证码选项 + /// + public class ValidateCodeOption + { + /// + /// 验证码使用的字符 + /// + public string CodeChars { get; set; } = "0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ"; + /// + /// 生成验证码的长度 + /// + public int CodeLength { get; set; } = 5; + } +}