ASP.NET图形验证码的生成

时间:2023-03-09 03:12:41
ASP.NET图形验证码的生成

效果:

ASP.NET图形验证码的生成

调用方法:

int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列
string vcode = QAPI.VerifImage.CreateVCode(r, );//产生验证码字符
pictureBox1.Image = QAPI.VerifImage.CreateVerifImage(vcode, r, true, true, TextRenderingHint.AntiAlias);//生成
//-----ASP.NET中上面最后一句改成:
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
Response.ContentType = "image/png";

源码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks; namespace QAPI
{
class VerifImage
{
[DllImport("kernel32.dll")]
private static extern void CopyMemory(int[] Destination, byte[] add, int Length);
static Color[] Backcolor = { Color.SeaShell, Color.LightCyan, Color.MistyRose, Color.AliceBlue, Color.Wheat, Color.PowderBlue };
static int[] thCor = { , , , , , , , , , , , };
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="vcode">验证码字符,仅限4个长度的英文和数字</param>
/// <param name="lstR">随机种子数组</param>
/// <param name="Aliasing">抗锯齿开关</param>
/// <param name="Backline">背景线条开关</param>
/// <param name="rth">字符打印质量模式</param>
/// <returns>返回Bitmap对象</returns>
public static Bitmap CreateVerifImage(string vcode, int[] lstR,
bool Aliasing = false,
bool Backline = true,
TextRenderingHint rth = TextRenderingHint.SystemDefault)
{
Bitmap map = new Bitmap(, );//创建图片背景
using (Graphics g = Graphics.FromImage(map))
{
g.Clear(Backcolor[lstR[] % ]);
if (Aliasing && Backline)//抗锯齿,用于背景线条
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
}
int cr = ; int cg = ; int cb = ;
if (Backline)//背景线条
for (int i = ; i < ; i++)//绘制背景线条,横竖各3条
{
cr = new Random(lstR[] + i * ).Next(, );
cg = new Random(lstR[] + i * ).Next(, );
cb = new Random(lstR[] + i * ).Next(, );
Pen pb = new Pen(Color.FromArgb(cr, cg, cb), i % + );
Point p1 = new Point(new Random(lstR[] + i * ).Next(, ), );
Point p2 = new Point(new Random(lstR[] + i * ).Next(, ), );
g.DrawLine(pb, p1, p2);
cr = new Random(lstR[] + i * ).Next(, );
cg = new Random(lstR[] + i * ).Next(, );
cb = new Random(lstR[] + i * ).Next(, );
pb = new Pen(Color.FromArgb(cr, cg, cb), i % + );
p1 = new Point(, new Random(lstR[] + i * ).Next(, ));
p2 = new Point(, new Random(lstR[] + i * ).Next(, ));
g.DrawLine(pb, p1, p2);
}
g.TextRenderingHint = rth;
Font f;
int left = ; int top = ;
for (int i = ; i < vcode.Length; i++)
{
int asc = (int)vcode[i];
int min = asc > ? : ;//大写英文和数字同字号大小有差异
int max = asc > ? : ;
int sz = new Random(lstR[i]).Next(min, max);
f = new Font("Arial", sz);
asc = (lstR[i] % ) * ;//字体颜色
cr = new Random(lstR[i]).Next(thCor[asc], thCor[asc + ]);
cb = new Random(lstR[i + ]).Next(thCor[asc + ], thCor[asc + ]);
cg = new Random(lstR[i + ]).Next(, );
Color cor = Color.FromArgb(cr, cg, cb);
if (i < )
left = (i * ) - + (sz % );
else
left = i * - (sz % );
top = - + (sz % );
g.DrawString(vcode[i].ToString(), f, new SolidBrush(cor), left, top);
}
}
return map;
}
/// <summary>
/// 生成随机英文数字的随机字符
/// </summary>
/// <param name="lstR">随机种子数组</param>
/// <param name="mode">生成模式</param>
/// <returns>长度4的字符串</returns>
public static string CreateVCode(int[] lstR, int mode = )
{
//去掉容易错判的字母 i,l,o,z
char[] c = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'', '', '', '', '', '', '', '', '', ''};
int p = ;
int length = ;
switch (mode)
{
case : p = ; length = ; break; //只是数字
case : p = ; length = ; break; //只是小写字母
case : p = ; length = ; break; //只是大写字母
case : p = ; length = ; break; //大小写混合
case : p = ; length = ; break; //大写字母+数字(不含1和0)
default: p = ; length = c.Length; break; //所有混合
}
string s = c[lstR[] % length + p].ToString() +
c[lstR[] % length + p].ToString() +
c[lstR[] % length + p].ToString() +
c[lstR[] % length + p].ToString();
return s;
}
public static int[] RandomList()
{
string guid = System.Guid.NewGuid().ToString("N");//f61fb920bb56454a86dfb91162717d87
byte[] byt = System.Text.Encoding.ASCII.GetBytes(guid);
int[] r = new int[];
CopyMemory(r, byt, );
return r;
}
//CreateVerifImage()最后一个参数的可能值
//System.Drawing.Text.TextRenderingHint.TextRenderingHint.SingleBitPerPixel;
//System.Drawing.Text.TextRenderingHint.TextRenderingHint.AntiAlias;
//System.Drawing.Text.TextRenderingHint.TextRenderingHint.SystemDefault;
//System.Drawing.Text.TextRenderingHint.TextRenderingHint.ClearTypeGridFit;
//System.Drawing.Text.TextRenderingHint.TextRenderingHint.SingleBitPerPixelGridFit;
}
}