private 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;
}
//生成图片
private Bitmap GenImg(string code)
{
Bitmap myPalette = new Bitmap(70, 20);//定义一个画板
Graphics gh = Graphics.FromImage(myPalette);//在画板上定义绘图的实例
Rectangle rc = new Rectangle(0, 0, 70, 20);//定义一个矩形
gh.FillRectangle(new SolidBrush(Color.White), rc);//填充矩形
Random rd = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < 30; i++)
{
int r = rd.Next(0, 255);
int g = rd.Next(0, 255);
int b = rd.Next(0, 255);
PointF p1 = new PointF((float)rd.Next(0, 70), (float)rd.Next(0, 20));
PointF p2 = new PointF((float)rd.Next(0, 70), (float)rd.Next(0, 20));
Pen p = new Pen(Color.FromArgb(r, g, b), 1f);
gh.DrawLine(p, p1, p2);
}
gh.DrawString(code, new Font("宋体", 16, FontStyle.Bold), new SolidBrush(Color.DarkBlue), rc);//在矩形内画出字符串
myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图片显示出来
return myPalette;
}