清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
一个C#生成简单验证码的类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls.WebParts; using System.Data; using System.Web.Security; using System.Collections; using System.Drawing; public partial class Code : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Session["label"] == null) { string str = ""; string[] code = {"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","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"}; //随机产生字符串 Random rand = new Random(); for (int i = 0; i < 4; i++) { str = str + code[rand.Next(0, code.Length)]; } CreateImage(str); foreach (var s in str) { str += s; } Session["str"] = str; //foreach (string s in str) //{ // str += s; //} //Session["str"] = str; // Label1.Text = str; // Session["label"] = Label1.Text; } } //画验证图片 private void CreateImage(string checkcode) { if (checkcode == null || checkcode.Length <= 0) return; int width = (int)(checkcode.Length * 25); System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling(checkcode.Length*32.5), 40); Graphics g = Graphics.FromImage(image); g.Clear(Color.White); //定义颜色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //定义字体 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; Random rand = new Random(); //画图片的背景线 for (int i = 0; i < 70; i++) { int x = rand.Next(image.Width); int y = rand.Next(image.Height); g.DrawLine(new Pen(Color.LightGray, 1), x, y, 10, 20); } //输出不同字体和颜色的验证码字符 for (int i = 0; i < checkcode.Length; i++) { int cindex = rand.Next(7); int findex = rand.Next(5); Font f = new System.Drawing.Font(font[findex], 23, System.Drawing.FontStyle.Bold);//23表示字体大小 Brush brush = new System.Drawing.SolidBrush(c[cindex]); int ii = 4; if ((i + 1) % 2 == 0) { ii = 2; } g.DrawString(checkcode.Substring(i, 1), f, brush, 15 + (i * 12), ii); } //画边框,画图片的边框线 g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1); //输出到浏览器 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); HttpContext.Current.Response.ContentType = "image/jpeg"; HttpContext.Current.Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); } }