样式一:
验证码样式一
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (!Page.IsPostBack) 4 { 5 this.GenImg(this.GenCode(4)); 6 } 7 } 8 private string GenCode(int num) 9 { 10 string[] source ={"1","2","3","4","5","6","7","8","9", 11 "A","B","C","D","E","F","G","H","I","J","K","L","M","N", 12 "P","Q","R","S","T","U","V","W","X","Y","Z"}; 13 string code = ""; 14 Random rd = new Random(); 15 for (int i = 0; i < num; i++) 16 { 17 code += source[rd.Next(0, source.Length)]; 18 } 19 return code; 20 } 21 22 //生成图片 23 private void GenImg(string code) 24 { 25 Bitmap myPalette = new Bitmap(40, 18); 26 27 Graphics gh = Graphics.FromImage(myPalette); 28 29 Rectangle rc = new Rectangle(0, 0, 40, 18); 30 31 gh.FillRectangle(new SolidBrush(Color.Gray), rc); 32 gh.DrawString(code, new Font("宋体", 12), new SolidBrush(Color.White), rc); 33 34 myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 35 36 Session["ValidateCode"] = code; 37 38 gh.Dispose(); 39 myPalette.Dispose(); 40 }
样式二:
验证码样式二
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 CreateCheckCodeImage(GenerateCheckCode()); 4 } 5 6 //随机生成验证码 7 private string GenerateCheckCode() 8 { 9 int number; 10 char code; 11 string checkCode = String.Empty; 12 System.Random random = new Random(); 13 for (int i = 0; i < 5; i++) 14 { 15 number = random.Next(); 16 if (number % 2 == 0) 17 code = (char)('0' + (char)(number % 10)); 18 else 19 code = (char)('A' + (char)(number % 26)); 20 checkCode += code.ToString(); 21 } 22 Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); 23 return checkCode; 24 } 25 26 //生成验证码图片 27 private void CreateCheckCodeImage(string checkCode) 28 { 29 if (checkCode == null || checkCode.Trim() == String.Empty) 30 return; 31 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); 32 Graphics g = Graphics.FromImage(image); 33 try 34 { 35 Random random = new Random(); 36 g.Clear(Color.White); 37 for (int i = 0; i < 25; i++) 38 { 39 int x1 = random.Next(image.Width); 40 int x2 = random.Next(image.Width); 41 int y1 = random.Next(image.Height); 42 int y2 = random.Next(image.Height); 43 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 44 } 45 46 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); 47 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); 48 g.DrawString(checkCode, font, brush, 2, 2); 49 for (int i = 0; i < 100; i++) 50 { 51 int x = random.Next(image.Width); 52 int y = random.Next(image.Height); 53 54 image.SetPixel(x, y, Color.FromArgb(random.Next())); 55 } 56 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 57 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 58 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 59 Response.ClearContent(); 60 Response.ContentType = "image/Gif"; 61 Response.BinaryWrite(ms.ToArray()); 62 } 63 finally 64 { 65 g.Dispose(); 66 image.Dispose(); 67 } 68 }
样式三:
验证码样式三
1 private static Random rnd = new Random(); 2 3 protected void Page_Load(object sender, EventArgs e) 4 { 5 int width = 60; 6 int height = 20; 7 Bitmap img = new Bitmap(width, height); 8 Graphics g = Graphics.FromImage(img); 9 Brush b; 10 b = new SolidBrush(Color.FromArgb(rnd.Next(200, 250), rnd.Next(200, 250), rnd.Next(200, 250))); 11 g.FillRectangle(b, 0, 0, width, height); 12 Pen p = new Pen(Color.FromArgb(rnd.Next(160, 200), rnd.Next(160, 200), rnd.Next(160, 200))); 13 for (int i = 0; i < 100; i++) 14 { 15 int x1 = rnd.Next(width); 16 int y1 = rnd.Next(height); 17 int x2 = x1 + rnd.Next(12); 18 int y2 = y1 + rnd.Next(12); 19 g.DrawLine(p, x1, y1, x2, y2); 20 } 21 Font f = new Font("Courier New", 16, FontStyle.Bold); 22 PointF pf; 23 StringBuilder sb = new StringBuilder(); 24 for (int i = 0; i < 4; i++) 25 { 26 string s = rnd.Next(10).ToString(); 27 sb.Append(s); 28 b = new SolidBrush(Color.FromArgb(rnd.Next(20, 130), rnd.Next(20, 130), rnd.Next(20, 130))); 29 pf = new PointF(13 * i, -1); 30 g.DrawString(s, f, b, pf); 31 } 32 Session["Captcha"] = sb.ToString(); 33 Response.ContentType = "image/pjpeg"; 34 img.Save(Response.OutputStream, ImageFormat.Jpeg); 35 b.Dispose(); 36 g.Dispose(); 37 img.Dispose(); 38 }
样式四:
验证码样式四
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 System.Random rand = new Random(); 4 int len = rand.Next(4, 6); 5 char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); 6 System.Text.StringBuilder myStr = new System.Text.StringBuilder(); 7 for (int iCount = 0; iCount < len; iCount++) 8 { 9 myStr.Append(chars[rand.Next(chars.Length)]); 10 } 11 string text = myStr.ToString(); 12 this.Session["checkcode"] = text; 13 Size ImageSize = Size.Empty; 14 Font myFont = new Font("MS Sans Serif", 12); 15 using (Bitmap bmp = new Bitmap(10, 10)) 16 { 17 using (Graphics g = Graphics.FromImage(bmp)) 18 { 19 SizeF size = g.MeasureString(text, myFont, 5000); 20 ImageSize.Width = (int)size.Width + 1; 21 ImageSize.Height = (int)size.Height + 1; 22 } 23 } 24 using (Bitmap bmp = new Bitmap(ImageSize.Width, ImageSize.Height)) 25 { 26 using (Graphics g = Graphics.FromImage(bmp)) 27 { 28 g.Clear(Color.White); 29 using (StringFormat f = new StringFormat()) 30 { 31 f.Alignment = StringAlignment.Near; 32 f.LineAlignment = StringAlignment.Center; 33 f.FormatFlags = StringFormatFlags.NoWrap; 34 g.DrawString( 35 text, 36 myFont, 37 Brushes.Black, 38 new RectangleF( 39 0, 40 0, 41 ImageSize.Width, 42 ImageSize.Height), 43 f); 44 } 45 } 46 int num = ImageSize.Width * ImageSize.Height * 30 / 100; 47 for (int iCount = 0; iCount < num; iCount++) 48 { 49 int x = rand.Next(ImageSize.Width); 50 int y = rand.Next(ImageSize.Height); 51 int r = rand.Next(255); 52 int g = rand.Next(255); 53 int b = rand.Next(255); 54 Color c = Color.FromArgb(r, g, b); 55 bmp.SetPixel(x, y, c); 56 } 57 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 58 bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 59 this.Response.ContentType = "image/png"; 60 ms.WriteTo(this.Response.OutputStream); 61 ms.Close(); 62 } 63 myFont.Dispose(); 64 }
样式五:
验证码样式五
1 static string[] FontItems = new string[] 2 { 3 "Arial", 4 "Helvetica", 5 "Geneva", 6 "sans-serif", 7 "Verdana" 8 }; 9 static Brush[] BrushItems = new Brush[] 10 { 11 Brushes.OliveDrab, 12 Brushes.ForestGreen, 13 Brushes.DarkCyan, 14 Brushes.LightSlateGray, 15 Brushes.RoyalBlue, 16 Brushes.SlateBlue, 17 Brushes.DarkViolet, 18 Brushes.MediumVioletRed, 19 Brushes.IndianRed, 20 Brushes.Firebrick, 21 Brushes.Chocolate, 22 Brushes.Peru, 23 Brushes.Goldenrod 24 }; 25 static string[] BrushName = new string[] 26 { 27 "OliveDrab", 28 "ForestGreen", 29 "DarkCyan", 30 "LightSlateGray", 31 "RoyalBlue", 32 "SlateBlue", 33 "DarkViolet", 34 "MediumVioletRed", 35 "IndianRed", 36 "Firebrick", 37 "Chocolate", 38 "Peru", 39 "Goldenrod" 40 }; 41 private static Color BackColor = Color.White; 42 private static Pen BorderColor = Pens.DarkGray; 43 private static int Width = 50; 44 private static int Height = 20; 45 private Random _random; 46 private string _code; 47 private int _brushNameIndex; 48 49 public void Page_Load(object sender, System.EventArgs e) 50 { 51 if (!IsPostBack) 52 { 53 // TODO : initialize 54 this._random = new Random(); 55 this._code = GetRandomCode(); 56 57 // TODO : use Session["code"] save the VerifyCode 58 Session["code"] = this._code; 59 60 // TODO : output Image 61 this.SetPageNoCache(); 62 this.OnPaint(); 63 } 64 } 65 66 //设置页面不被缓存 67 private void SetPageNoCache() 68 { 69 Response.Buffer = true; 70 Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1); 71 Response.Expires = 0; 72 Response.CacheControl = "no-cache"; 73 Response.AppendHeader("Pragma", "No-Cache"); 74 } 75 76 //取得一个 4 位的随机码 77 private string GetRandomCode() 78 { 79 return Guid.NewGuid().ToString().Substring(0, 4); 80 } 81 82 //随机取一个字体 83 private Font GetFont() 84 { 85 int fontIndex = _random.Next(0, FontItems.Length); 86 FontStyle fontStyle = GetFontStyle(_random.Next(0, 2)); 87 return new Font(FontItems[fontIndex], 12, fontStyle); 88 } 89 90 //取一个字体的样式 91 private FontStyle GetFontStyle(int index) 92 { 93 switch (index) 94 { 95 case 0: 96 return FontStyle.Bold; 97 case 1: 98 return FontStyle.Italic; 99 default: 100 return FontStyle.Regular; 101 } 102 } 103 104 //随机取一个笔刷 105 private Brush GetBrush() 106 { 107 int brushIndex = _random.Next(0, BrushItems.Length); 108 _brushNameIndex = brushIndex; 109 return BrushItems[brushIndex]; 110 } 111 112 113 //绘画事件 114 private void OnPaint() 115 { 116 Bitmap objBitmap = null; 117 Graphics g = null; 118 try 119 { 120 objBitmap = new Bitmap(Width, Height); 121 g = Graphics.FromImage(objBitmap); 122 Paint_Background(g); 123 Paint_Text(g); 124 Paint_TextStain(objBitmap); 125 Paint_Border(g); 126 objBitmap.Save(Response.OutputStream, ImageFormat.Gif); 127 Response.ContentType = "image/gif"; 128 } 129 catch { } 130 finally 131 { 132 if (null != objBitmap) 133 objBitmap.Dispose(); 134 if (null != g) 135 g.Dispose(); 136 } 137 } 138 139 140 //绘画背景颜色 141 private void Paint_Background(Graphics g) 142 { 143 g.Clear(BackColor); 144 } 145 146 //绘画边框 147 private void Paint_Border(Graphics g) 148 { 149 g.DrawRectangle(BorderColor, 0, 0, Width - 1, Height - 1); 150 } 151 152 //绘画文字 153 private void Paint_Text(Graphics g) 154 { 155 g.DrawString(_code, GetFont(), GetBrush(), 3, 1); 156 } 157 158 //绘画文字噪音点 159 private void Paint_TextStain(Bitmap b) 160 { 161 for (int n = 0; n < 30; n++) 162 { 163 int x = _random.Next(Width); 164 int y = _random.Next(Height); 165 b.SetPixel(x, y, Color.FromName(BrushName[_brushNameIndex])); 166 } 167 }
|