博客
关于我
用户登陆的验证码的制作
阅读量:676 次
发布时间: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/

你可能感兴趣的文章
PAT 1027 Colors in Mars
查看>>
PAT 1127 ZigZagging on a Tree[难]
查看>>
PAT 2-07. 素因子分解(20)
查看>>
PAT-1044. Shopping in Mars (25)
查看>>
PAT-乙级-1040 有几个PAT
查看>>
PAT1093 Count PAT's (25)(逻辑题)
查看>>
PATA1038题解(需复习)
查看>>
Patching Array
查看>>
Path does not chain with any of the trust anchors
查看>>
Path形状获取字符串型变量数据
查看>>
PAT甲级——1001 A+B Format (20分)
查看>>
Skywalking原理
查看>>
PAT甲级——1006 Sign In and Sign Out (25分)
查看>>
PAT甲级——1007 Maximum Subsequence Sum (25分)
查看>>
PAT甲级——1009 Product of Polynomials (25分)(最后一个测试点段错误)
查看>>
Spring对jdbc的支持
查看>>
PayPal网站付款标准版(for PHP)
查看>>
Paystack Android SDK 集成与使用指南
查看>>
pbf格式详解,javascript加载导出pbf文件示例
查看>>
PBOC2.0与3.0的区别
查看>>