winform 登录窗口怎么设置验证码验证登录? 求大神贴出代码来参考一下吧 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 生成验证码字符串 /// </summary> /// <param name="codeLen">验证码字符长度</param> /// <returns>返回验证码字符串</returns> private string MakeCode(int codeLen) { if (codeLen < 1) { return string.Empty; } int number; string checkCode = string.Empty; Random random = new Random(); for (int index = 0; index < codeLen; index++) { number = random.Next(); if (number % 2 == 0) { checkCode += (char)('0' + (char)(number % 10)); //生成数字 } else { checkCode += (char)('A' + (char)(number % 26)); //生成字母 } } return checkCode; } 生成验证码图片流:?// ///<summary> /// 获取验证码图片流 /// </summary> /// <param name="checkCode">验证码字符串</param> /// <returns>返回验证码图片流</returns> private MemoryStream CreateCodeImg(string checkCode) { if (string.IsNullOrEmpty(checkCode)) { return null; } Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); Graphics graphic = Graphics.FromImage(image); try { Random random = new Random(); graphic.Clear(Color.White); int x1 = 0, y1 = 0, x2 = 0, y2 = 0; for (int index = 0; index < 25; index++) { x1 = random.Next(image.Width); x2 = random.Next(image.Width); y1 = random.Next(image.Height); y2 = random.Next(image.Height); graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } Font font = new Font("Arial", 12, (FontStyle.Bold |FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true); graphic.DrawString(checkCode, font, brush, 2, 2); int x = 0; int y = 0; //画图片的前景噪音点 for(int i=0; i<100; i++) { x = random.Next(image.Width); y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的边框线 graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); //将图片验证码保存为流Stream返回 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); return ms; } finally { graphic.Dispose(); image.Dispose(); } } 在HttpHander的ProcessRequest中进行调用:?pupublic void ProcessRequest(HttpContext context) { string code = MakeCode(5); context.Response.ClearContent(); context.Response.ContentType = "image/Gif"; MemoryStream ms = CreateCodeImg(code); if (null != ms) { context.Response.BinaryWrite(ms.ToArray()); } } 楼上已经将验证码的生成过程贴出来了,只要在Winform中使用一个PictureBox显示就OK。 不能将NULL复制给float类型! NHibernate的问题 未处理的“System.InvalidOperationException”类型的异常出现在 system.data.dll 中。 请问如何将创建的 Bitmap 转成 String 循环冗余校验(CRC)算法 正则表达式 急 帮帮这只小菜鸟!~~~C#数据库的连接 菜的要哭系列1:关于DataGrid…… wince winform扫描二维码获取网页上二维码信息 哪有vs.net下载,100分只要能下马上给 登入界面,验证不同权限用户,以及验证码,密码后,实现登入 c# 跨线程 委托
/// 生成验证码字符串
/// </summary>
/// <param name="codeLen">验证码字符长度</param>
/// <returns>返回验证码字符串</returns>
private string MakeCode(int codeLen)
{
if (codeLen < 1)
{
return string.Empty;
}
int number;
string checkCode = string.Empty;
Random random = new Random();
for (int index = 0; index < codeLen; index++)
{
number = random.Next();
if (number % 2 == 0)
{
checkCode += (char)('0' + (char)(number % 10)); //生成数字
}
else
{
checkCode += (char)('A' + (char)(number % 26)); //生成字母
}
}
return checkCode;
} 生成验证码图片流:?// ///<summary>
/// 获取验证码图片流
/// </summary>
/// <param name="checkCode">验证码字符串</param>
/// <returns>返回验证码图片流</returns>
private MemoryStream CreateCodeImg(string checkCode)
{
if (string.IsNullOrEmpty(checkCode))
{
return null;
}
Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics graphic = Graphics.FromImage(image);
try
{
Random random = new Random();
graphic.Clear(Color.White);
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
for (int index = 0; index < 25; index++)
{
x1 = random.Next(image.Width);
x2 = random.Next(image.Width);
y1 = random.Next(image.Height);
y2 = random.Next(image.Height);
graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold |FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
graphic.DrawString(checkCode, font, brush, 2, 2);
int x = 0;
int y = 0;
//画图片的前景噪音点
for(int i=0; i<100; i++)
{
x = random.Next(image.Width);
y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//将图片验证码保存为流Stream返回
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms;
}
finally
{
graphic.Dispose();
image.Dispose();
}
} 在HttpHander的ProcessRequest中进行调用:?pupublic void ProcessRequest(HttpContext context)
{
string code = MakeCode(5);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
MemoryStream ms = CreateCodeImg(code);
if (null != ms)
{
context.Response.BinaryWrite(ms.ToArray());
}
}