Asp.net 生成验证码
public class CreateCodeImageUtil
{
public void CreateImage()
{
//声明code字体大小
int fontsize = ; //获取code的值
string code = GetCode(); //声明一个图片实例
Bitmap image = new Bitmap(code.Length * fontsize, ); //声明画图类的实例
Graphics g = Graphics.FromImage(image);
g.Clear(Color.FromArgb(, , )); //任取四点,画四条贝塞尔样条作为背景噪音线
Random random = new Random();
Color color = Color.FromArgb(, , );
Brush brush = new SolidBrush(color);
Pen pen = new Pen(brush, );
for (int i = ; i < ; i++)
{
Point p1 = new Point(random.Next(image.Width), random.Next(image.Height));
Point p2 = new Point(random.Next(image.Width), random.Next(image.Height));
Point p3 = new Point(random.Next(image.Width), random.Next(image.Height));
Point p4 = new Point(random.Next(image.Width), random.Next(image.Height)); g.DrawBezier(pen, p1, p2, p3, p4);
//g.DrawLine(pen, p3, p4);
} //绘制code
int angle = random.Next(, );
Font font = new Font("Arial", fontsize, (FontStyle.Bold | FontStyle.Italic));
//线性渐变
LinearGradientBrush textbrush = new LinearGradientBrush
(new Rectangle(, , image.Width - , image.Height - ),
Color.DimGray,
Color.Purple,
angle);
g.DrawString(code, font, textbrush, , ); //设置前景噪点
for (int i = ; i < ; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(, , ));
} //将图片返回到输出流
MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray()); //释放绘图资源
g.Dispose();
image.Dispose();
} public string GetCode()
{
try
{
Random random = new Random(); string code = "";
int num = random.Next(, );
for (int i = ; i < num; i++)
{
char chr = (char)('' + (char)(random.Next() % ));
code += chr;
}
for (int i = ; i < - num; i++)
{
char chr = (char)('A' + (char)(random.Next() % ));
code += chr;
} return code;
}
catch
{
return "";
}
}
}
Asp.net 生成验证码的更多相关文章
- 012. asp.net生成验证码图片(汉字示例/字母+数字)
protected void Page_Load(object sender, EventArgs e) { //生成验证码图片的基本步骤 string checkCode = "新年快乐& ...
- 【转载】ASP.NET 生成验证码
直接上code using System; using System.Collections.Generic; using System.Linq; using System.Web; using S ...
- ASP.NET——生成验证码
实现:随机生成四位数字的验证码,点击验证码可无刷新生成新的验证码,最后点击按钮进行检验 PS:本实例使用UpdatePanel实现无刷新. 前台代码: <asp:ScriptManager ID ...
- ASP.NET生成验证码
首先,添加一个一般处理程序 注释很详细了,有不懂的欢迎评论 using System; using System.Collections.Generic; using System.Drawing; ...
- ASP.NET图形验证码的生成
效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...
- ASP.NET ashx实现无刷新页面生成验证码
现在大部分网站登陆时都会要求输入验证码,在网上也看了一些范例,现在总结一下如何实现无刷新页面生成验证码. 效果图: 实现方式: 前台: <div> <span>Identify ...
- 详细说说如何生成验证码—ASP.NET细枝末节(4)
前言 今天小编详细的说一下,ASP.NET网站开发过程中生成验证码的全部问题. 本文的目标,是让读者了解,生成验证码涉及的全部基础知识问题. 当然这里说的是比较简单的验证码. 真正符合要求的验证码,涉 ...
- (一)【转】asp.net mvc生成验证码
网站添加验证码,主要为防止机器人程序批量注册,或对特定的注册用户用特定程序暴力破解方式,以进行不断的登录.灌水等危害网站的操作.验证码被广泛应用在注册.登录.留言等提交信息到服务器端处理的页面中. ...
- Asp.net mvc生成验证码
1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
随机推荐
- 进程间通信和同步:pipe、FIFO、消息队列、信号量、共享内存、信号
一.半双工管道(pipe) 关于管道详细介绍可参考http://www.cnblogs.com/nufangrensheng/p/3560130.html. 1.管道实现父子进程间通信实例: /* p ...
- QtInternal 之 高效使用QString
注意:本文翻译自 http://developer.qt.nokia.com 中的 UsingQStringEffectively ,中文译文见 简体中文版 ,如果你对翻译wiki感兴趣 ...
- Oracle—用户管理的完全恢复(四)
在用户管理的备份(三)中,最后打开数据库时,用了alter database open resetlogs;的命令,这里为什么用resetlogs命令? 一.resetlogs的作用 1.将当前的日志 ...
- Java 使用 Redis
安装 开始在 Java 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 Java redis 驱动,且你的机器上能正常使用 Java. Java的安装配置可以参考我们的 Ja ...
- uboot 网络不通问题解决一例1
平台:Hi3531 PHY:RTL8211 现象:在uboot中执行ping命令的时候,总是超时. 过程: 使用uboot自带的phy操作命令mii读出的数据全是0xff.这里要介绍一下uboot中的 ...
- LeetCode41 First Missing Positive
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- Python练习题 004:判断某日期是该年的第几天
[Python练习题 004]输入某年某月某日,判断这一天是这一年的第几天? ---------------------------------------------- 这题竟然写了 28 行代码! ...
- Java基础知识强化之IO流笔记73:NIO之 Channel
1. Java NIO的Channel(通道)类似 Stream(流),但又有些不同: 既可以从通道中读取数据,又可以写数据到通道.但流的读写通常是单向的. 通道可以异步地读写. 通道中的数据总是要先 ...
- Hosts文件是什么?
Hosts文件主要作用是定义IP地址和主机名的映射关系,是一个映射IP地址和主机名的规定.可以用文本文件打开!当用户在浏览器中输入一个需要登录的 网址时,系统会首先自动从Hosts文件中寻找对应的IP ...
- Visual Studio 扩展包(.vsix)制作
前言:上篇介绍了 Visual Studio扩展工具添加与卸载,本编要介绍的是Visual Studio 扩展包(.vsix)的制作. 方法: ①.下载并安装Visual Studio 2010 SD ...