温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:Ajax无刷新中文验证码
当前文件路径:ChineseCode/Img.aspx.cs

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
using System.Text; 12
using System.Drawing; 13
//该源码下载自www.51aspx.com(51aspx.com) 14
15
public partial class Img : System.Web.UI.Page 16
{ 17
protected void Page_Load(object sender, EventArgs e) 18
{ 19
//获取GB2312编码页(表) 20
Encoding gb = Encoding.GetEncoding("gb2312"); 21
22
//调用函数产生4个随机中文汉字编码 23
object[] bytes = ChineseCode.CreateRegionCode(4); 24
25
//根据汉字编码的字节数组解码出中文汉字 26
string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[]))); 27
string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[]))); 28
string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[]))); 29
string str4 = gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[]))); 30
string[] str = new string[] { str1, str2, str3, str4 }; 31
32
CreateCheckCodeImage(str); 33
34
PublicCla.HZ = str1 + str2 + str3 + str4; 35
} 36
37
private void CreateCheckCodeImage(string[] checkCode) 38
{ 39
if (checkCode == null || checkCode.Length <= 0) 40
return; 41
42
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 32.5)), 60); 43
System.Drawing.Graphics g = Graphics.FromImage(image); 44
45
try 46
{ 47
//生成随机生成器 48
Random random = new Random(); 49
50
//清空图片背景色 51
g.Clear(Color.White); 52
53
//画图片的背景噪音线 54
for (int i = 0; i < 20; i++) 55
{ 56
int x1 = random.Next(image.Width); 57
int x2 = random.Next(image.Width); 58
int y1 = random.Next(image.Height); 59
int y2 = random.Next(image.Height); 60
61
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 62
} 63
64
//定义颜色 65
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; 66
//定义字体 67
string[] f = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; 68
69
for (int k = 0; k <= checkCode.Length - 1; k++) 70
{ 71
int cindex = random.Next(7); 72
int findex = random.Next(5); 73
74
Font drawFont = new Font(f[findex], 16, (System.Drawing.FontStyle.Bold)); 75
76
77
78
SolidBrush drawBrush = new SolidBrush(c[cindex]); 79
80
float x = 5.0F; 81
float y = 0.0F; 82
float width = 20.0F; 83
float height = 25.0F; 84
int sjx = random.Next(10); 85
int sjy = random.Next(image.Height - (int)height); 86
87
RectangleF drawRect = new RectangleF(x + sjx + (k * 25), y + sjy, width, height); 88
89
StringFormat drawFormat = new StringFormat(); 90
drawFormat.Alignment = StringAlignment.Center; 91
92
g.DrawString(checkCode[k], drawFont, drawBrush, drawRect, drawFormat); 93
} 94
95
//画图片的前景噪音点 96
for (int i = 0; i < 100; i++) 97
{ 98
int x = random.Next(image.Width); 99
int y = random.Next(image.Height); 100
101
image.SetPixel(x, y, Color.FromArgb(random.Next())); 102
} 103
104
//画图片的边框线 105
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 106
107
System.IO.MemoryStream ms = new System.IO.MemoryStream(); 108
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 109
Response.ClearContent(); 110
Response.ContentType = "image/Gif"; 111
Response.BinaryWrite(ms.ToArray()); 112
} 113
finally 114
{ 115
g.Dispose(); 116
image.Dispose(); 117
} 118
} 119
} 120





}
}