1
首先先建立一个新类(如:CreateImage.cs),然后在这个类中写入下面的代码
2
//
引用
3
using
System.Drawing;
4
using
System.Web;
5
//
执行代码
6
public
static
void
DrawImage()
7
{
8
CreateImage img
=
new
CreateImage();
9
HttpContext.Current.Session[
"
CheckCode
"
]
=
img.RndNum(
4
);
10
img.CreateImages(HttpContext.Current.Session[
"
CheckCode
"
].ToString());
11
}
12
///
<summary>
13
///
生成验证图片
14
///
</summary>
15
///
<param name="checkCode">
验证字符
</param>
16
private
void
CreateImages(
string
checkCode)
17
{
18
int
iwidth
=
(
int
)(checkCode.Length
*
13
);
19
System.Drawing.Bitmap image
=
new
System.Drawing.Bitmap(iwidth,
23
);
20
Graphics g
=
Graphics.FromImage(image);
21
g.Clear(Color.White);
22
//
定义颜色
23
Color[] c
=
{Color.Black,Color.Red,Color.DarkBlue,Color.Green,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple};
24
//
定义字体
25
string
[] font
=
{
"
Verdana
"
,
"
Microsoft Sans Serif
"
,
"
Comic Sans MS
"
,
"
Arial
"
,
"
宋体
"
};
26
Random rand
=
new
Random();
27
//
随机输出噪点
28
for
(
int
i
=
0
;i
<
50
;i
++
)
29
{
30
int
x
=
rand.Next(image.Width);
31
int
y
=
rand.Next(image.Height);
32
g.DrawRectangle(
new
Pen(Color.LightGray,
0
),x,y,
1
,
1
);
33
}
34
//
输出不同字体和颜色的验证码字符
35
for
(
int
i
=
0
;i
<
checkCode.Length;i
++
)
36
{
37
int
cindex
=
rand.Next(
7
);
38
int
findex
=
rand.Next(
5
);
39
Font f
=
new
System.Drawing.Font(font[findex],
10
, System.Drawing.FontStyle.Bold);
40
Brush b
=
new
System.Drawing.SolidBrush(c[cindex]);
41
int
ii
=
4
;
42
if
((i
+
1
)
%
2
==
0
)
43
{
44
ii
=
2
;
45
}
46
g.DrawString(checkCode.Substring(i,
1
), f, b,
3
+
(i
*
12
), ii);
47
}
48
//
画一个边框
49
g.DrawRectangle(
new
Pen(Color.Black,
0
),
0
,
0
,image.Width
-
1
,image.Height
-
1
);
50
//
输出到浏览器
51
System.IO.MemoryStream ms
=
new
System.IO.MemoryStream();
52
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
53
HttpContext.Current.Response.ClearContent();
54
//
Response.ClearContent();
55
HttpContext.Current.Response.ContentType
=
"
image/Jpeg
"
;
56
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
57
g.Dispose();
58
image.Dispose();
59
}
60
///
<summary>
61
///
生成随机的字母
62
///
</summary>
63
///
<param name="VcodeNum">
生成字母的个数
</param>
64
///
<returns>
string
</returns>
65
public
string
RndNum(
int
VcodeNum)
66
{
67
string
Vchar
=
"
0,1,2,3,4,5,6,7,8,9
"
;
68
string
[] VcArray
=
Vchar.Split(
'
,
'
) ;
69
string
VNum
=
""
;
//
由于字符串很短,就不用StringBuilder了
70
int
temp
=
-
1
;
//
记录上次随机数值,尽量避免生产几个一样的随机数
71
//
采用一个简单的算法以保证生成随机数的不同
72
Random rand
=
new
Random();
73
for
(
int
i
=
1
; i
<
VcodeNum
+
1
; i
++
)
74
{
75
if
( temp
!=
-
1
)
76
{
77
rand
=
new
Random(i
*
temp
*
unchecked
((
int
)DateTime.Now.Ticks));
78
}
79
int
t
=
rand.Next(VcArray.Length ) ;
80
if
(temp
!=
-
1
&&
temp
==
t)
81
{
82
return
RndNum( VcodeNum );
83
}
84
temp
=
t ;
85
VNum
+=
VcArray[t];
86
}
87
return
VNum ;
88
}
89
90
其次建立一个新的页面(如:Image.aspx),在这个页面Page_Load的事件中写入引用类的方法,例如:
91
private
void
Page_Load(
object
sender, System.EventArgs e)
92
{
93
CreateImage.DrawImage ();
94
}
95
最后在显示界面上使用Label,Image,Button,TextBox控件,
96
Image控件的Url属性要连接Image.aspx,然后在写
97
private
void
Button1_Click(
object
sender, System.EventArgs e)
98
{
99
if
(
this
.TextBox2.Text.Trim()
==
Session[
"
CheckCode
"
].ToString())
100
{
101
this
.Label1.Text
=
"
显示成功!
"
;
102
}
103
else
104
{
this
.Label1.Text
=
"
显示错误!
"
;}
105
}
转载于:https://www.cnblogs.com/catbim/archive/2006/04/11/371913.html
转载请注明原文地址: https://win8.8miu.com/read-1499952.html