温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:简单四则运算验证码(C#)源码
当前文件:
SiZeYunSuanYanZhengMa/ValidationCode/Image2.aspx.cs[4K,2009-6-12 11:54:19],打开代码结构图
SiZeYunSuanYanZhengMa/ValidationCode/Image2.aspx.cs[4K,2009-6-12 11:54:19],打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
12
//该源码下载自www.51aspx.com(51aspx.com) 13
14
using System.Drawing; 15
using System.Drawing.Drawing2D; //引用的命名控件 16
17
namespace ValidationCode 18
{ 19
public partial class Image2 : System.Web.UI.Page 20
{ 21
protected void Page_Load(object sender, EventArgs e) 22
{ 23
CreateCheckCodeImage(GetExpressions()); 24
} 25
26
/// <summary> 27
/// 产生算术式 28
/// </summary> 29
private string GetExpressions() 30
{ 31
Random rd = new Random(); //创建随机数对象 32
33
int Results = 0; 34
int Num1 = rd.Next(10); ; 35
int Num2 = rd.Next(10); ; 36
string Expressions = ""; 37
38
int F = (rd.Next(4) + 1); 39
switch (F) 40
{ 41
case 1: 42
Results = Num1 + Num2; 43
Expressions = Num1 + " 加上 " + Num2;// +" 结果是" +Results; 44
break; 45
case 2: 46
Results = Num1 - Num2; 47
Expressions = Num1 + " 减去 " + Num2; 48
break; 49
case 3: 50
Results = Num1 * Num2; 51
Expressions = Num1 + " 乘以 " + Num2; 52
break; 53
case 4: 54
if (Num2 > 0) 55
{ 56
Results = Convert.ToInt16(Num1 / Num2); 57
Expressions = Num1 + " 整除 " + Num2; 58
} 59
else 60
{ 61
Results = Num1; 62
Expressions = Num1 + " 整除 1";// + Results; 63
} 64
break; 65
} 66
67
//验证码值存放到Session中用来比较 68
Session["Code"] = Results.ToString(); 69
return Expressions; 70
71
} 72
73
//5_1_a_s_p_x.c_o_m 74
75
/// <summary> 76
/// 生成图片(增加背景噪音线、前景噪音点) 77
/// </summary> 78
/// <param name="checkCode">随机出字符串</param> 79
private void CreateCheckCodeImage(string checkCode) 80
{ 81
if (checkCode.Trim() == "" || checkCode == null) 82
return; 83
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)(checkCode.Length * 14), 22); 84
Graphics g = Graphics.FromImage(image); 85
try 86
{ 87
//生成随机生成器 88
Random random = new Random(); 89
90
//清空图片背景色 91
g.Clear(Color.White); 92
93
//画图片的背景噪音线 94
int i; 95
for (i = 0; i < 25; i++) 96
{ 97
int x1 = random.Next(image.Width); 98
int x2 = random.Next(image.Width); 99
int y1 = random.Next(image.Height); 100
int y2 = random.Next(image.Height); 101
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 102
} 103
104
//定义一个含10种字体的数组 105
String[] fontFamily ={ "Arial", "Verdana", "Comic Sans MS", "Impact", "Haettenschweiler", "幼圆", "黑体", "隶书", "宋体", "楷体_GB2312" }; 106
FontStyle[] fontStyle ={ FontStyle.Bold, FontStyle.Italic, FontStyle.Regular, FontStyle.Strikeout, FontStyle.Underline }; 107
Font font = new System.Drawing.Font(fontFamily[random.Next(9)], 12, fontStyle[random.Next(5)]); 108
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true); 109
g.DrawString(checkCode, font, brush, 2, 2); 110
111
//画图片的前景噪音点 112
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 113
System.IO.MemoryStream ms = new System.IO.MemoryStream(); 114
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 115
Response.ClearContent(); 116
Response.ContentType = "image/Gif"; 117
Response.BinaryWrite(ms.ToArray()); 118
119
} 120
catch 121
{ 122
g.Dispose(); 123
image.Dispose(); 124
} 125
126
} 127
128
} 129
} 130






}
}