TinyMind
.net.cn
首页
资讯
文章
注册
登录
VS2005中验证码生成页
mbskys
关注文章
TinyMind专栏频道
<%
@PageLanguage
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
code.aspx.cs
"
Inherits
=
"
code
"
%>
<!
DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
鏃犳爣棰橀〉
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
</
div
>
</
form
>
</
body
>
</
html
>
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Drawing.Imaging;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
/**/
///
<summary>
///
code验证码生成页
///
</summary>
public
partial
class
code:System.Web.UI.Page
{
//
验证码长度
private
int
codeLen
=
8
;
//
图片清晰度
private
int
fineness
=
80
;
//
图片宽度
private
int
imgWidth
=
128
;
//
图片高度
private
int
imgHeight
=
24
;
//
字体家族名称
private
string
fontFamily
=
"
TimesNewRoman
"
;
//
字体大小
private
int
fontSize
=
14
;
//
字体样式
private
int
fontStyle
=
0
;
//
绘制起始坐标X
private
int
posX
=
0
;
//
绘制起始坐标Y
private
int
posY
=
0
;
//
------------------------------------------------------------
//
code.aspx页面加载函数
//
------------------------------------------------------------
private
void
Page_Load(
object
sender,System.EventArgse)
{
读取Request传递参数
#region
读取Request传递参数
//
获取代码长度设置
if
(Request[
"
CodeLen
"
]
!=
null
)
{
try
{
codeLen
=
Int32.Parse(Request[
"
CodeLen
"
]);
//
规定验证码长度
if
(codeLen
<
4
||
codeLen
>
16
)
throw
new
Exception(
"
验证码长度必须在4到16之间
"
);
}
catch
(ExceptionEx)
{
throw
Ex;
}
}
//
获取图片清晰度设置
if
(Request[
"
Fineness
"
]
!=
null
)
{
try
{
fineness
=
Int32.Parse(Request[
"
Fineness
"
]);
//
验证清晰度
if
(fineness
<
0
||
fineness
>
100
)
throw
new
Exception(
"
图片清晰度必须在0到100之间
"
);
}
catch
(ExceptionEx)
{
throw
Ex;
}
}
//
获取图片宽度
if
(Request[
"
ImgWidth
"
]
!=
null
)
{
try
{
imgWidth
=
Int32.Parse(Request[
"
ImgWidth
"
]);
if
(imgWidth
<
16
||
imgWidth
>
480
)
throw
new
Exception(
"
图片宽度必须在16到480之间
"
);
}
catch
(ExceptionEx)
{
throw
Ex;
}
}
//
获取图片高度
if
(Request[
"
ImgHeight
"
]
!=
null
)
{
try
{
imgHeight
=
Int32.Parse(Request[
"
ImgHeight
"
]);
if
(imgHeight
<
16
||
imgHeight
>
320
)
throw
new
Exception(
"
图片高度必须在16到320之间
"
);
}
catch
(ExceptionEx)
{
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
(ExceptionEx)
{
throw
Ex;
}
}
//
获取字体样式
if
(Request[
"
FontStyle
"
]
!=
null
)
{
try
{
fontStyle
=
Int32.Parse(Request[
"
FontStyle
"
]);
}
catch
(ExceptionEx)
{
throw
Ex;
}
}
//
验证码绘制起始位置X
if
(Request[
"
PosX
"
]
!=
null
)
{
try
{
posX
=
Int32.Parse(Request[
"
PosX
"
]);
}
catch
(ExceptionEx)
{
throw
Ex;
}
}
//
验证码绘制起始位置Y
if
(Request[
"
PosY
"
]
!=
null
)
{
try
{
posY
=
Int32.Parse(Request[
"
PosY
"
]);
}
catch
(ExceptionEx)
{
throw
Ex;
}
}
#endregion
string
code
=
Createcode();
//
生成BITMAP图像
Bitmapbitmap
=
new
Bitmap(imgWidth,imgHeight);
//
给图像设置干扰
DisturbBitmap(bitmap);
//
绘制验证码图像
Drawcode(bitmap,code);
//
保存验证码图像,等待输出
bitmap.Save(Response.OutputStream,ImageFormat.Gif);
}
//
------------------------------------------------------------
//
随机生成验证码,并保存到SESSION中
//
------------------------------------------------------------
private
string
Createcode()
{
string
code
=
""
;
//
随机数对象
Randomrandom
=
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(Bitmapbitmap)
{
//
通过随机数生成
Randomrandom
=
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(Bitmapbitmap,
string
code)
{
//
获取绘制器对象
Graphicsg
=
Graphics.FromImage(bitmap);
//
设置绘制字体
Fontfont
=
new
Font(fontFamily,fontSize,GetFontStyle());
PenmyPen
=
new
Pen(Color.Black,
2
);
//
随机画两条线
Randomr
=
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
FontStyleGetFontStyle()
{
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;
}
}
优质内容筛选与推荐
>>
1、类与对象 动手动脑
2、something about the SV_POSITION
3、iOS image processing with the accelerate framework(透明玻璃效果)
4、ThinkPHP CURD方法盘点:data方法
5、tf.pad 用法说明
赞赏
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。
阅读
好看
已推荐到看一看
你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
取消
推荐
我知道了
已取消,“好看”想法已同步删除
知道了
已推荐到看一看
和朋友分享想法
最多200字,当前共
字
发送
已发送
朋友将在看一看看到
确定
分享你的想法...
取消
分享想法到看一看
确定
最多200字,当前共
字
发送中
网络异常,请稍后重试
知道了
微信扫一扫
关注该公众号
0
│
收藏
│
举报