再发一个验证码程序,带演示图

验证码效果图:

源码:

ValidCode
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Drawing.Imaging;
using System.Security.Cryptography;
using System.Drawing.Drawing2D;

public partial class ValidCode : System.Web.UI.Page


{
protected override void OnInit(EventArgs e)

{
base.OnInit(e);

string authStr = CreateAuthStr(4);

VerifyImage verifyimg = new VerifyImage(authStr, 90, 50);

System.Drawing.Bitmap image = verifyimg.Image;

System.Web.HttpContext.Current.Response.ContentType = "image/pjpeg";

//Session["AuthStr"] = authStr.ToLower();

image.Save(this.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}


/**//// <summary>
/// 产生验证码
/// </summary>
/// <returns>验证码</returns>
public static string CreateAuthStr(int len)

{
int number;
StringBuilder checkCode = new StringBuilder();

Random random = new Random();

for (int i = 0; i < len; i++)

{
number = random.Next();

if (number % 2 == 0)

{
checkCode.Append((char)('0' + (char)(number % 10)));
}
else

{
checkCode.Append((char)('A' + (char)(number % 26)));
}

}

return checkCode.ToString();
}
}


/**//// <summary>
/// 验证码图片类
/// </summary>
public class VerifyImage


{

/**//// <summary>
/// 要显示的文字
/// </summary>
public string Text

{

get
{ return this.text; }
}

/**//// <summary>
/// 图片
/// </summary>
public Bitmap Image

{

get
{ return this.image; }
}

/**//// <summary>
/// 宽度
/// </summary>
public int Width

{

get
{ return this.width; }
}

/**//// <summary>
/// 高度
/// </summary>
public int Height

{

get
{ return this.height; }
}

private string text;
private int width;
private int height;
private Bitmap image;

private static byte[] randb = new byte[4];
private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();


/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="code">要显示的验证码</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public VerifyImage(string code, int width, int height)

{
this.text = code;
this.width = width;
this.height = height;
this.GenerateImage();

}

~VerifyImage()

{
Dispose(false);
}

public void Dispose()

{
GC.SuppressFinalize(this);
this.Dispose(true);
}

protected virtual void Dispose(bool disposing)

{
if (disposing)
this.image.Dispose();
}

private FontFamily[] fonts =
{
new FontFamily("Times New Roman"),
new FontFamily("Georgia"),
new FontFamily("Arial"),
new FontFamily("Comic Sans MS")
};


public static int Next()

{
rand.GetBytes(randb);
int value = BitConverter.ToInt32(randb, 0);
if (value < 0) value = -value;
return value;
}

public static int Next(int max)

{
rand.GetBytes(randb);
int value = BitConverter.ToInt32(randb, 0);
value = value % (max + 1);
if (value < 0) value = -value;
return value;
}

public static int Next(int min, int max)

{
int value = Next(max - min) + min;
return value;
}



/**//// <summary>
/// 生成验证码图片
/// </summary>
private void GenerateImage()

{
Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);

Graphics g = Graphics.FromImage(bitmap);
Rectangle rect = new Rectangle(0, 0, this.width, this.height);
g.SmoothingMode = SmoothingMode.AntiAlias;

g.Clear(Color.White);

int emSize = Next(3) + 18;//(int)((this.width - 20) * 2 / text.Length);
FontFamily family = fonts[Next(fonts.Length - 1)];
Font font = new Font(family, emSize, FontStyle.Bold);

SizeF measured = new SizeF(0, 0);
SizeF workingSize = new SizeF(this.width, this.height);
while (emSize > 2 && (measured = g.MeasureString(text, font)).Width > workingSize.Width || measured.Height > workingSize.Height)

{
font.Dispose();
font = new Font(family, emSize -= 2);
}

SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100)));
for (int x = 0; x < 3; x++)

{
Pen linePen = new Pen(Color.FromArgb(Next(150), Next(150), Next(150)), 1);
g.DrawLine(linePen, new PointF(0.0F + Next(20), 0.0F + Next(this.height)), new PointF(0.0F + Next(this.width), 0.0F + Next(this.height)));
}

for (int x = 0; x < this.text.Length; x++)

{
drawBrush.Color = Color.FromArgb(Next(150) + 20, Next(150) + 20, Next(150) + 20);
PointF drawPoint = new PointF(0.0F + Next(4) + x * 15, 8.0F + Next(4));
g.DrawString(this.text[x].ToString(), font, drawBrush, drawPoint);
}

double distort = Next(5, 10) * (Next(10) == 1 ? 1 : -1);

using (Bitmap copy = (Bitmap)bitmap.Clone())

{
for (int y = 0; y < height; y++)

{
for (int x = 0; x < width; x++)

{
int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.0)));
int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 54.0)));
if (newX < 0 || newX >= width) newX = 0;
if (newY < 0 || newY >= height) newY = 0;
bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
}
}
}


//g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);

font.Dispose();
drawBrush.Dispose();
g.Dispose();

this.image = bitmap;
}

}


源码:












































































































































































































































































































【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述