C#生成图形验证码

时间:2023-03-09 21:30:49
C#生成图形验证码

先看效果:

C#生成图形验证码

再上代码

public class CaptchaHelper
{
private static Random rand = new Random();
private static int previousAngle = 0; /// <summary>
/// 生成图形验证码
/// </summary>
/// <returns></returns>
public static byte[] Create(int codeLength, int width, int height, int fontSize, out String code)
{
String sCode = String.Empty;
//可用颜色列表
Color[] oColors = {
Color.FromArgb(118, 199, 192),
Color.FromArgb(97, 216, 206),
Color.FromArgb(84, 176, 168),
Color.FromArgb(117, 226, 217),
Color.FromArgb(43, 205, 191)
//Color.Red,
//Color.Blue,
//Color.Green,
//Color.Orange,
//Color.Brown,
//Color.Brown,
//Color.DarkBlue
};
//可用字体列表
string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
//可用字符列表
char[] oCharacter = {
'2','3','4','5','6','8','9',
'A','B','C','D','E','F','G','H','J','K','L','M','N','P','R','S','T','W','X','Y'
};
//Random oRnd = new Random();
Bitmap oBmp = null;
Graphics oGraphics = null;
int N1 = 0;
Point oPoint1 = default(Point);
Point oPoint2 = default(Point);
string sFontName = null;
Font oFont = null;
Color oColor = default(Color); //生成验证码字符串
for (N1 = 0; N1 <= codeLength - 1; N1++)
{
sCode += oCharacter[rand.Next(oCharacter.Length)];
} oBmp = new Bitmap(width, height);
oGraphics = Graphics.FromImage(oBmp);
oGraphics.Clear(Color.White);
try
{
for (N1 = 0; N1 <= 2; N1++)
{
//画噪线
oPoint1.X = rand.Next(width);
oPoint1.Y = rand.Next(height);
oPoint2.X = rand.Next(width);
oPoint2.Y = rand.Next(height);
oColor = oColors[rand.Next(oColors.Length)];
oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2);
} float spaceWith = 0, dotX = 0;
if (codeLength != 0)
{
spaceWith = (width - fontSize * codeLength - 10) / codeLength;
} for (N1 = 0; N1 <= sCode.Length - 1; N1++)
{
//画验证字符串
sFontName = oFontNames[rand.Next(oFontNames.Length)];
oFont = new Font(sFontName, fontSize, FontStyle.Bold);
oColor = oColors[rand.Next(oColors.Length)]; dotX = Convert.ToSingle(N1) * fontSize + (N1 + 1) * spaceWith;
DrawCharacter(sCode[N1].ToString(), oGraphics, oFont, new SolidBrush(oColor), (int)dotX, fontSize, height);
} for (int i = 0; i <= 30; i++)
{
//画噪点
int x = rand.Next(oBmp.Width);
int y = rand.Next(oBmp.Height);
Color clr = oColors[rand.Next(oColors.Length)];
oBmp.SetPixel(x, y, clr);
} code = sCode;
//保存图片数据
MemoryStream stream = new MemoryStream();
oBmp.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
oGraphics.Dispose();
}
} private static void DrawCharacter(string txt, Graphics gr, Font font, Brush brush, int x, int charWidth, int height)
{
// 文字居中
using (StringFormat stringFormat = new StringFormat())
{
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
RectangleF rectf = new RectangleF(x, 0, charWidth, height); // 转换为 Path
using (GraphicsPath graphicsPath = new GraphicsPath())
{
graphicsPath.AddString(txt, font.FontFamily, (int)(font.Style), font.Size, rectf, stringFormat); // 旋转一个随机角度
float dx = (float)(x + charWidth / 2);
float dy = (float)(height / 2);
gr.TranslateTransform(-dx, -dy, MatrixOrder.Append);
int angle = previousAngle;
do
{
angle = rand.Next(-30, 30);
} while (Math.Abs(angle - previousAngle) < 20);
previousAngle = angle;
gr.RotateTransform(angle, MatrixOrder.Append);
gr.TranslateTransform(dx, dy, MatrixOrder.Append); // 画文字
gr.FillPath(brush, graphicsPath);
gr.ResetTransform();
}
}
}
}