一个生成Validatecode 的代码。

Response.Clear();
            Response.ContentType 
= "image/jpeg";
            
int width = 50;
            
int height = 25;
            Bitmap bmp 
= new Bitmap(width, height, PixelFormat.Format24bppRgb);
            Graphics g 
= Graphics.FromImage(bmp);
            HatchBrush b 
= new HatchBrush(HatchStyle.Cross, Color.LightGray, Color.WhiteSmoke);
            g.FillRectangle(b, 
00, width, height);
            Random r 
= new Random(Convert.ToInt32(DateTime.Now.Millisecond));
            
string Code = r.Next(10009999).ToString();
            Session[
"ValidateCode"= Code;
            g.DrawString(Code, 
new Font("Arial"10, FontStyle.Bold), SystemBrushes.WindowText, 10.0F5.0F);
            bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
            b.Dispose();
            g.Dispose();
            bmp.Dispose();
            Response.End();
posted @ 2008-06-25 17:39  roboth  阅读(1698)  评论(0)    收藏  举报