C#" ContentType="text/html" ResponseEncoding="utf-8" Debug="true" %>
private void ValidateCode(string VNum) {
Bitmap Img = null;
Graphics g = null ;
MemoryStream ms = null ;
int gheight = VNum.Length*12;
Img = new Bitmap(gheight,20);
g = Graphics.FromImage(Img);
Font f = new Font( "新宋体",12 );
Solidbrush s = new SolidBrush( Color.Red );
g.DrawString(VNum,f,s,3,3);
ms=new MemoryStream();
Img.Save(ms,ImageFormat.Png);
Response.ClearContent() ;//更改http ContentType
Response.ContentType="image/Png";
Response.BinaryWrite(ms.ToArray());
//回收资源
g.Dispose();
Img.Dispose();
Response.End();
}
private string RndNum(int VcodeNum) {
string Vchar = "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,W,X,Y,Z" ;
string[] VcArray = Vchar.Split(',') ;
string VNum = "" ;//由于字符串很短,就不用StringBuilder了
int temp = -1 ;//记录上次随机数值,尽量避免生产几个一样的随机数
//采用一个简单的算法以保证生成随机数的不同
Random rand =new Random();
for ( int i = 1 ; i < VcodeNum+1 ; i++ ) {
if ( temp != -1) {
rand =new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(35) ;
if (temp != -1 && temp == t) {
return RndNum( VcodeNum );
}
temp = t ;
VNum += VcArray[t];
}
return VNum ;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-963169/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10752019/viewspace-963169/