以前一直对C#的GDI画图部分知识点不怎么用所以忘得差不多了,这两天正好公司要做一个博客系统,其中一个需求就是留言时为了防止恶意攻击必须填写验证码,正好借着这个机会复习了一下,以下是实现代码,写的比较简单。
View 层
1 @{ 2 ViewBag.Title = "Home Page"; 3 } 4 <div class="row"> 5 <h1>test</h1> 6 <div class="col-lg-12"> 7 @using(Html.BeginForm("Index","Home", FormMethod.Post, new {@id="form",@enctype = "multipart/form-data" })) 8 { 9 <div class="form-group"> 10 <input type="file" name="file"/> 11 <input type="file" name="files" /> 12 <button>提交</button> 13 </div> 14 <div class="form-group"> 15 <div class="col-lg-1"> 16 <input type="text" class="col-lg-3"/> 17 </div> 18 <div class="col-lg-3"> 19 <img id="img" onclick="CheckCode()" src="/Home/GetValidateCode" style="height:30px;110px" title="点击更换" alt="点击更换" /> 20 <a href="javascript:void(0)" onclick="CheckCode()">点击更换</a> 21 </div> 22 </div> 23 } 24 </div> 25 </div> 26 @section scripts{ 27 <script> 28 $(function () { 29 $("#img").click(function () { 30 CheckCode() 31 }) 32 }) 33 function CheckCode() { 34 $("#img").attr("src", "/Home/GetValidateCode?date="+new Date()); 35 } 36 37 </script> 38 }
Controller 层
1 /// <summary> 2 /// View页面请求获得验证码 3 /// </summary> 4 public void GetValidateCode() 5 { 6 //获取随机生成的编码 7 string ValiDateCode = GetValiDateCode(); 8 Session["Key"] = ValiDateCode; 9 byte[] bytes = GetValidateCode(ValiDateCode); 10 Response.ClearContent(); 11 Response.ContentType = "image/Gif"; 12 Response.BinaryWrite(bytes); 13 // 也可以用MVC 的FileResult 14 //return File(bytes, @"image/jpeg"); 15 } 16 17 /// <summary> 18 /// 生成验证码方法 19 /// </summary> 20 /// <param name="ValiDateCode"></param> 21 /// <returns></returns> 22 public static byte[] GetValidateCode(string ValiDateCode) 23 { 24 Bitmap img = new Bitmap((int)Math.Ceiling(ValiDateCode.Length * 12.6), 22); //创建 Bitmap对象 25 Graphics graphics = Graphics.FromImage(img); //创建 Graphics 对象 26 graphics.Clear(Color.White); //清空图片背景色 27 Random random = new Random(); //创建 Random 实例 28 for (int i = 0; i <= 24; i++) 29 { 30 int x1 = random.Next(img.Width); 31 int x2 = random.Next(img.Width); 32 int y1 = random.Next(img.Height); 33 int y2 = random.Next(img.Height); 34 graphics.DrawLine(new Pen(Color.Silver),x1, y1, x2, y2); //在图片上画噪点线 35 } 36 Font font = new Font("Arial", 13,(FontStyle.Bold)); 37 //创建 Brush 对象, LinearGradientBrush实现字体渐变效果 LinearGradientBrush(Rectangle rectangle,Color color1,Color color2,float angle, bool isAngleScaleable) 38 LinearGradientBrush linear = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Gray, Color.Blue, 1.4f, true); 39 graphics.DrawString(ValiDateCode, font, linear, 2, 2); //绘制生成的验证码, 40 graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1); 41 MemoryStream stream = new MemoryStream(); //创建 流对象 42 img.Save(stream, ImageFormat.Jpeg); //将图像以特定的格式保存到流对象中 43 return stream.ToArray(); 44 } 45 46 /// <summary> 47 /// 获取随机生成的验证码 48 /// </summary> 49 /// <returns></returns> 50 private static string GetValiDateCode() 51 { 52 string[] letter = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "i", "m", "n", "o", "p", "Q" }; 53 Random random = new Random(); 54 StringBuilder result = new StringBuilder(); 55 for (int i = 1; i <= 5; i++) 56 { 57 if (i % 2 == 0) 58 { 59 result.Append(letter[random.Next(1, 15)]); 60 } 61 else 62 { 63 result.Append(random.Next(1, 9)); 64 } 65 } 66 return result.ToString(); 67 }
效果图: