温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:科信客户关系管理系统源码
当前文件路径:KeXinCrm/CheckCode.aspx.cs

1using System; 2
using System.Collections; 3
using System.ComponentModel; 4
using System.Data; 5
using System.Drawing; 6
using System.Web; 7
using System.Web.SessionState; 8
using System.Web.UI; 9
using System.Web.UI.WebControls; 10
using System.Web.UI.HtmlControls; 11
using System.Drawing.Imaging; 12
using System.IO; 13
14
namespace CRMS 15
{ 16
/// <summary> 17
/// CheckCode 的摘要说明。 18
/// </summary> 19
public partial class CheckCode : System.Web.UI.Page 20
{ 21
public static string CheckCode2=""; 22
protected void Page_Load(object sender, System.EventArgs e) 23
{ 24
this.CreateCheckCodeImage(GenerateCheckCode()); 25
} 26
27
Web 窗体设计器生成的代码 45
46
private string GenerateCheckCode() 47
{ 48
int number; 49
char code; 50
string checkCode = String.Empty; 51
52
System.Random random = new Random(); 53
54
for(int i=0; i<5; i++) 55
{ 56
number = random.Next(); 57
58
if(number % 2 == 0) 59
code = (char)('0' + (char)(number % 10)); 60
else 61
code = (char)('A' + (char)(number % 26)); 62
63
checkCode += code.ToString(); 64
} 65
66
//Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); 67
68
Session["checkCode2"]=checkCode; 69
70
return checkCode; 71
//string kk=Session["checkCode2"].ToString(); 72
//Response.Write(kk); 73
//Response.End(); 74
} 75
76
private void CreateCheckCodeImage(string checkCode) 77
{ 78
if(checkCode == null || checkCode.Trim() == String.Empty) 79
return; 80
81
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); 82
Graphics g = Graphics.FromImage(image); 83
84
try 85
{ 86
//生成随机生成器 87
Random random = new Random(); 88
89
//清空图片背景色 90
g.Clear(Color.White); 91
92
//画图片的背景噪音线 93
for(int i=0; i<25; i++) 94
{ 95
int x1 = random.Next(image.Width); 96
int x2 = random.Next(image.Width); 97
int y1 = random.Next(image.Height); 98
int y2 = random.Next(image.Height); 99
100
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 101
} 102
103
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); 104
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); 105
g.DrawString(checkCode, font, brush, 2, 2); 106
107
//画图片的前景噪音点 108
for(int i=0; i<100; i++) 109
{ 110
int x = random.Next(image.Width); 111
int y = random.Next(image.Height); 112
113
image.SetPixel(x, y, Color.FromArgb(random.Next())); 114
} 115
116
//画图片的边框线 117
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 118
119
System.IO.MemoryStream ms = new System.IO.MemoryStream(); 120
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 121
Response.ClearContent(); 122
Response.ContentType = "image/Gif"; 123
Response.BinaryWrite(ms.ToArray()); 124
} 125
finally 126
{ 127
g.Dispose(); 128
image.Dispose(); 129
} 130
} 131
} 132
133
} 134





}