1using System;

2using System.Collections;

3using System.ComponentModel;

4using System.Data;

5using System.Drawing;

6using System.Drawing.Imaging;

7using System.Web;

8using System.Web.SessionState;

9using System.Web.UI;

10using System.Web.UI.WebControls;

11using System.Web.UI.HtmlControls;

12

13public partial class yanzzengma : System.Web.UI.Page


14...{

15 // 驗證碼長度

16 private int codeLen = 4;

17 // 圖片清晰度

18 private int fineness =90;

19 // 圖片寬度

20 private int imgWidth = 55;

21 // 圖片高度

22 private int imgHeight = 24;

23 // 字體家族名稱

24 private string fontFamily = "Times New Roman";

25 // 字體大小

26 private int fontSize = 14;

27 // 字體樣式

28 private int fontStyle = 0;

29 // 繪制起始坐標 X

30 private int posX = 0;

31 // 繪制起始坐標 Y

32 private int posY = 0;

33 protected void Page_Load(object sender, EventArgs&nb


34 ...{


35 讀取 Request 傳遞參數讀取 Request 傳遞參數#region 讀取 Request 傳遞參數

36 // 獲取代碼長度設置

37 if (Request["CodeLen"] != null)


38 ...{

39 try


40 ...{

41 codeLen = Int32.Parse(Request["CodeLen"]);

42

43 // 規定驗證碼長度

44 if (codeLen < 4 || codeLen > 16)

45 throw new Exception("驗證碼長度必須在4到16之間");

46 }

47 catch (Exception Ex)

...{

49 throw Ex;

50 }

51 }

52

53 // 獲取圖片清晰度設置

54 if (Request["Fineness"] != null)


55 ...{

56 try


57 ...{

58 fineness = Int32.Parse(Request["Fineness"]);

59

60 // 驗證清晰度

61 if (fineness < 0 || fineness > 100)

62 throw new Exception("圖片清晰度必須在0到100之間");

63 }

64 catch (Exception Ex)


...{

66 throw Ex;

67 }

68 }

69

70 // 獲取圖片寬度

71 if (Request["ImgWidth"] != null)


72 ...{

73 try


74 ...{

75 imgWidth = Int32.Parse(Request["ImgWidth"]);

76

77 if (imgWidth < 16 || imgWidth > 480)

78 throw new Exception("圖片寬度必須在16到480之間");

79 }

80 catch (Exception Ex)


81 ...{

82 throw Ex;

83 }

84 }

85

86 // 獲取圖片高度

87 if (Request["ImgHeight"] != null)


88 ...{

89 try


90 ...{

91 imgWidth = Int32.Parse(Request["ImgHeight"]);

92

93 if (imgWidth < 16 || imgWidth > 320)

94 throw new Exception("圖片高度必須在16到320之間");

95 }

96 catch (Exception Ex)


97 ...{

98 throw Ex;

99 }

100 }

101

102 // 獲取驗證碼字體家族名稱

103 if (Request["FontFamily"] != null)

...{

105 fontFamily = Request["FontFamily"];

106 }

107

108 // 獲取驗證碼字體大小

109 if (Request["FontSize"] != null)


110 ...{

111 try


112 ...{

113 fontSize = Int32.Parse(Request["FontSize"]);

114

115 if (fontSize < 8 || fontSize > 72)

116 throw new Exception("字體大小必須在8到72之間");

117 }

118 catch (Exception Ex)


119 ...{

120 throw Ex;

}

122 }

123

124 // 獲取字體樣式

125 if (Request["FontStyle"] != null)


126 ...{

127 try


128 ...{

129 fontStyle = Int32.Parse(Request["FontStyle"]);

130 }

131 catch (Exception Ex)


132 ...{

133 throw Ex;

134 }

135 }

136

137 // 驗證碼繪制起始位置 X

138 if (Request["PosX"] != null)


...{

140 try


141 ...{

142 posX = Int32.Parse(Request["PosX"]);

143 }

144 catch (Exception Ex)


145 ...{

146 throw Ex;

147 }

148 }

149

150 // 驗證碼繪制起始位置 Y

151 if (Request["PosY"] != null)


152 ...{

153 try


154 ...{

155 &nbs posY = Int32.Parse(Request["PosY"]);

156 }

157 catch (Exception Ex)


158 ...{

159 throw Ex;

160 }

161 }

162 #endregion

163

164 string validateCode = CreateValidateCode();

165

166 // 生成BITMAP圖像

167 Bitmap bitmap = new Bitmap(imgWidth, imgHeight);

168

169 // 給圖像設置干擾

170 DisturbBitmap(bitmap);

171

172 // 繪制驗證碼圖像

173 DrawValidateCode(bitmap, validateCode);

174

175 // 保存驗證碼圖像,等待輸出

176 bitmap.Save(Response.OutputStream, ImageFormat.Gif);

177 }

178


179 Web 窗體設計器生成的代碼Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼

180 override protected void OnInit(EventArgs e)

181 ...{

182 //

183 // CODEGEN: 該調用是 ASP.Net Web 窗體設計器所必需的。

184 //

185 InitializeComponent();

186 base.OnInit(e);

187 }

188


189 /**//**//**//// <summary>


190 /**//// 設計器支持所需的方法 - 不要使用代碼編輯器修改


191 /**//// 此方法的內容。


192 /**//// </summary>

193 private void InitializeComponent()


194 ...{

195 this.Load += new System.EventHandler(this.Page_Load);

196 }

197 #endregion

198

199 //------------------------------------------------------------

200 // 隨機生成驗證碼,並保存到SESSION中

201 //------------------------------------------------------------

202 private string CreateValidateCode()


203 ...{

204 string validateCode = "";

205 int num=0;

206 // 隨機數對象

207 Random random = new Random();

208

209 for (int i = 0; i < codeLen; i++)


210 ...{

211 // 26: a - z

212 int n = random.Next(26);

213

214

215 // 將數字轉換成大寫字母

216 validateCode += (char)(n + 65);

217 }

218

219 // 保存驗證碼

220 Session["ValidateCode"] = validateCode;

221

222 return validateCode;

223 }

224

225 //------------------------------------------------------------

226 // 為圖片設置干擾點

227 //------------------------------------------------------------

228 private void DisturbBitmap(Bitmap bitmap)


229 ...{

230 // 通過隨機數生成

231 Random random = new Random();

232

233 for (int i = 0; i < bitmap.Width; i++)


234 ...{

235 for (int j = 0; j < bitmap.Height; j++)


236 ...{

237 if (random.Next(100) <= this.fineness)

238 bitmap.SetPixel(i, j, Color.White);

239 }

240 }

241 }

242

243 //------------------------------------------------------------

244 // 繪制驗證碼圖片

245 //------------------------------------------------------------

246 private void DrawValidateCode(Bitmap bitmap, string validateCode)


247 ...{

248 // 獲取繪制器對象

249 Graphics g = Graphics.FromImage(bitmap);

250

251 // 設置繪制字體

252 Font font = new Font(fontFamily, fontSize, GetFontStyle());

253

254 // 繪制驗證碼圖像

255 g.DrawString(validateCode, font, Brushes.Black, posX, posY);

256 }

257

258 //------------------------------------------------------------

259 // 換算驗證碼字體樣式:1 粗體 2 斜體 3 粗斜體,默認為普通字體

260 //------------------------------------------------------------

261 private FontStyle GetFontStyle()


262 ...{

263 if (fontStyle == 1)

264 return FontStyle.Bold;

265 else if (fontStyle == 2)

266 return FontStyle.Italic;

267 else if (fontStyle == 3)

268 return FontStyle.Bold | FontStyle.Italic;

269 else

270 return FontStyle.Regular;

271 }

272

273

274}