生成验证码
StringBuilder sb = new StringBuilder(); Random random = new Random(); for (int i = 0; i < 4; i++) { sb.Append(random.Next(0, 9)); } Bitmap img = new Bitmap((int)Math.Ceiling((sb.ToString().Length * 12.5)), 22); Graphics gc = Graphics.FromImage(img); gc.Clear(Color.White); gc.DrawRectangle(new Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1); Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkRed, 1.2f, true); gc.DrawString(sb.ToString(), font, brush, 2, 2); MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Jpeg); Response.ClearContent(); Response.ContentType = "image/gif"; Response.BinaryWrite(ms.ToArray()); gc.Dispose(); img.Dispose(); Response.End();