zoukankan
html css js c++ java
ValidateBox验证码控件原代码
今天想了想,还是把这个源代码发布出来。
//
********************************************************
//
验证码验证控件
//
Designed by Faib Studio.
//
Copyright 2007
//
Email faib920@126.com or QQ 55570729
//
********************************************************
using
System;
using
System.Collections.Specialized;
using
System.ComponentModel;
using
System.ComponentModel.Design;
using
System.Drawing;
using
System.Drawing.Drawing2D;
using
System.Drawing.Imaging;
using
System.Drawing.Design;
using
System.IO;
using
System.Text;
using
System.Reflection;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Globalization;
namespace
FaibClass.WebControls
{
[DefaultProperty(
"
Text
"
)]
[ToolboxData(
"
<{0}:ValidateBox Runat=Server Font-Name=\
"
System\
"
Font-Size=\
"
9pt\
"
></{0}:ValidateBox>
"
)]
public
class
ValidateBox : System.Web.UI.WebControls.Image
{
私有变量
#region
私有变量
private
int
m_Length
=
4
;
private
string
m_newImage
=
""
;
private
string
m_ValidateKey
=
""
;
private
float
m_TextAngle
=
0
;
private
bool
m_IsLaod
=
false
;
private
FontInfo m_Font;
private
ValidateStyle m_ReturnStyle
=
ValidateStyle.Client;
private
ValidateCodeType m_ValidateCodeType
=
ValidateCodeType.Commix;
private
string
m_CustomCodes;
#endregion
public
ValidateBox()
{
m_Font
=
new
Style().Font;
m_Font.Name
=
"
System
"
;
m_Font.Size
=
9
;
}
属性
#region
属性
/**/
///
<summary>
///
返回验证码。
///
</summary>
[Browsable(
false
)]
public
string
Text
{
get
{
if
(ViewState[
"
Text
"
]
==
null
)
return
""
;
else
return
ViewState[
"
Text
"
].ToString();
}
}
/**/
///
<summary>
///
验证码验证标识。
///
</summary>
[Category(
"
Behavior
"
)]
[Description(
"
验证码验证标识。
"
)]
public
string
ValidateKey
{
get
{
return
m_ValidateKey;}
set
{m_ValidateKey
=
value;}
}
/**/
///
<summary>
///
验证码产生类型。
///
</summary>
[Category(
"
Behavior
"
)]
[Description(
"
验证码产生类型。
"
)]
[DefaultValue(ValidateCodeType.Commix)]
public
ValidateCodeType ValidateType
{
get
{
return
m_ValidateCodeType;}
set
{m_ValidateCodeType
=
value;}
}
/**/
///
<summary>
///
验证码验证验证方式。
///
</summary>
[Category(
"
Behavior
"
)]
[Description(
"
验证码验证验证方式。
"
)]
[DefaultValue(ValidateStyle.Client)]
public
ValidateStyle ValidateStyle
{
get
{
return
m_ReturnStyle;}
set
{m_ReturnStyle
=
value;}
}
/**/
///
<summary>
///
设置或返回验证码字符的长度。
///
</summary>
[Category(
"
Appearance
"
)]
[Description(
"
设置或返回验证码字符的长度。
"
)]
[DefaultValue(
4
)]
public
int
Length
{
get
{
return
m_Length;}
set
{m_Length
=
value;}
}
/**/
///
<summary>
///
设置或返回字体。
///
</summary>
[Browsable(
true
)]
[Category(
"
Appearance
"
)]
[Description(
"
设置或返回字体。
"
)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public
new
FontInfo Font
{
get
{
return
m_Font;}
set
{m_Font
=
value;}
}
/**/
///
<summary>
///
设置或返回字体倾斜的角度。
///
</summary>
[Category(
"
Appearance
"
)]
[Description(
"
字体倾斜的角度。
"
)]
[DefaultValue(
typeof
(
float
),
"
0
"
)]
public
float
TextAngle
{
get
{
return
m_TextAngle;}
set
{m_TextAngle
=
value;}
}
/**/
///
<summary>
///
设置或返回字体倾斜的角度。
///
</summary>
[Category(
"
Appearance
"
)]
[Description(
"
自定义随机抽取的验证码列表。
"
)]
public
string
CustomCodes
{
get
{
return
m_CustomCodes;}
set
{m_CustomCodes
=
value;}
}
public
override
string
ImageUrl
{
get
{
return
base
.ImageUrl;}
set
{
base
.ImageUrl
=
value;
if
(
!
m_IsLaod)
{
ViewState[
"
OldImage
"
]
=
value;
}
}
}
#endregion
公共方法
#region
公共方法
protected
override
void
OnUnload(EventArgs e)
{
if
(m_ValidateKey
!=
null
&&
m_ValidateKey
!=
""
)
{
switch
(m_ReturnStyle)
{
case
ValidateStyle.Session:
Context.Session[m_ValidateKey]
=
ViewState[
"
Text
"
].ToString();
break
;
case
ValidateStyle.Cookie:
Context.Response.Cookies[m_ValidateKey].Value
=
ViewState[
"
Text
"
].ToString();
break
;
case
ValidateStyle.Cache:
Context.Cache[m_ValidateKey]
=
ViewState[
"
Text
"
].ToString();
break
;
}
}
base
.OnUnload (e);
}
protected
override
void
OnLoad(EventArgs e)
{
string
strCode
=
GetValidateCode();
ViewState[
"
Text
"
]
=
strCode;
BuildImage();
if
(m_ReturnStyle
==
ValidateStyle.Client)
{
this
.Attributes.Add(
"
Text
"
,
this
.Text);
this
.Attributes.Add(
"
value
"
,
this
.Text.ToLower());
}
this
.Style.Add(
"
cursor
"
,
"
hand
"
);
this
.Attributes.Add(
"
onclick
"
, Page.GetPostBackEventReference(
this
,
""
));
base
.OnLoad(e);
}
#endregion
私有方法
#region
私有方法
private
void
BuildImage()
{
bool
blnHasPic;
if
(ViewState[
"
OldImage
"
]
!=
null
)
{
blnHasPic
=
System.IO.File.Exists(Page.Server.MapPath(ViewState[
"
OldImage
"
].ToString()));
}
else
{
blnHasPic
=
false
;
}
m_newImage
=
this
.ClientID
+
"
.fbs.ashx
"
;
if
(
!
blnHasPic
&&
this
.Width.IsEmpty)
{
this
.Width
=
Unit.Pixel(
70
);
}
if
(
!
blnHasPic
&&
this
.Height.IsEmpty)
{
this
.Height
=
Unit.Pixel(
20
);
}
string
strCode
=
ViewState[
"
Text
"
].ToString();
string
strFName
=
"
System
"
;
float
floFSize
=
9
;
FontStyle fntstyFont
=
FontStyle.Regular;
if
(m_Font
!=
null
)
{
if
(m_Font.Name
!=
string
.Empty)
{
strFName
=
m_Font.Name;
}
//
如果使用是的是FontSize.Large等相对值应该怎么做
if
(m_Font.Size.Type
!=
FontSize.NotSet)
{
floFSize
=
(
float
)m_Font.Size.Unit.Value;
}
else
{
floFSize
=
9
;
}
if
(m_Font.Bold)
{
fntstyFont
|=
FontStyle.Bold;
}
if
(m_Font.Italic)
{
fntstyFont
|=
FontStyle.Italic;
}
if
(m_Font.Strikeout)
{
fntstyFont
|=
FontStyle.Strikeout;
}
if
(m_Font.Underline)
{
fntstyFont
|=
FontStyle.Underline;
}
}
Font fntOut
=
new
Font(strFName, floFSize, fntstyFont);
生成图片
#region
生成图片
MemoryStream ms
=
null
;
Bitmap objBitmap;
if
(ViewState[
"
OldImage
"
]
!=
null
)
{
objBitmap
=
(Bitmap)Bitmap.FromFile(Page.Server.MapPath(ViewState[
"
OldImage
"
].ToString()));
}
else
{
objBitmap
=
new
Bitmap(
int
.Parse(Width.Value.ToString()),
int
.Parse(Height.Value.ToString()));
}
Graphics objGraphics
=
Graphics.FromImage(objBitmap);
if
(ViewState[
"
OldImage
"
]
==
null
)
{
objGraphics.FillRectangle(
new
SolidBrush(BackColor
==
Color.Empty
?
Color.White : BackColor),
0
,
0
, objBitmap.Width, objBitmap.Height);
}
SizeF wh
=
objGraphics.MeasureString(strCode, fntOut);
int
l
=
(objBitmap.Width
-
(
int
)wh.Width)
/
2
;
int
t
=
(objBitmap.Height
-
(
int
)wh.Height)
/
2
;
for
(
int
i
=
0
; i
<
strCode.Length; i
++
)
{
if
(i
==
0
)
{
l
-=
(
int
)(objGraphics.MeasureString(strCode.Substring(i,
1
), fntOut).Width
/
3
);
}
Matrix myMatrix
=
new
Matrix();
myMatrix.RotateAt(i
%
2
==
0
?
TextAngle :
0
-
TextAngle,
new
PointF(l
+
wh.Width
/
2
, wh.Height
/
2
), MatrixOrder.Append);
objGraphics.Transform
=
myMatrix;
objGraphics.DrawString(strCode.Substring(i,
1
), fntOut,
new
SolidBrush(ForeColor
==
Color.Empty
?
Color.Black : ForeColor), l, t);
l
+=
(
int
)objGraphics.MeasureString(strCode.Substring(i,
1
), fntOut).Width;
}
ms
=
new
MemoryStream();
objBitmap.Save(ms, ImageFormat.Jpeg);
AshxHandler.RegisterCacheFile(
this
.Page, m_newImage, ms.ToArray());
objGraphics.Dispose();
objBitmap.Dispose();
#endregion
m_IsLaod
=
true
;
this
.ImageUrl
=
m_newImage
+
"
?.
"
+
DateTime.Now.ToString(
"
mmfffMMyyddssHH
"
);
m_IsLaod
=
false
;
}
private
string
GetValidateCode()
{
char
[] strCode
=
null
;
int
[] ascii
=
null
;
if
(m_CustomCodes
!=
null
)
{
string
[] s
=
m_CustomCodes.Split(
'
,
'
);
strCode
=
new
char
[s.Length];
for
(
int
i
=
0
; i
<
s.Length; i
++
)
{
strCode[i]
=
char
.Parse(s[i]);
}
}
else
if
(m_ValidateCodeType
==
ValidateCodeType.Numeric)
{
strCode
=
new
char
[]
{
'
0
'
,
'
1
'
,
'
2
'
,
'
3
'
,
'
4
'
,
'
5
'
,
'
6
'
,
'
7
'
,
'
8
'
,
'
9
'
}
;
}
else
if
(m_ValidateCodeType
==
ValidateCodeType.Character)
{
strCode
=
new
char
[]
{
'
A
'
,
'
B
'
,
'
C
'
,
'
D
'
,
'
E
'
,
'
F
'
,
'
G
'
,
'
H
'
,
'
I
'
,
'
J
'
,
'
K
'
,
'
L
'
,
'
M
'
,
'
N
'
,
'
O
'
,
'
P
'
,
'
Q
'
,
'
R
'
,
'
S
'
,
'
T
'
,
'
U
'
,
'
V
'
,
'
W
'
,
'
X
'
,
'
Y
'
,
'
Z
'
}
;
}
else
if
(m_ValidateCodeType
==
ValidateCodeType.Chinese)
//
汉字
{
ascii
=
new
int
[]
{
0xb0a1
,
//
a
0xb2a1
,
//
b
0xb3a1
,
//
c
0Xb4a1
,
//
c
0xb5a1
,
//
d
0xb6ea
,
//
e
0xb7a2
,
//
f
0xb9a1
,
//
g
0xbaa1
,
//
h
0xbca1
,
//
j
0xc0a1
,
//
k
0xc1a1
,
//
l
0xc3a1
,
//
m
0xc5a1
,
//
n
0xc6a1
,
//
p
0xc7a1
,
//
q
0xc9a1
,
//
s
0xcca1
,
//
t
0xcea1
,
//
w
0xcfa1
,
//
x
0xd3a1
,
//
y
0xd6a1
//
z
}
;
}
else
//
混合的
{
strCode
=
new
char
[]
{
'
0
'
,
'
1
'
,
'
2
'
,
'
3
'
,
'
4
'
,
'
5
'
,
'
6
'
,
'
7
'
,
'
8
'
,
'
9
'
,
'
A
'
,
'
B
'
,
'
C
'
,
'
D
'
,
'
E
'
,
'
F
'
,
'
G
'
,
'
H
'
,
'
I
'
,
'
J
'
,
'
K
'
,
'
L
'
,
'
M
'
,
'
N
'
,
'
O
'
,
'
P
'
,
'
Q
'
,
'
R
'
,
'
S
'
,
'
T
'
,
'
U
'
,
'
V
'
,
'
W
'
,
'
X
'
,
'
Y
'
,
'
Z
'
}
;
}
StringBuilder builder
=
new
StringBuilder();
Random r
=
new
Random();
bool
isChinese
=
m_ValidateCodeType
==
ValidateCodeType.Chinese;
for
(
int
i
=
0
; i
<
m_Length; i
++
)
{
if
(isChinese)
{
int
index
=
r.Next(ascii.Length);
int
ascii1
=
ascii[index]
+
r.Next(
10
);
builder.Append(GetHZ(ascii1));
}
else
{
builder.Append(strCode[r.Next(
0
, strCode.Length)].ToString());
}
}
return
builder.ToString();
}
private
string
GetHZ(
int
Ascii)
{
byte
[] b
=
new
byte
[
2
];
string
code
=
Ascii.ToString(
"
x
"
);
b[
0
]
=
byte
.Parse(code.Substring(
0
,
2
), NumberStyles.HexNumber);
b[
1
]
=
byte
.Parse(code.Substring(
2
,
2
), NumberStyles.HexNumber);
return
Encoding.GetEncoding(
0
).GetString(b);
}
#endregion
}
}
两个相关的枚举类型:
public
enum
ValidateStyle
{
/**/
///
<summary>
///
客户端返回
///
</summary>
Client
=
0
,
/**/
///
<summary>
///
Session返回
///
</summary>
Session,
/**/
///
<summary>
///
Cookie返回
///
</summary>
Cookie,
/**/
///
<summary>
///
Cache返回
///
</summary>
Cache
}
public
enum
ValidateCodeType
{
/**/
///
<summary>
///
纯数字
///
</summary>
Numeric
=
0
,
/**/
///
<summary>
///
纯字符
///
</summary>
Character,
/**/
///
<summary>
///
数字与字符混合
///
</summary>
Commix,
/**/
///
<summary>
///
中文汉字
///
</summary>
Chinese
}
查看全文
相关阅读:
JS 正则查找与替换
MySQL 批量插入值
Python/Django 批量下载Excel
Python/Django 下载Excel2007
Python/Django 下载Excel2003
Python3安装Scrapy
服务器响应状态码
细说同域-同父域-跨域
细说Ajax跨域
PostgreSQL学习
原文地址:https://www.cnblogs.com/faib/p/743161.html
最新文章
HDOJ 2643 第二类斯特林数
HDOJ 4372 第一类斯特林数
HDOJ 3625 第一类斯特林数
HDOJ 1370 中国剩余定理
git的使用 快排
初识爬虫
跨域问题
drf版本控制 缓存
drf路由配置 解析器 响应器
def权限,频率,分页
热门文章
drf 序列化 认证
resfult规范,drf安装
Vue框架学习
django中间件以及解决csrf
IDEA Getter&Setter方法名snake_case转camelCase
Ubuntu 18.04更新软件源
vim 跳行查看日志
RabbitMQ死循环-延长ACK时间
DB2锁表或超时解决方案
SQLYog 快捷键
Copyright © 2011-2022 走看看