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
;
}
}
查看全文
相关阅读:
正则表达式全部符号解释
如何在开机启动时自动打开小键盘灯
SQL SERver2005中row_number() 的用法
用命令打开控制面板的各项
SQL Server 2008 对 TSQL 语言的增强
SQL2000DBCC DBREINDEX重建索引提高SQL Server性能
Sql Server 中常用的字符串函数
coalesce的用法
word to PD R qm addin 运行时错误‘91’
Windows系统:开始–运行–命令大全
原文地址:https://www.cnblogs.com/gwazy/p/908198.html
最新文章
JQuery跑马灯(文字无缝向上翻动)(自已封装的第三个插件)
strong vs weak
C#中隐式类型变量和匿名类型的区别
Python基础教程读书笔记(第15章—第16章:Python和万维网;测试)
在Windows系统下设置Python的环境变量,以及安装BeautifulSoup
PHP操作sqlite类
Ubuntu下使用SVN
ubuntu安装phpcurl拓展
Python基础教程读书笔记(第17章—第19章:扩展Python;程序打包;好玩的编程)
ubuntu重启apache2服务报错
热门文章
MySql与SqlServer的一些常用用法的差别(转)
Python Challenge 0
python删除文件
Python strip lstrip rstrip使用方法
VIM常用小技巧
Python Challenge 4
Python Challenge 1
使用Python的MD5模块比较两个文件是否一致
Python Challenge 3
Python Challenge 2
Copyright © 2011-2022 走看看