生成验证码一般来说大体有这么几步:
1、生成验证码字符串,一般由四个或更多随机字符拼凑而成;
2、填充图片背景,并绘制图片的背景噪音线;
3、将验证码绘制到图片中;
4、绘制前景噪点;
5、返回图片流,释放资源
 
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 生成验证码的更多相关文章

  1. 012. asp.net生成验证码图片(汉字示例/字母+数字)

    protected void Page_Load(object sender, EventArgs e) { //生成验证码图片的基本步骤 string checkCode = "新年快乐& ...

  2. 【转载】ASP.NET 生成验证码

    直接上code using System; using System.Collections.Generic; using System.Linq; using System.Web; using S ...

  3. ASP.NET——生成验证码

    实现:随机生成四位数字的验证码,点击验证码可无刷新生成新的验证码,最后点击按钮进行检验 PS:本实例使用UpdatePanel实现无刷新. 前台代码: <asp:ScriptManager ID ...

  4. ASP.NET生成验证码

    首先,添加一个一般处理程序 注释很详细了,有不懂的欢迎评论 using System; using System.Collections.Generic; using System.Drawing; ...

  5. ASP.NET图形验证码的生成

    效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...

  6. ASP.NET ashx实现无刷新页面生成验证码

    现在大部分网站登陆时都会要求输入验证码,在网上也看了一些范例,现在总结一下如何实现无刷新页面生成验证码. 效果图: 实现方式: 前台: <div> <span>Identify ...

  7. 详细说说如何生成验证码—ASP.NET细枝末节(4)

    前言 今天小编详细的说一下,ASP.NET网站开发过程中生成验证码的全部问题. 本文的目标,是让读者了解,生成验证码涉及的全部基础知识问题. 当然这里说的是比较简单的验证码. 真正符合要求的验证码,涉 ...

  8. (一)【转】asp.net mvc生成验证码

    网站添加验证码,主要为防止机器人程序批量注册,或对特定的注册用户用特定程序暴力破解方式,以进行不断的登录.灌水等危害网站的操作.验证码被广泛应用在注册.登录.留言等提交信息到服务器端处理的页面中.   ...

  9. Asp.net mvc生成验证码

    1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

随机推荐

  1. Windows内存管理和linux内存管理

    windows内存管理 windows 内存管理方式主要分为:页式管理,段式管理,段页式管理. 页式管理的基本原理是将各进程的虚拟空间划分为若干个长度相等的页:页式管理把内存空间按照页的大小划分成片或 ...

  2. Working with MTD Devices

    转:http://www.linuxforu.com/2012/01/working-with-mtd-devices/ Working with MTD Devices By Mohan Lal J ...

  3. 【Android 界面效果41】Matrix 与 ColorMatrix

    Matrix: 简单用法就是直接使用它的setXX()方法 而高级一点来理解他就是去理解一个线性矩形 首先我们来认识线性矩形:(用画图粗略地画不要见怪) 分析: 那还有一组 MRERSP_0 MRER ...

  4. 【Linux/Ubuntu学习1】Linux /etc 目录详解

    /etc目录 包含很多文件.许多网络配置文件也在/etc 中. /etc/rc   or/etc/rc.d   or/etc/rc*.d   启动.或改变运行级时运行的scripts或scripts的 ...

  5. 自定义EditText实现一键删除数据

    转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 自定义EditText带删除小图标, 实现的功能: 点击删除小图标,删除当 ...

  6. java 生成UUID

    UUID(Universally Unique Identifier)全局唯一标识符,是一个128位长的数字,一般用16进制表示. 算法的核心思想是结合机器的网卡.当地时间.一个随即数来生成UUID, ...

  7. spring mvc 重新定向到一个新的Url

    在写项目的时候要求根据请求的参数的不同重新将请求分发,在查阅了spring mvc的一些资料无果后(想使用拦截器去做)就没办法使用重定向的方式去写了 /** * 通过访问API的方式分发请求 * * ...

  8. codeforces 677C C. Vanya and Label(组合数学+快速幂)

    题目链接: C. Vanya and Label time limit per test 1 second memory limit per test 256 megabytes input stan ...

  9. EntityFramework使用中的一些Bug

    1. No Entity Framework provider found for the ADO.NET provider 解决方法:添加  EntityFramework.SqlServer.dl ...

  10. ASP.Net 验证控件 RequiredFieldValidator

    使用 ASP.NET 验证控件可在网页上检查用户输入.有用于各种不同类型验证的控件,例如范围检查或模式匹配验证控件.每个验证控件都引用网页上其他位置的输入控件(服务器控件).当处理用户输入时(例如,当 ...