日期:2014-05-17 浏览次数:20467 次
string chkCode = string.Empty; char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' }; Random rnd = new Random(); //生成验证码字符串 for (int i = 0; i < 4; i++) { chkCode += character[rnd.Next(character.Length)]; }
------解决方案--------------------
context.Response.ContentType = "image/JPEG";
using (System.Drawing.Bitmap bitmap= new System.Drawing.Bitmap(100, 50))
{
using(System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(bitmap))
{
/* g.DrawString("如鹏网",new System.Drawing.Font("宋体",20),System.Drawing.Brushes.Blue,new System.Drawing.Point(0,0));
System.Drawing.Pen pen = (System.Drawing.Pen)System.Drawing.Pens.Red.Clone();
pen.Width = 3;
g.DrawEllipse(pen,new System.Drawing.Rectangle(10,10,10,10));
bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);*/
Random rand = new Random();
int code = rand.Next(100,200);
string strCode = code.ToString();
HttpContext.Current.Session["Code"] = strCode;
g.DrawString(strCode,new System.Drawing.Font("宋体",12),System.Drawing.Brushes.Blue,new System.Drawing.PointF(0,0));
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
很不错的例子