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 ...
随机推荐
- 在Shell中使用函数文件
需要编写一个较庞大的脚本时,可能会涉及许多函数.变量.这是通常建议将众多的函数.变量放入一个单独的脚本内.这样做的好处很明显,不用担心某个函数.变量是否已经被定义和使用,也不用频繁地定义.清除函数和变 ...
- tachyon 集群容错
集群容错就是HA.这次顺带也练一下hadoop的HA 环境: centos6.5+jdk1.7+hadoop2.2.0+tachyon0.5.0+zookeeper3.4.6 hadoop 192.1 ...
- JavaEE&Docker 容器示例
准备:jboss.jdk.一个javaee的war包.Dockerfile 注:jboss和jdk可以不用提前准备好,在命令中wget也可以,因为我恰好有,就直接复制了 Dockerfile内容: # ...
- .NET和SQL Server中“空值”辨析 (DBNull与Null的区别)
对表进行插入操作,如datetime和int类型的两个字段,都允许为null,用“sqlcmd.Parameters.Add("@t12",tb12.Text)”参数绑定时.dat ...
- Entity Framework之问题收集
本节讨论内容主要针对收集了上篇文章大家碰到问题的讨论解决,会持续收集扩充. DbContext加载原值,当前值,数据库值,属性操作,对象复制,对象值复制(VO,DTO->POCO),复杂对象取值 ...
- 函数查询(Function Query)
函数查询 可以利用 numeric字段的值 或者 与字段相关的的某个特定的值的函数,来对文档进行评分. 1. 使用函数查询的方法 这里主要有三种方法可以使用函数查询,这三种s方法都是通过solr ...
- css 默认样式
body,textarea,input,select,option {font-size:12px;color:#333;font-family:Tahoma,Arial,sans-serif;} h ...
- C#实现插件式架构的方法
插件式架构,一种全新的.开放性的.高扩展性的架构体系.插件式架构设计近年来非常流行,基于插件的设计好处很多,把扩展功能从框架中剥离出来,降低了框架的复杂度,让框架更容易实现.扩展功能与框架以一种很松的 ...
- .NET强制进行即时垃圾回收
大家知道,.NET控制系统垃圾回收(一种自动回收未使用内存的服务)是自动的. 可有时候需要手动强制进行即时垃圾回收. 代码如下: GC.Collect();
- ReactNative学习-滑动查看图片第三方组件react-native-swiper
滑动查看图片第三方组件:react-native-swiper,现在的版本为:1.4.3,该版本还不支持Android. 下面介绍的是该组件的一些用法,可能总结的不完整,希望大家一起来共同完善. 官方 ...