完成接口,生成器,配置等

This commit is contained in:
falcon 2019-11-27 10:31:46 +08:00
parent 20ed901174
commit 6964a121f0
3 changed files with 128 additions and 3 deletions

View File

@ -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
/// </summary>
public interface IValidateCode
{
/// <summary>
/// 生成随机代码
/// </summary>
/// <returns></returns>
string GenerateCode();
/// <summary>
/// 根据代码生成验证图片
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
Stream GenerateImgStream(string code);
}
}

View File

@ -0,0 +1,95 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
namespace Falcon.ValidateCode
{
/// <summary>
/// 图片验证码
/// </summary>
public class VerificationCodeServices:IValidateCode
{
/// <summary>
/// 生成选项
/// </summary>
public ValidateCodeOption Options { get; set; }
private static Random _ran = new Random(unchecked((int)DateTime.Now.Ticks));
/// <summary>
/// 实例化验证码生成器
/// </summary>
public VerificationCodeServices() : this(new ValidateCodeOption()) {
}
/// <summary>
/// 通过提供配置实例化验证码生成器
/// </summary>
/// <param name="option"></param>
public VerificationCodeServices(ValidateCodeOption option) {
this.Options = option;
}
/// <summary>
/// 生成随机验证码
/// </summary>
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();
}
/// <summary>
/// 生成图片数据流
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
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;
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Falcon.ValidateCode
{
/// <summary>
/// 生成验证码选项
/// </summary>
public class ValidateCodeOption
{
/// <summary>
/// 验证码使用的字符
/// </summary>
public string CodeChars { get; set; } = "0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
/// <summary>
/// 生成验证码的长度
/// </summary>
public int CodeLength { get; set; } = 5;
}
}