博客
关于我
用户登陆的验证码的制作
阅读量:677 次
发布时间:2019-03-16

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

protected void FormCheck()

{
// 验证码的生成
Random rand = new Random();
int len = rand.Next(4, 6);

// 可选字符  char[] str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();  // 创建字符串构建器  System.Text.StringBuilder code = new System.Text.StringBuilder();  // 随机生成验证码  for (int i = 0; i < len; i++)  {      code.Append(str[rand.Next(str.Length)]);  }  // 将验证码存放到会话中  this.Session["result"] = code.ToString();  // 获取验证码的大小  Size imageSize = new Size();  imageSize.Width = (int)(imageSize.Width + 6);  imageSize.Height = (int)(imageSize.Height + 5);  // 创建并生成验证码的图像  using (Bitmap bmp = new Bitmap(imageSize.Width, imageSize.Height))  {      using (Graphics g = Graphics.FromImage(bmp))      {          g.Clear(Color.White);          g.DrawString(code.ToString(), myFont, Brushes.Black, new RectangleF(0, 0, imageSize.Width, imageSize.Height));      }  }  // 随机设置图片像素颜色  int x = rand.Next(imageSize.Width);  int y = rand.Next(imageSize.Height);  int r = rand.Next(255);  int g = rand.Next(255);  int b = rand.Next(255);  Color c = Color.FromArgb(r, g, b);  bmp.SetPixel(x, y, c);  // 保存图片到流中并输出  using (MemoryStream ms = new MemoryStream())  {      bmp.Save(ms, ImageFormat.Png);      this.Response.ContentType = "image/png";      ms.WriteTo(this.Response.OutputStream);      ms.Close();  }

}

转载地址:http://ssdqz.baihongyu.com/

你可能感兴趣的文章
Python selenium爬取影评生成词云图
查看>>
python selenium自动化测试报告
查看>>
Python selenium自动化测试框架实战 —— 登录测试案例
查看>>
Python Selenium设计模式 —— POM
查看>>
Python Serial:如何使用 read 或 readline 函数一次读取多个字符
查看>>
Python set([]) 如何检查两个对象是否相等?一个对象需要定义哪些方法来自定义它?
查看>>
Python setup.py:数据文件无法复制目录:不存在或不是常规文件
查看>>
Python setuptools sdist:仅安装版本化文件
查看>>
Python Shell下使用matplotlib
查看>>
Python Slice How-to,我知道Python Slice,但我怎么才能使用内置的Slice对象呢?
查看>>
python socket分包发送数据
查看>>
python socket模块_Python socket模块实现TCP服务端客户端
查看>>
Python SOCKS5代理客户端HTTPS
查看>>
Python Soc网络分析:通过使用函数迭代列表来计算机会网络
查看>>
Python Sphinx自动摘要:成员函数的自动列表
查看>>
Python SQL和NoSQL数据库操作实战
查看>>
python stdout flush_sys.stdout.flush()方法的用法
查看>>
python string 运算
查看>>
Python str与bytes之间的转换
查看>>
Python subprocess ffmpeg
查看>>