zoukankan
html css js c++ java
验证码控件
自定义控件部分
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Text;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Drawing;
using
System.IO;
using
System.Drawing.Imaging;
namespace
CheckImageLib
{
[DefaultProperty(
"
Text
"
)]
[ToolboxData(
"
<{0}:CheckImages runat=server></{0}:CheckImages>
"
)]
public
class
CheckImages : WebControl
{
[Bindable(
true
)]
[Category(
"
Appearance
"
)]
[DefaultValue(
""
)]
[Localizable(
true
)]
public
string
Text
{
get
{
String s
=
(String)ViewState[
"
Text
"
];
return
((s
==
null
)
?
String.Empty : s);
}
set
{
ViewState[
"
Text
"
]
=
value;
}
}
protected
override
void
Render(HtmlTextWriter output)
{
output.Write(
this
.CreateCheckCodeImage(GenerateCheckCode()));
//
output.Write("");
}
private
string
GenerateCheckCode()
{
int
number;
char
code;
string
checkCode
=
String.Empty;
System.Random random
=
new
Random();
for
(
int
i
=
0
; i
<
5
; i
++
)
{
number
=
random.Next();
if
(number
%
2
==
0
)
code
=
(
char
)(
'
0
'
+
(
char
)(number
%
10
));
else
code
=
(
char
)(
'
A
'
+
(
char
)(number
%
26
));
checkCode
+=
code.ToString();
}
HttpContext.Current.Response.Cookies.Add(
new
HttpCookie(
"
CheckCode
"
, checkCode));
return
checkCode;
}
private
string
CreateCheckCodeImage(
string
checkCode)
{
if
(checkCode
==
null
||
checkCode.Trim()
==
String.Empty)
return
""
;
System.Drawing.Bitmap bm
=
new
System.Drawing.Bitmap((
int
)Math.Ceiling((checkCode.Length
*
12.5
)),
22
);
Graphics bp
=
Graphics.FromImage(bm);
//
生成随机生成器
Random random
=
new
Random();
//
清空图片背景色
bp.Clear(Color.White);
//
画图片的背景噪音线
for
(
int
i
=
0
; i
<
25
; i
++
)
{
int
x1
=
random.Next(bm.Width);
int
x2
=
random.Next(bm.Width);
int
y1
=
random.Next(bm.Height);
int
y2
=
random.Next(bm.Height);
bp.DrawLine(
new
Pen(Color.Silver), x1, y1, x2, y2);
}
Font font
=
new
System.Drawing.Font(
"
Arial
"
,
12
, (System.Drawing.FontStyle.Bold
|
System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush
=
new
System.Drawing.Drawing2D.LinearGradientBrush(
new
Rectangle(
0
,
0
, bm.Width, bm.Height), Color.Blue, Color.DarkRed,
1.2f
,
true
);
bp.DrawString(checkCode, font, brush,
2
,
2
);
//
画图片的前景噪音点
for
(
int
i
=
0
; i
<
100
; i
++
)
{
int
x
=
random.Next(bm.Width);
int
y
=
random.Next(bm.Height);
bm.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//
画图片的边框线
bp.DrawRectangle(
new
Pen(Color.Silver),
0
,
0
, bm.Width
-
1
, bm.Height
-
1
);
FileStream fs
=
new
FileStream(Page.Server.MapPath(Page.Request.Url.AbsolutePath.Replace(
"
.aspx
"
,
"
.jpg
"
)), FileMode.Create);
bm.Save(fs, ImageFormat.Jpeg);
bm.Dispose();
bp.Dispose();
fs.Close();
return
"
<img src=
"
+
Page.Request.Url.AbsolutePath.Replace(
"
.aspx
"
,
"
.jpg
"
)
+
"
></img>
"
;
}
}
}
调用部分
<
form
id
="form1"
runat
="server"
>
<
div
>
<
asp:TextBox
ID
="txtCheckCode"
runat
="server"
></
asp:TextBox
>
<
asp:Label
ID
="lblMessage"
runat
="server"
></
asp:Label
></
div
>
<
asp:Button
ID
="Button1"
runat
="server"
OnClick
="Button1_Click"
Text
="Button"
/>
<
table
>
<
tr
>
<
td
style
=" 7px"
>
<
cc1:CheckImages
ID
="CheckImages1"
runat
="server"
/>
</
td
>
</
tr
>
</
table
>
</
form
>
protected
void
Button1_Click(
object
sender, EventArgs e)
{
if
(Request.Cookies[
"
CheckCode
"
]
==
null
)
{
lblMessage.Text
=
"
您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。
"
;
lblMessage.Visible
=
true
;
return
;
}
if
(String.Compare(Request.Cookies[
"
CheckCode
"
].Value, txtCheckCode.Text,
true
)
!=
0
)
{
lblMessage.Text
=
"
验证码错误,请输入正确的验证码。
"
;
lblMessage.Visible
=
true
;
return
;
}
}
查看全文
相关阅读:
token的时限多长才合适?
WebFTP安装说明
维度表和事实表的区别
互联网产品mysql数据库设计总结
网络的介数中心性(betweenness)及计算方法
python中的编码与解码
增强学习Reinforcement Learning经典算法梳理3:TD方法
Mybatis 参考
防御CSRF、XSS和SQL注入攻击
转:PriorityQueue
原文地址:https://www.cnblogs.com/gwazy/p/908198.html
最新文章
Deep Learning基础--26种神经网络激活函数可视化
Deep Learning基础--参数优化方法
机器学习、深度学习的理论与实战入门建议整理
CNN中千奇百怪的卷积方式大汇总
拉格朗日乘数法
深度解读最流行的优化算法:梯度下降(精简版)
深度解读最流行的优化算法:梯度下降
常见的几种最优化方法(梯度下降法、牛顿法、拟牛顿法、共轭梯度法等)
数据结构中的各种树浅谈
最小生成树-Prim算法和Kruskal算法
热门文章
算法题之找出数组里第K大的数
算法题之Median of Two Sorted Arrays
路径规划常见算法小结
在Github里集成Readthedocs服务
IIS备份和还原
记录对定时任务调度器的小小改进
开源一个windows消息队列查看器
开源一个定时任务调度器 webscheduler
文件导出也可以这么写【js+blob】
Treesoft数据库管理系统使用说明
Copyright © 2011-2022 走看看