protected string GetCode()
{//随机生成数(4个)
string strCode = "";
Random rand = new Random();
string[] strs = new string[36] { "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 nNum = "";
for (int nI = 0; nI < 4; nI++)
{
nNum = strs[rand.Next(0, 35)];
strCode += nNum.ToString();
}
return strCode;
}
protected void DrawCode(string strCode)
{//绘画图片
Session["Code"] = strCode; //页面传值
Bitmap bmp = new Bitmap(70, 22);//画布大小
Graphics g = Graphics.FromImage(bmp);//构造图片
g.Clear(Color.Green);//背景色
/*
//线条
Pen blackPen = new Pen(Color.Red, 1);
Rectangle rect = new Rectangle(10, 0, 100, 20);
float startAngle = 45.0F;
float sweepAngle = 270.0F;
g.DrawArc(blackPen, rect, startAngle, sweepAngle);
*/
Random random = new Random();
for (int i = 0; i < 135; i++)
{
int x = random.Next(bmp.Width);
int y = random.Next(bmp.Height);
bmp.SetPixel(x, y, Color.FromArgb(0, 0, 0));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);//画图片的边框线
g.DrawString(strCode, new Font("宋体", 18), new SolidBrush(Color.Black), 8, 0);
//Response. ; //字符串,字体 大小,字体色,位置
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//打印这个图片
}
}