博客
关于我
用户登陆的验证码的制作
阅读量: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读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
查看>>
Python 中 3 个不可思议的返回功能
查看>>
python 中 dict 的另一种用法
查看>>
Python 中 PIL 读取图片出现异常旋转的解决方法
查看>>
python读取word表格内容(1)
查看>>
python 中os.path.join 双斜杠的解决办法
查看>>
python 中PIL.Image和OpenCV图像格式相互转换
查看>>
Python 中Semaphore 信号量对象、Event事件、Condition
查看>>
python 中with的使用及样例
查看>>
python读取wav文件并播放[pyaudio/wave]
查看>>
python读取txt文件的行数
查看>>
Python 中内置的最大堆 API
查看>>
Python 中只有一个 True 和一个 False 对象吗?
查看>>
python读取mtcars数据集并实现以下操作_关于数据处理。。,Python交流,技术交流区,鱼C论坛 - Powered by Discuz!...
查看>>
Python 中多线程与多处理之间的区别
查看>>
Python 中如何使用 lambda 函数
查看>>
Python 中如何创建多行字符串?
查看>>
Python 中如何处理异常?
查看>>
Python 中如何实现列表的切片?
查看>>
Python 中如何实现字典的排序?
查看>>