博客
关于我
用户登陆的验证码的制作
阅读量: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制作甘特图的基本知识(附Demo)
查看>>
python制作一个简单的服务器,【Python】 做一个简单的 http 服务器
查看>>
Python到底能做什么?它的优点在哪?
查看>>
python利用pytorch库导出图像分割算子
查看>>
python利用pyshark监听网卡来抓包其中pyshark中摸索的一些可用参数
查看>>
python利用excel分析过杀漏失
查看>>
python判断汉字数目
查看>>
python判断文件是空的,如果是空的,就删除
查看>>
python判断密码是否正确_python密码判断是否符合要求的方法
查看>>
python判断字符串包含中文_Python 判断字符串是否包含中文
查看>>
python删除第一行_Python 乱码指北:一行删掉根目录
查看>>
Python删除列表元素的三种方法
查看>>
python初步学习-python数据类型-集合(set)
查看>>
python列表生成字典_Python中将字典转换为列表的方法
查看>>
python列表对应元素合并为列表及判断一个列表是几维
查看>>
python列表去重复后按照顺序_从包含不可共元素的Python列表中删除重复元素,同时保留顺序?...
查看>>
python列表前几个_python之列表
查看>>
python列表元组
查看>>
Python列表/元组/字典和集合使用
查看>>
python列表 行列选择_python_pandas_dataframe_行列选择_切片操作
查看>>