zoukankan
html css js c++ java
ASP.net 验证码(C#)
public
class
ValidateCode : System.Web.UI.Page
2
{
3
private
void
Page_Load(
object
sender, System.EventArgs e)
4
{
5
this
.CreateCheckCodeImage(GenerateCheckCode());
6
}
7
8
web 窗体设计器生成的代码
web 窗体设计器生成的代码
#region
web 窗体设计器生成的代码
9
override
protected
void
OnInit(EventArgs e)
10
{
11
//
12
//
CODEGEN: 该调用是 asp.NET web 窗体设计器所必需的。
13
//
14
InitializeComponent();
15
base
.OnInit(e);
16
}
17
18
/**/
/**/
/**/
///
<summary>
19
/**/
///
设计器支持所需的方法 - 不要使用代码编辑器修改
20
/**/
///
此方法的内容。
21
/**/
///
</summary>
22
private
void
InitializeComponent()
23
{
24
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
25
}
26
#endregion
27
28
private
string
GenerateCheckCode()
29
{
30
int
number;
31
char
code;
32
string
checkCode
=
String.Empty;
33
34
System.Random random
=
new
Random();
35
36
for
(
int
i
=
0
; i
<
5
; i
++
)
37
{
38
number
=
random.Next();
39
40
if
(number
%
2
==
0
)
41
code
=
(
char
)(
'
0
'
+
(
char
)(number
%
10
));
42
else
43
code
=
(
char
)(
'
A
'
+
(
char
)(number
%
26
));
44
45
checkCode
+=
code.ToString();
46
}
47
48
Response.Cookies.Add(
new
HttpCookie(
"
CheckCode
"
, checkCode));
49
50
return
checkCode;
51
}
52
53
private
void
CreateCheckCodeImage(
string
checkCode)
54
{
55
if
(checkCode
==
null
||
checkCode.Trim()
==
String.Empty)
56
return
;
57
58
System.Drawing.Bitmap image
=
new
System.Drawing.Bitmap((
int
)Math.Ceiling((checkCode.Length
*
12.5
)),
22
);
59
Graphics g
=
Graphics.FromImage(image);
60
61
try
62
{
63
//
生成随机生成器
64
Random random
=
new
Random();
65
66
//
清空图片背景色
67
g.Clear(Color.White);
68
69
//
画图片的背景噪音线
70
for
(
int
i
=
0
; i
<
25
; i
++
)
71
{
72
int
x1
=
random.Next(image.Width);
73
int
x2
=
random.Next(image.Width);
74
int
y1
=
random.Next(image.Height);
75
int
y2
=
random.Next(image.Height);
76
77
g.DrawLine(
new
Pen(Color.Silver), x1, y1, x2, y2);
78
}
79
80
Font font
=
new
System.Drawing.Font(
"
Arial
"
,
12
, (System.Drawing.FontStyle.Bold
|
System.Drawing.FontStyle.Italic));
81
System.Drawing.Drawing2D.LinearGradientBrush brush
=
new
System.Drawing.Drawing2D.LinearGradientBrush(
new
Rectangle(
0
,
0
, image.Width, image.Height), Color.Blue, Color.DarkRed,
1.2f
,
true
);
82
g.DrawString(checkCode, font, brush,
2
,
2
);
83
84
//
画图片的前景噪音点
85
for
(
int
i
=
0
; i
<
100
; i
++
)
86
{
87
int
x
=
random.Next(image.Width);
88
int
y
=
random.Next(image.Height);
89
90
image.SetPixel(x, y, Color.FromArgb(random.Next()));
91
}
92
93
//
画图片的边框线
94
g.DrawRectangle(
new
Pen(Color.Silver),
0
,
0
, image.Width
-
1
, image.Height
-
1
);
95
96
System.IO.MemoryStream ms
=
new
System.IO.MemoryStream();
97
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
98
Response.ClearContent();
99
Response.ContentType
=
"
image/Gif
"
;
100
Response.BinaryWrite(ms.ToArray());
101
}
102
finally
103
{
104
g.Dispose();
105
image.Dispose();
106
}
107
}
查看全文
相关阅读:
JAVA LinkedList和ArrayList的使用及性能分析
学习笔记—Node中的模块调试
学习笔记—Node的核心模块
学习笔记—Node中VM模块详解
学习笔记—Node中require的实现
入园了
【引用】asp.net服务器推送(ServerPush)和客户端拉拽技术
ajax xmlHttp.responseXML取不到值问题备忘
oracle实时插值速度突然变慢问题解决办法
[转帖 作者: fuyuncat 来源: www.HelloDBA.com ]Oracle IO问题解析
原文地址:https://www.cnblogs.com/studio313/p/219469.html
最新文章
二叉树的遍历
Python基础——简介
Python基础——安装运行
python基础——字符串
python基础——数字&集合&布尔类型
CSS隐藏与替换文字
CSS 文本域和按钮对齐
关于列表
有关css字体的事
HTML表单
热门文章
关于列表
怎样对齐文体框和图像按钮
关于清浮动
CSS字体2
a标签里面加title标签的作用
NSArray
NSString
NSMutableString
List的遍历 Java
ios 快捷键
Copyright © 2011-2022 走看看