//产生随机字符串
private string GenCode(intnum)
{
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"};
stringcode="";
Randomrd=newRandom();
for(inti=0;i
{
code+=source[rd.Next(0,source.Length)];
}
returncode;
}

//生成图片
private void GenImg(stringcode)
{
BitmapmyPalette=newBitmap(60,20);//定义一个画板

Graphicsgh=Graphics.FromImage(myPalette);//在画板上定义绘图的实例

Rectanglerc=newRectangle(0,0,60,20);//定义一个矩形

gh.FillRectangle(newSolidBrush(Color.Silver),rc);//填充矩形
gh.DrawString(code,newFont("宋体",16),newSolidBrush(Color.Gray),rc);//在矩形内画出字符串

myPalette.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);//将图片显示出来

Session["ValidateCode"]=code;//将字符串保存到Session中,以便需要时进行验证

gh.Dispose();
myPalette.Dispose();
}
http://workgroup.cn/CS/blogs/aspnet/archive/2006/04/10/ASP.NET_1F7510628F963A678C9AC18B0178_.aspx
posted on 2007-03-08 00:20  mbskys  阅读(94)  评论(0)    收藏  举报