[转载]c#的验证码代码(能直接用)
2011-02-27 22:41
225 查看
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
public partial class : System.Web.UI.Page//CheckCode是网页名字,另外,你要弄一个图片控件,将它的地址设为这个网页的名字。
{
// 验证码长度
private int codeLen = 4;
// 图片清晰度
private int fineness = 100;
// 图片宽度
private int imgWidth = 100;
// 图片高度
private int imgHeight = 24;
// 字体家族名称
private string fontFamily = "Arial Black";
// 字体大小
private int fontSize = 18;
// 字体样式
private int fontStyle = 0;
// 绘制起始坐标 X
private int posX = 0;
// 绘制起始坐标 Y
private int posY = 0;
//------------------------------------------------------------
// code.aspx 页面加载函数
//------------------------------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
#region 读取 Request 传递参数
// 获取代码长度设置
if (Request["CodeLen"] != null)
{
try
{
codeLen = Int32.Parse(Request["CodeLen"]);
// 规定验证码长度
if (codeLen < 4 || codeLen > 16)
throw new Exception("验证码长度必须在4到16之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片清晰度设置
if (Request["Fineness"] != null)
{
try
{
fineness = Int32.Parse(Request["Fineness"]);
// 验证清晰度
if (fineness < 0 || fineness > 100)
throw new Exception("图片清晰度必须在0到100之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片宽度
if (Request["ImgWidth"] != null)
{
try
{
imgWidth = Int32.Parse(Request["ImgWidth"]);
if (imgWidth < 16 || imgWidth > 480)
throw new Exception("图片宽度必须在16到480之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片高度
if (Request["ImgHeight"] != null)
{
try
{
imgHeight = Int32.Parse(Request["ImgHeight"]);
if (imgHeight < 16 || imgHeight > 320)
throw new Exception("图片高度必须在16到320之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取验证码字体家族名称
if (Request["FontFamily"] != null)
{
fontFamily = Request["FontFamily"];
}
// 获取验证码字体大小
if (Request["FontSize"] != null)
{
try
{
fontSize = Int32.Parse(Request["FontSize"]);
if (fontSize < 8 || fontSize > 72)
throw new Exception("字体大小必须在8到72之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取字体样式
if (Request["FontStyle"] != null)
{
try
{
fontStyle = Int32.Parse(Request["FontStyle"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
// 验证码绘制起始位置 X
if (Request["PosX"] != null)
{
try
{
posX = Int32.Parse(Request["PosX"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
// 验证码绘制起始位置 Y
if (Request["PosY"] != null)
{
try
{
posY = Int32.Parse(Request["PosY"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
#endregion
string code = Createcode();
// 生成BITMAP图像
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);
// 给图像设置干扰
DisturbBitmap(bitmap);
// 绘制验证码图像
Drawcode(bitmap, code);
// 保存验证码图像,等待输出
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
}
//------------------------------------------------------------
// 随机生成验证码,并保存到SESSION中
//------------------------------------------------------------
private string Createcode()
{
string code = "";
// 随机数对象
Random random = new Random();
for (int i = 0; i < codeLen; i++)
{
// 26: a - z
int n = random.Next(26);
// 将数字转换成大写字母
code += (char)(n + 65);
}
// 保存验证码
Session["code"] = code;
return code;
}
//------------------------------------------------------------
// 为图片设置干扰点
//------------------------------------------------------------
private void DisturbBitmap(Bitmap bitmap)
{
// 通过随机数生成
Random random = new Random();
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
if (random.Next(100) <= this.fineness)
bitmap.SetPixel(i, j, Color.White);
}
}
}
//------------------------------------------------------------
// 绘制验证码图片
//------------------------------------------------------------
private void Drawcode(Bitmap bitmap, string code)
{
// 获取绘制器对象
Graphics g = Graphics.FromImage(bitmap);
// 设置绘制字体
Font font = new Font(fontFamily, fontSize, GetFontStyle());
Pen myPen = new Pen(Color.Black,2);
//随机画两条线
Random r = new Random();
float x1 = r.Next(imgWidth);
float x2 = r.Next(imgWidth) ;
float y1 = r.Next(imgHeight);
float y2 = r.Next(imgHeight);
g.DrawLine(myPen, x1, y1, x2, y2);
x1 = r.Next(imgWidth );
x2 = r.Next(imgWidth);
y1 = r.Next(imgHeight);
y2 = r.Next(imgHeight);
g.DrawLine(myPen, x1, y1, x2, y2);
// 绘制验证码图像
g.DrawString(code, font, Brushes.Black, posX, posY);
}
//------------------------------------------------------------
// 换算验证码字体样式:1 粗体 2 斜体 3 粗斜体,默认为普通字体
//------------------------------------------------------------
private FontStyle GetFontStyle()
{
if (fontStyle == 1)
return FontStyle.Bold;
else if (fontStyle == 2)
return FontStyle.Italic;
else if (fontStyle == 3)
return FontStyle.Bold | FontStyle.Italic;
else
return FontStyle.Regular;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
public partial class : System.Web.UI.Page//CheckCode是网页名字,另外,你要弄一个图片控件,将它的地址设为这个网页的名字。
{
// 验证码长度
private int codeLen = 4;
// 图片清晰度
private int fineness = 100;
// 图片宽度
private int imgWidth = 100;
// 图片高度
private int imgHeight = 24;
// 字体家族名称
private string fontFamily = "Arial Black";
// 字体大小
private int fontSize = 18;
// 字体样式
private int fontStyle = 0;
// 绘制起始坐标 X
private int posX = 0;
// 绘制起始坐标 Y
private int posY = 0;
//------------------------------------------------------------
// code.aspx 页面加载函数
//------------------------------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
#region 读取 Request 传递参数
// 获取代码长度设置
if (Request["CodeLen"] != null)
{
try
{
codeLen = Int32.Parse(Request["CodeLen"]);
// 规定验证码长度
if (codeLen < 4 || codeLen > 16)
throw new Exception("验证码长度必须在4到16之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片清晰度设置
if (Request["Fineness"] != null)
{
try
{
fineness = Int32.Parse(Request["Fineness"]);
// 验证清晰度
if (fineness < 0 || fineness > 100)
throw new Exception("图片清晰度必须在0到100之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片宽度
if (Request["ImgWidth"] != null)
{
try
{
imgWidth = Int32.Parse(Request["ImgWidth"]);
if (imgWidth < 16 || imgWidth > 480)
throw new Exception("图片宽度必须在16到480之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取图片高度
if (Request["ImgHeight"] != null)
{
try
{
imgHeight = Int32.Parse(Request["ImgHeight"]);
if (imgHeight < 16 || imgHeight > 320)
throw new Exception("图片高度必须在16到320之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取验证码字体家族名称
if (Request["FontFamily"] != null)
{
fontFamily = Request["FontFamily"];
}
// 获取验证码字体大小
if (Request["FontSize"] != null)
{
try
{
fontSize = Int32.Parse(Request["FontSize"]);
if (fontSize < 8 || fontSize > 72)
throw new Exception("字体大小必须在8到72之间");
}
catch (Exception Ex)
{
throw Ex;
}
}
// 获取字体样式
if (Request["FontStyle"] != null)
{
try
{
fontStyle = Int32.Parse(Request["FontStyle"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
// 验证码绘制起始位置 X
if (Request["PosX"] != null)
{
try
{
posX = Int32.Parse(Request["PosX"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
// 验证码绘制起始位置 Y
if (Request["PosY"] != null)
{
try
{
posY = Int32.Parse(Request["PosY"]);
}
catch (Exception Ex)
{
throw Ex;
}
}
#endregion
string code = Createcode();
// 生成BITMAP图像
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);
// 给图像设置干扰
DisturbBitmap(bitmap);
// 绘制验证码图像
Drawcode(bitmap, code);
// 保存验证码图像,等待输出
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
}
//------------------------------------------------------------
// 随机生成验证码,并保存到SESSION中
//------------------------------------------------------------
private string Createcode()
{
string code = "";
// 随机数对象
Random random = new Random();
for (int i = 0; i < codeLen; i++)
{
// 26: a - z
int n = random.Next(26);
// 将数字转换成大写字母
code += (char)(n + 65);
}
// 保存验证码
Session["code"] = code;
return code;
}
//------------------------------------------------------------
// 为图片设置干扰点
//------------------------------------------------------------
private void DisturbBitmap(Bitmap bitmap)
{
// 通过随机数生成
Random random = new Random();
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
if (random.Next(100) <= this.fineness)
bitmap.SetPixel(i, j, Color.White);
}
}
}
//------------------------------------------------------------
// 绘制验证码图片
//------------------------------------------------------------
private void Drawcode(Bitmap bitmap, string code)
{
// 获取绘制器对象
Graphics g = Graphics.FromImage(bitmap);
// 设置绘制字体
Font font = new Font(fontFamily, fontSize, GetFontStyle());
Pen myPen = new Pen(Color.Black,2);
//随机画两条线
Random r = new Random();
float x1 = r.Next(imgWidth);
float x2 = r.Next(imgWidth) ;
float y1 = r.Next(imgHeight);
float y2 = r.Next(imgHeight);
g.DrawLine(myPen, x1, y1, x2, y2);
x1 = r.Next(imgWidth );
x2 = r.Next(imgWidth);
y1 = r.Next(imgHeight);
y2 = r.Next(imgHeight);
g.DrawLine(myPen, x1, y1, x2, y2);
// 绘制验证码图像
g.DrawString(code, font, Brushes.Black, posX, posY);
}
//------------------------------------------------------------
// 换算验证码字体样式:1 粗体 2 斜体 3 粗斜体,默认为普通字体
//------------------------------------------------------------
private FontStyle GetFontStyle()
{
if (fontStyle == 1)
return FontStyle.Bold;
else if (fontStyle == 2)
return FontStyle.Italic;
else if (fontStyle == 3)
return FontStyle.Bold | FontStyle.Italic;
else
return FontStyle.Regular;
相关文章推荐
- [转载]c#的验证码代码(能直接用)
- 客户端通过C# 代码直接从服务器下载文件到本地
- 在C#代码中应用Log4Net系列教程(附源代码)【转载】
- C# 直接执行、调用本机代码、汇编代码 shell Native Code
- 转载:Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- C#获取网页中的验证码图片(转载)
- C# WORD操作实现代码(转载)
- [转载]获取html绝对坐标 屏蔽Ctrl+V 按回车键直接登录 js代码 .
- 很漂亮的按钮css样式(没有用到图片,可直接拷贝代码使用)转载
- c#中的反射代码实例讲解(转载)
- C# 里怎样得到当前执行的函数名,当前代码行,源代码文件名(转载)
- C#完整执行存储过程的代码加实例[转载]
- ASP.NET Doorstep之 在HTML中嵌入C#代码(转载)
- C#代码动态编译、动态执行、动态调试(转载)
- [转]去噪:用于验证码图片识别的类续(C#代码)
- C#指定用户执行程序的示例代码(转载)
- 转载c#代码:
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- C#.net 好用的验证码代码 汉字-变色-扭曲-波动
- C# 实现WinForm窗口最小化到系统托盘代码(转载)