You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.5 KiB

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
namespace Znyc.Admin.Commons.VerificationCode
{
/// <summary>
/// 验证码实现
/// </summary>
public class Captcha
{
private const string Letters = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
/// <summary>
/// 生成验证码图片
/// </summary>
/// <param name="captchaCode">验证码</param>
/// <param name="width">图形宽度,默认为验证码长度x25</param>
/// <param name="height">图形高度,默认30px</param>
/// <returns></returns>
public async Task<CaptchaResult> GenerateCaptchaImageAsync(string captchaCode, int width = 0, int height = 30)
{
//验证码颜色集合
Color[] c =
{
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" };
//定义图像的大小,生成图像的实例
Bitmap image = new Bitmap(width == 0 ? captchaCode.Length * 25 : width, height);
Graphics g = Graphics.FromImage(image);
//背景设为白色
g.Clear(Color.White);
Random random = new Random();
// 画图片的背景噪音线
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
}
//验证码绘制在g中
for (int i = 0; i < captchaCode.Length; i++)
{
//随机颜色索引值
int cindex = random.Next(c.Length);
//随机字体索引值
int findex = random.Next(fonts.Length);
//字体
Font f = new Font(fonts[findex], 15, FontStyle.Bold);
//颜色
Brush b = new SolidBrush(c[cindex]);
int ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
//绘制一个验证字符
g.DrawString(captchaCode.Substring(i, 1), f, b, 17 + i * 17, ii);
}
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int x1 = random.Next(image.Width);
int y = random.Next(image.Height);
int y1 = random.Next(image.Height);
if (i % 11 == 0)
{
g.DrawLine(new Pen(Color.Silver), x, y, x1, y1);
}
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
//g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Png);
g.Dispose();
image.Dispose();
return await Task.FromResult(new CaptchaResult
{
CaptchaCode = captchaCode,
CaptchaMemoryStream = ms,
Timestamp = DateTime.Now
});
}
/// <summary>
/// 生成随机验证码字符
/// </summary>
/// <param name="codeLength">验证码位数</param>
/// <returns></returns>
public async Task<string> GenerateRandomCaptchaAsync(int codeLength = 4)
{
string[] array = Letters.Split(new[] { ',' });
Random random = new Random();
int temp = -1;
string captcheCode = string.Empty;
for (int i = 0; i < codeLength; i++)
{
if (temp != -1)
{
random = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int index = random.Next(array.Length);
if (temp != -1 && temp == index)
{
return await GenerateRandomCaptchaAsync(codeLength);
}
temp = index;
captcheCode += array[index];
}
return await Task.FromResult(captcheCode);
}
}
}