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
;
}
}
查看全文
相关阅读:
基于短周期价量特征的多因子选股体系
OnePy--构建属于自己的量化回测框架
Should You Build Your Own Backtester?
针对Quant的Python快速入门指南
史上最全量化资源整理
数字货币量化分析报告[2018-01-31]
TuShare获取K线数据
理工科背景如何转行量化交易?
数字货币量化分析报告[2018-01-30]
Ta-lib K线模式识别
原文地址:https://www.cnblogs.com/gwazy/p/908198.html
最新文章
List集合去重的一种方法
浅谈 C#委托
Js模型和封装
kendo ui的treeView节点点击事件修改和grid的配置的一点总结
浅谈你感兴趣的 CLR GC 机制底层
Java JVM运行时数据区,内存管理和GC垃圾回收
关于ajax的同步和异步
EF更新,数据库值变化,前台页面并不变化,刷新也不变化,重新运行程序则变化----开发中遇到的问题(已解决)
关于EF的 序列化类型为“XXX”的对象时检测到循环引用。
多表insert操作详解
热门文章
对索引列的查询和排序问题的研究
排序分析函数中对null的处理
深入理解Oracle中的随机函数
动态SQL中不同变量的写法总结
彻底理解Oracle中的集合操作与复合查询
merge into的用法及10g新特性总结
EXISTS的使用详解
深入理解null的原理
Oracle中case用法总结
经典技术指标
Copyright © 2011-2022 走看看