博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
验证码生成-->漂亮啊
阅读量:4512 次
发布时间:2019-06-08

本文共 4537 字,大约阅读时间需要 15 分钟。

验证码不用输出太多的HTML代码,直接创建一个一般处理程序,直接上代码

1  public class VCode : IHttpHandler  2     {  3   4         HttpContext context = null;  5         public void ProcessRequest(HttpContext context)  6         {  7             this.context = context;  8             CreateVImage(CreateRandomNum(4));  9         } 10  11         #region 创建随机数 CreateRandomNum(int NumCount) 12         ///  13         /// 创建随机数 14         ///  15         /// 条形码上的数字个数 16         /// 
返回随机数
17 private string CreateRandomNum(int NumCount) 18 { 19 string allChar = "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"; 20 string[] allCharArr = allChar.Split(','); 21 string randomNum = ""; 22 int temp = -1;//记录上次随机数的数值,尽量避免产生几个相同的随机数 23 Random random = new Random(); 24 for (int i = 0; i < NumCount; i++) 25 { 26 if (temp != -1) 27 { 28 random = new Random(i * temp * ((int)DateTime.Now.Ticks)); 29 } 30 int t = random.Next(36); 31 if (temp == t) 32 { 33 return CreateRandomNum(NumCount); 34 } 35 temp = t; 36 randomNum += allCharArr[t]; 37 } 38 return randomNum; 39 } 40 #endregion 41 42 #region 生成验证码图片 CreateVImage(string validateNum) 43 44 /// 45 /// 生成验证码图片 CreateVImage(string validateNum) 46 /// 47 /// 验证码数字 48 private void CreateVImage(string validateNum) 49 { 50 if (validateNum == null || validateNum.Trim() == string.Empty) 51 { 52 return; 53 } 54 //生成Bitmap图像 55 Bitmap image = new Bitmap(validateNum.Length * 12 + 10, 22); 56 Graphics g = Graphics.FromImage(image); 57 try 58 { 59 //随机数生成 60 Random random = new Random(); 61 //清空图片背景色,将背景色换为白色 62 g.Clear(Color.White); 63 //画图片的背景线 64 for (int i = 0; i < 25; i++) 65 { 66 int x1 = random.Next(image.Width); 67 int x2 = random.Next(image.Width); 68 int y1 = random.Next(image.Height); 69 int y2 = random.Next(image.Height); 70 g.DrawLine(new Pen(Brushes.Beige), x1, y1, x2, y2); 71 } 72 //设置字体 73 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); 74 //设置笔刷 75 System.Drawing.Drawing2D.LinearGradientBrush brush = 76 new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); 77 //用设置好的字体和笔刷将验证码写到图片上 78 g.DrawString(validateNum, font, brush, 2, 2); 79 //画图片上的点 80 for (int i = 0; i < 100; i++) 81 { 82 int x = random.Next(image.Width); 83 int y = random.Next(image.Height); 84 image.SetPixel(x, y, Color.FromArgb(random.Next())); 85 } 86 87 //画图片边框线 88 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width, image.Height); 89 //创建一个内存流 90 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 91 //将图片保存到指定流中,并指定图片格式 92 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 93 //清空缓存区所有内容输出 94 context.Response.ClearContent(); 95 context.Response.ContentType = "image/jpeg"; 96 context.Response.BinaryWrite(ms.ToArray()); 97 } 98 catch (Exception ex) 99 {100 context.Response.Write("异常..."+ex.Message);101 }102 finally103 {104 //释放资源105 g.Dispose();106 image.Dispose();107 }108 109 }110 #endregion111 112 public bool IsReusable113 {114 get115 {116 return false;117 }118 }119 }
ValidateCode

直接复制过去,改下类名直接可用,效果图如下:

周围黑的不是验证码生成的,截图原因而已

 

转载于:https://www.cnblogs.com/liyajie/p/ValidateCode.html

你可能感兴趣的文章
最精简的IOCP封装
查看>>
Jmeter-JDBC连接测试
查看>>
HDU 4344
查看>>
nyoj43 24 Point game(DFS)
查看>>
RedisTemplate操作Redis
查看>>
Android基础TOP5_2:MultiAutoCompleteTextView多文本自动补全文本框
查看>>
初识NodeJS,一个基于GoogleV8引擎的Javascript运行环境
查看>>
Selenium—获取页面的title,url;使用句柄方式切换窗口
查看>>
UIView的autoresizingMask属性
查看>>
空间索引应用
查看>>
CheckSum
查看>>
html5 context属性解释及代码实例
查看>>
codeforces A. Vasily the Bear and Triangle 解题报告
查看>>
tarjan求强连通分量
查看>>
几个机器学习上的概念
查看>>
golang——concurrency笔记
查看>>
iOS 8 by Tutorials (section2:开始自适应布局)
查看>>
git fetch 和 git pull 的区别
查看>>
为Linux服务器设置静态IP的方法
查看>>
关于瑞昱8763bfr的学习总结(1)
查看>>