直接上code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging; namespace AnalyzerExtAuth.Common
{
public partial class ValidateCodeImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//验证码中可能会出现的字符集合
String checkCodeString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//验证码字符集合的长度
int length = checkCodeString.Length;
//设置绘制验证码的字体,并设置为粗体并倾斜
Font font = new Font("宋体", , (FontStyle.Bold | FontStyle.Italic));
//绘制验证码的笔刷
Brush brush = null;
//绘制验证码文字的颜色
Color brushColor = new Color();
//验证码的字符串
String checkCode = String.Empty;
//当前要绘制的验证字符
String code = String.Empty;
//要生成的验证码图片对象
Bitmap image = new Bitmap(, );
//绘图画板
Graphics graphics = Graphics.FromImage(image);
//填充背景为白色
graphics.Clear(Color.White); //创建随机数对象
Random random = new Random(); int x1, x2, y1, y2;
Pen pen = new Pen(Color.Silver);
//画背景噪音线
for (int i = ; i <= ; i++)
{
x1 = random.Next(image.Width);
y1 = random.Next(image.Height);
x2 = random.Next(image.Width);
y2 = random.Next(image.Height);
graphics.DrawLine(pen, x1, y1, x2, y2);
} for (int i = ; i < ; i++)
{
//为了保证取的字符索引不超过0-35之间
//取任何数的余数都肯定小于自身
//采用当前时间的毫秒 % 验证码字符的总长度=当前验证字符
int current = random.Next(DateTime.Now.Millisecond) % length;
//截取验证字符
code = checkCodeString.Substring(current, );
//拼接到验证码的字符串
checkCode += code;
//随机生成验证码字符的颜色
brushColor = Color.FromArgb(random.Next(), random.Next(), random.Next());
//笔刷的颜色
brush = new SolidBrush(brushColor);
//绘制刚刚得到的字符串
graphics.DrawString(code, font, brush, i * + , );
} Response.Clear();
Response.ContentType = "image/pjpeg";
//在Session中保存验证码字符串,以便与用户输入进行比较
Session["CheckCode"] = checkCode;
image.Save(Response.OutputStream, ImageFormat.Jpeg);
image.Dispose();
Response.End(); }
}
}
//验证控件
< img src = "ValidateCode.aspx" width = "60" height = "25" style = "cursor: pointer;" onclick = "this.src='ValidateCode.aspx?id'+Math.random()*10000" / >

最新文章

  1. NSRunLoop
  2. mac+phpstorm+xampp断点调试
  3. Git系列教程一 入门与简介
  4. 设置参数进行启动的Python脚本模板
  5. C#字符串和数据之间的转换
  6. OD附加功能分析
  7. Win7 64位系统 VS2010连接Oracle报错的问题
  8. 解决win7系统远程桌面 server 2003 卡的问题
  9. Spring MVC 环境搭建(二)
  10. hg 证书验证失败
  11. 转接口IC ADV7280/ADV7280-M:CVBS转MIPI转接口芯片 10位、4倍过采样标清电视视频解码器,支持去隔行
  12. 在Linux中使用线程
  13. javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
  14. VBR与CBR的区别是什么?
  15. 今年暑假不AC - HZNU寒假集训
  16. 一个简洁的小H车调运模型
  17. KMP算法详细分解
  18. npm install的时候出现unexpected end of file错误提示时的解决办法
  19. 用selenium获取cookies
  20. myeclipse设置,提高开发效率

热门文章

  1. 模块之 logging模块 time 模块 random模块 sys模块 pickle模块
  2. CF 497 div 2 B
  3. wget常用下载命令
  4. Java8_Lambda表达式
  5. play后面加the不加the如何分辨
  6. ubuntu16.04中docker安装curl拒绝连接问题
  7. 学习成绩&gt;=90分的同学用A表示,60-89分之间的用B表示,60分以下的用利用条件运算符的嵌套来完成此题:C表示。
  8. 服务器和客户端Socket单向通信例子
  9. 在Visual studio 2010中为C#的“///”注释内容生成XML文档 .
  10. HDU 3395 Special Fish(拆点+最大费用最大流)