1】
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Collections; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using System.Web.UI.HtmlControls; 11 using System.Drawing; 12 13 public partial class checkcode : System.Web.UI.Page 14 { 15 protected void Page_Load(object sender, EventArgs e) 16 { 17 CreateCheckCodeImage(GenerateCheckCode()); 18 } 19 private string GenerateCheckCode() 20 { 21 int number; 22 char code; 23 string checkCode = String.Empty; 24 System.Random random = new Random(); 25 26 for (int i = 0; i < 4; i++) 27 { 28 number = random.Next(); 29 30 if (number % 2 == 0) 31 code = (char)('0' + (char)(number % 10)); 32 else 33 code = (char)('A' + (char)(number % 26)); 34 35 checkCode += code.ToString(); 36 } 37 // 随机生成的字母或数字组合放入Cookie中,则可以通过Cookie取得验证码,查看输入的是否匹配 38 Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); 39 return checkCode; 40 } 41 private void CreateCheckCodeImage(string checkCode) 42 { 43 if (checkCode == null || checkCode.Trim() == String.Empty) 44 return; 45 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); 46 Graphics g = Graphics.FromImage(image); 47 try 48 { 49 //生成随机生成器 50 Random random = new Random(); 51 //清空图片背景色 52 g.Clear(Color.White); 53 //画图片的背景噪音线 54 for (int i = 0; i < 2; i++) 55 { 56 int x1 = random.Next(image.Width); 57 int x2 = random.Next(image.Width); 58 int y1 = random.Next(image.Height); 59 int y2 = random.Next(image.Height); 60 g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2); 61 } 62 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); 63 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), 64 Color.Blue, Color.DarkRed, 1.2f, true); 65 g.DrawString(checkCode, font, brush, 2, 2); 66 //画图片的前景噪音点 67 for (int i = 0; i < 100; i++) 68 { 69 int x = random.Next(image.Width); 70 int y = random.Next(image.Height); 71 image.SetPixel(x, y, Color.FromArgb(random.Next())); 72 } 73 //画图片的边框线 74 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 75 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 76 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 77 Response.ClearContent(); 78 Response.ContentType = "image/Gif"; 79 Response.BinaryWrite(ms.ToArray()); 80 } 81 finally 82 { 83 g.Dispose(); 84 image.Dispose(); 85 } 86 } 87 }
2】
1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Security; 8 using System.Web.UI; 9 using System.Web.UI.HtmlControls; 10 using System.Web.UI.WebControls; 11 using System.Web.UI.WebControls.WebParts; 12 using System.Xml.Linq; 13 using System.Drawing; 14 using System.Drawing.Imaging; 15 using System.Drawing.Drawing2D; 16 17 namespace Web 18 { 19 public partial class yanzheng : System.Web.UI.Page 20 { 21 protected void Page_Load(object sender, EventArgs e) 22 { 23 CreateCheckCodeImage(GenCode(7)); 24 } 25 /**/ 26 /// <summary> 27 /// '产生随机字符串 28 /// </summary> 29 /// <param name="num">随机出几个字符</param> 30 /// <returns>随机出的字符串</returns> 31 private string GenCode(int num) 32 { 33 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" }; 34 string code = ""; 35 Random rd = new Random(); 36 int i; 37 for (i = 0; i < num; i++) 38 { 39 code += source[rd.Next(0, source.Length)]; 40 } 41 return code; 42 43 } 44 45 /**/ 46 /// <summary> 47 /// 生成图片(增加背景噪音线、前景噪音点) 48 /// </summary> 49 /// <param name="checkCode">随机出字符串</param> 50 private void CreateCheckCodeImage(string checkCode) 51 { 52 if (checkCode.Trim() == "" || checkCode == null) 53 return; 54 Session["Code"] = checkCode; //将字符串保存到Session中,以便需要时进行验证 55 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)(checkCode.Length * 15), 22); 56 Graphics g = Graphics.FromImage(image); 57 try 58 { 59 //生成随机生成器 60 Random random = new Random(); 61 62 //清空图片背景色 63 g.Clear(Color.White); 64 65 // 画图片的背景噪音线 66 int i; 67 for (i = 0; i < 30; i++) 68 { 69 int x1 = random.Next(image.Width); 70 int x2 = random.Next(image.Width); 71 int y1 = random.Next(image.Height); 72 int y2 = random.Next(image.Height); 73 g.DrawLine(new Pen(Color.Gainsboro), x1, y1, x2, y2);//绘制一条连接由坐标对指定的两个点的线条 74 } 75 76 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold)); 77 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.MidnightBlue, Color.Green, 1.2F, true); 78 g.DrawString(checkCode, font, brush, 2, 2); 79 80 //画图片的前景噪音点 81 g.DrawRectangle(new Pen(Color.YellowGreen), 0, 0, image.Width - 1, image.Height - 1); 82 83 //输出 84 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 85 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 86 Response.ClearContent(); 87 Response.ContentType = "image/Gif"; 88 Response.BinaryWrite(ms.ToArray()); 89 90 } 91 catch 92 { 93 g.Dispose(); 94 image.Dispose(); 95 } 96 97 } 98 } 99 }