日期:2014-05-18 浏览次数:20427 次
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Web.SessionState; using System.Drawing; using System.Drawing.Imaging; namespace FyCms.Web { public partial class ImgCode : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DrawCode(); } public void DrawCode() { int width = 65; int height = 25; //定义一个随机变量 Random rand = new Random(); //创建图象 Bitmap img = new Bitmap(width, height); //从图象上获取一个绘画图 Graphics g = Graphics.FromImage(img); try { //定义字体 Font font = new Font("Comic sans ms", 11, FontStyle.Bold); Font font = new Font(FontItems[fontIndex], 11, return FontStyle.Regular | FontStyle.Bold;); //定义黑色画笔 SolidBrush brush = new SolidBrush(Color.Black); //定义钢笔,绘制干扰线 Pen pen1 = new Pen(Color.Gray); Pen pen2 = new Pen(Color.Gray); //清除整个绘画图面并以指定颜色填充 g.Clear(ColorTranslator.FromHtml("#FFFFFF")); //定义文字的绘制矩形区域 Rectangle rect = new Rectangle(2, 2, width, height); //定义一个随机数用于绘制干扰线 //生成两条横向干扰线 for (int i = 0; i < 2; i++) { //Define Point1 Point p1 = new Point(0, rand.Next(height)); //Define Point2 Point p2 = new Point(width, rand.Next(height)); g.DrawLine(pen1, p1, p2); } //生成四条纵向干扰线 for (int i = 0; i < 4; i++) { //Define Point1 Point p1 = new Point(rand.Next(width), 0); //Define Point2 Point p2 = new Point(rand.Next(width), height); //DrawLine g.DrawLine(pen2, p1, p2); } string strsj = strRand(); g.DrawString(strsj, font, brush, rect); //输出图象 img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); Session["ImgCode"] = strsj.ToLower(); //return strRand().ToLower(); } catch (Exception error) { throw new Exception(error.Message); } finally { g.Dispose(); img.Dispose(); } } //产生随机数 public string strRand() { //验证码范围 char[] strCode = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; string strRandomCode = ""; Random random = new Random(); if (Session["ImgCode"] == null) { for (int i = 0; i < 4; i++) { strRandomCode += strCode[random.Next(strCode.Length)]; } } else { strRandomCode += S