ASP.NET软件开发_ 旋转的可视验证码图象_源码
来源:互联网 发布:java复制文件并重命名 编辑:程序博客网 时间:2024/05/18 12:35
/// <summary>
/// 旋转的可视验证码图象
/// </summary>
public class VlidationCode
{
public enum RandomStringMode
{
/// <summary>
/// 小写字母
/// </summary>
LowerLetter,
/// <summary>
/// 大写字母
/// </summary>
UpperLetter,
/// <summary>
/// 混合大小写字母
/// </summary>
Letter,
/// <summary>
/// 数字
/// </summary>
Digital,
/// <summary>
/// 混合数字与大小字母
/// </summary>
Mix
}
public static string GenerateRandomString(int length, RandomStringMode mode)
{
string rndStr = string.Empty;
if (length == 0)
return rndStr;
//以数组方式候选字符,可以更方便的剔除不要的字符,如数字 0 与字母 o
char[] digitals = new char[10] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char[] lowerLetters = new char[26] {
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z' };
char[] upperLetters = new char[26] {
'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z' };
char[] letters = new char[52]{
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z' };
char[] mix = new char[62]{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z' };
int[] range = new int[2] { 0, 0 };
Random random = new Random();
switch (mode)
{
case RandomStringMode.Digital:
for (int i = 0; i < length; ++i)
rndStr += digitals[random.Next(0, digitals.Length)];
break;
case RandomStringMode.LowerLetter:
for (int i = 0; i < length; ++i)
rndStr += lowerLetters[random.Next(0, lowerLetters.Length)];
break;
case RandomStringMode.UpperLetter:
for (int i = 0; i < length; ++i)
rndStr += upperLetters[random.Next(0, upperLetters.Length)];
break;
case RandomStringMode.Letter:
for (int i = 0; i < length; ++i)
rndStr += letters[random.Next(0, letters.Length)];
break;
default:
for (int i = 0; i < length; ++i)
rndStr += mix[random.Next(0, mix.Length)];
break;
}
return rndStr;
}
public static void ShowCode(int i)
{
ShowValidationCode(ref i, 4, 20, RandomStringMode.UpperLetter, Color.Gold, Color.WhiteSmoke);
}
/// <summary>
/// 显示验证码
/// </summary>
/// <param name="seed">随机数辅助种子</param>
/// <param name="strLen">验证码字符长度</param>
/// <param name="fontSize">字体大小</param>
/// <param name="mode">随机字符模式</param>
/// <param name="clrFont">字体颜色</param>
/// <param name="clrBg">背景颜色</param>
public static void ShowValidationCode(ref int seed, int strLen, int fontSize, RandomStringMode mode, Color clrFont, Color clrBg)
{
int tmpSeed;
unchecked
{
tmpSeed = (int)(seed * DateTime.Now.Ticks);
++seed;
}
Random rnd = new Random(tmpSeed);
string text = GenerateRandomString(strLen, mode);
int height = fontSize * 2;
// 因为字体旋转后每个字体所占宽度会所有加大,所以要加一点补偿宽度
int width = fontSize * text.Length + fontSize / (text.Length - 2);
Bitmap bmp = new Bitmap(width, height);
Graphics graphics = Graphics.FromImage(bmp);
Font font = new Font("Courier New", fontSize, FontStyle.Bold);
Brush brush = new SolidBrush(clrFont);
Brush brushBg = new SolidBrush(clrBg);
graphics.FillRectangle(brushBg, 0, 0, width, height);
Bitmap tmpBmp = new Bitmap(height, height);
Graphics tmpGph = null;
int degree = 40;
Point tmpPoint = new Point();
for (int i = 0; i < text.Length; i++)
{
tmpBmp = new Bitmap(height, height);
tmpGph = Graphics.FromImage(tmpBmp);
// tmpGph.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
// 不填充底色,文字 ClearType 效果不见了,why?!
// tmpGph.FillRectangle(brushBg, 0, 0, tmpBmp.Width, tmpBmp.Height);
degree = rnd.Next(20, 51); // [20, 50]随机角度
if (rnd.Next(0, 2) == 0)
{
tmpPoint.X = 12; // 调整文本坐标以适应旋转后的图象
tmpPoint.Y = -6;
}
else
{
degree = ~degree + 1; // 逆时针旋转
tmpPoint.X = -10;
tmpPoint.Y = 6;
}
tmpGph.RotateTransform(degree);
tmpGph.DrawString(text[i].ToString(), font, brush, tmpPoint);
graphics.DrawImage(tmpBmp, i * fontSize, 0); // 拼接图象
}
//输出图象
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
bmp.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/gif";
HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());
HttpContext.Current.Response.End();
//释放资源
font.Dispose();
brush.Dispose();
brushBg.Dispose();
tmpGph.Dispose();
tmpBmp.Dispose();
graphics.Dispose();
bmp.Dispose();
memoryStream.Dispose();
}
}
- ASP.NET软件开发_ 旋转的可视验证码图象_源码
- asp.net 验证码_无噪点
- asp.net 验证码_彩色
- 如何在C#的WinForm中制作饼状图和柱状图_C#_.Net开发_软件开发_天新网
- 步步为营_Android开发课_番外篇[5]_软件的安装与卸载源码
- C#中通过DataGrid显示隶属关系_C#_.Net开发_软件开发_天新网
- C#中实现DataGrid双向排序_C#_.Net开发_软件开发_天新网
- Visual C#中调用Windows服务初探_C#_.Net开发_软件开发_天新网
- ASP图象组件shotgraph生成图片验证码的代码
- ASP图象组件shotgraph生成图片验证码的代码
- 什么是软件开发?(^_^)
- ASP.NET重定向_第三篇_调用 ASP.NET 网页的方式
- DEMO 源码系列_旋转星云
- spring开发_邮箱注册_激活_获取验证码
- ASP.NET WEB开发规范_控件命名规范
- 彩色的ASP.NET验证码组件[源码]
- 图象旋转的笔记
- asp.net图象统计
- getch()和getche() getchar()
- WinCE中打印函数总结
- CronTrigger教程
- tomcat 识别 web 工程
- Oracle行列互换 横表和纵表
- ASP.NET软件开发_ 旋转的可视验证码图象_源码
- android控件之Textview
- android sdk安装问题
- MLGBD 从今天开始起,不偷懒了。
- 挖潜无极限---数据挖掘技术与应用热点扫描
- opengl 打开和关闭垂直同步方法
- 辞职也需要辞得帅,辞得大家都开心,多为将来考虑,辞职不要急,本是好事要办好
- java多线程与并发之java线程简介(一)
- 如何减少编码过程中的错误