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设计模式之单例模式
sql查询优化整理
MYSQL 调优学习笔记
记一次失败的大厂面试
ElasticSearch 6.3.2 整合 Springboot 2.1.10.RELEASE 版本,使用 Logstash 导入 mysql 数据
ajax技术实现登录判断用户名是否重复以及利用xml实现二级下拉框联动
浅谈 KMP 算法
转载:Docker入门只需看这一篇就够了
Spring Boot 监听 Activemq 中的特定 topic ,并将数据通过 RabbitMq 发布出去
hadoop入门之海量Web日志分析 用Hadoop提取KPI统计指标
原文地址:https://www.cnblogs.com/studio313/p/219469.html
最新文章
git命令-切换分支
PHP工程师必备知识整理
股票涨跌预测方法之四:实际预测
股票涨跌预测方法之三:建立模型并训练
股票涨跌预测方法之二:股票技术指标计算
股票涨跌预测方法之一:下载股票数据
深度学习结合非局部均值滤波的图像去噪算法
非局部均值滤波算法的python实现
gabor变换人脸识别的python实现,att_faces数据集平均识别率99%
LBP人脸识别的python实现
热门文章
PCA人脸识别的python实现
自动色彩均衡(ACE)快速算法
centos7 rpm 安装mysql
centos7安装python3和pip3
微信小程序开发流程
MySQL索引及查询优化总结
maven私服nexus搭建(windows)
CAS单点登录服务器搭建
Java多线程
微信公众号开发前期准备工作
Copyright © 2011-2022 走看看