NET 很简单的一个生成随机验证码封装的一个类
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Drawing;
- public class VerifyCode
- {
- public VerifyCode()
- {
- }
-
-
-
-
-
-
- public string GenCode(int num)
- {
-
- string[] source ={"0","1","2","3","4","5","6","7","8","9",
- "A","B","C","D","E","F","G","H","I","J","K","L","M","N",
- "O","P","Q","R","S","T","U","V","W","X","Y","Z"};
-
- string code = "";
- Random rd = new Random();
- for (int i = 0; i < num; i++)
- {
- code += source[rd.Next(0, source.Length)];
- }
- return code;
- }
-
- public void GenImg(string code, Page curPage)
- {
- Bitmap myPalette = new Bitmap(60, 20);
- Graphics gh = Graphics.FromImage(myPalette);
- Rectangle rc = new Rectangle(0, 0, 60, 20);
- gh.FillRectangle(new SolidBrush(Color.Blue), rc);
- gh.DrawString(code, new Font("宋体", 16), new SolidBrush(Color.White), rc);
- myPalette.Save(curPage.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
- curPage.Session["ValidateCode"] = code;
- gh.Dispose();
- myPalette.Dispose();
- }
- }
原文地址:https://www.cnblogs.com/dingdingmao/p/3146576.html