using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging; public partial class user_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string checkCode = CreateRandomCode();
GetImgWithValidateCode(checkCode, , ,);
}
///<summary>
/// 生成随机字符串
///</summary>
///<param name="codeCount">字符数量</param>
///<returns>随机字符串</returns>
private string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -;
Random rand = new Random();
for (int i = ; i < codeCount; i++)
{
if (temp != -)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next();
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
} ///<summary>
/// 创建验证码图片
///</summary>
///<param name="code">字符串</param>
///<param name="width">宽</param>
///<param name="height">高</param>
///<param name="fontSize">字号</param>
public void GetImgWithValidateCode(string code, int width, int height, int fontSize)
{
Color bgcolor = GetControllableColor();
Color color = GetControllableColor(); int charNum = code.Length;
Bitmap bitMap = null;
Graphics gph = null;
//创建内存流
MemoryStream memStream = new MemoryStream();
Random random = new Random(); //创建位图对象
bitMap = new Bitmap(width, height);
//根据上面创建的位图对象创建绘图图面
gph = Graphics.FromImage(bitMap);
//设定验证码图片背景色
gph.Clear(bgcolor);
//产生随机干扰线条
for (int i = ; i < ; i++)
{
Pen backPen = new Pen(color, );
int x = ;
int y = height / ;
int x2 = width;
int y2 = random.Next(height);
gph.DrawLine(backPen, x, y, x2, y2);
} SolidBrush sb = new SolidBrush(color);
PointF Cpoint1 = new PointF(, );
Random rnd1 = new Random();
int x1 = , y1 = ;
//通过循环,绘制每个字符,
for (int i = ; i < code.Length; i++)
{
x1 = rnd1.Next() + ((width - ) / code.Length) * i;
y1 = rnd1.Next(bitMap.Height / );
Cpoint1 = new PointF(x1, y1);
Font textFont = new Font("Arial", fontSize, FontStyle.Bold);//字体随机,字号大小30,加粗 //随机倾斜字符
Matrix transform = gph.Transform;
transform.Shear(Convert.ToSingle(rnd1.NextDouble() - 0.5), 0.001f);
gph.Transform = transform;
gph.DrawString(code.Substring(i, ), textFont, sb, Cpoint1);
gph.ResetTransform();
} //画图片的前景噪音点
for (int i = ; i < ; i++)
{
int x = random.Next(bitMap.Width);
int y = random.Next(bitMap.Height);
bitMap.SetPixel(x, y, Color.White);
}
//画图片的边框线
gph.DrawRectangle(new Pen(Color.Black, ), , , bitMap.Width - , bitMap.Height - );
try
{
bitMap.Save(memStream, ImageFormat.Gif);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
bitMap.Dispose();
System.Drawing.Image img = System.Drawing.Image.FromStream(memStream); gph.DrawImage(img, , , width, height);
//img.Save(@"D:\test.jpg", ImageFormat.Jpeg); //如果是asp.net 用下面的输出图片 System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
gph.Dispose();
img.Dispose();
} ///<summary>
/// 产生一种 R,G,B 均大于 colorBase 随机颜色,以确保颜色不会过深
///</summary>
///<param name="colorBase">The color base.</param>
///<returns>Color</returns>
public Color GetControllableColor(int colorBase)
{
Color color = Color.Black;
if (colorBase > )
{
return color;
}
Random random = new Random();
color = Color.FromArgb(random.Next() + colorBase, random.Next() + colorBase, random.Next() + colorBase);
return color;
}
}

验证码效果:

还有2种验证码请看我博客

http://www.cnblogs.com/webapi/p/5725576.html

http://www.cnblogs.com/webapi/p/5726498.html

Asp.Net验证码2的更多相关文章

  1. ASP.net 验证码(C#) MVC

    ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...

  2. MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式

    MVC的验证(模型注解和非侵入式脚本的结合使用)   @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客 ...

  3. 关于 ASP.NET 验证码

    Session["CheckCode"] 这个..不懂神马意思.. .创建一个用户控件 用户名:TextBox 密码: TextBox 验证码:TextBox 验证码图片 < ...

  4. ASP.NET 验证码 不同浏览器 不刷新问题

    具体为什么不刷新是缓存机制不同,验证码图片的src或ImageUrl的获取是来自一个文件,由于连接地址没变所以不同内核浏览器有的会认为源没有变,解决办法就是在连接后面加上一个随机参数如可以用JS的Ma ...

  5. asp.net验证码及怎么获取里面的数值(整合)

    一.ASP.Net的验证码的作用 对于一个预防攻击的web表单来讲,验证码通常是一个常见的措施.因为如果对于一些public区域的页面内容来讲,譬如一个登录表单,如果没有必要的安全措施,很可能遭到模拟 ...

  6. ASP.NET——验证码的制作

            我们在登陆站点,发表博客或者提交评论的时候,常常会遇到填写验证码这一项,当时感觉挺奇妙的样子,最终在牛腩新闻公布系统里接触到了,在这里小小的总结下.         用到的东东有三个: ...

  7. asp.net 验证码技术

    网站验证码是一种很常用的技术.下面我介绍下技术上是如何实现的. 验证码是一张图片.我们需要在前台代码中写一段<img>,src指向一张页面(ValidateImage.aspx). < ...

  8. asp.net验证码的编写

    很多时候我们在登录什么网站的时候,除了需要什么用户名和密码之外,有的还需要验证码那么在asp.net中这个验证码如何编写和设计,今天我就来给大家说一下: 首先创建一个页面名字随便起一个,我们这里叫做C ...

  9. asp.net验证码

    asp.net 生成验证码问题 .添加一个.ashx文件 <%@ WebHandler Language="C#" class="CheckCode" % ...

随机推荐

  1. Unity 3D 游戏上线之后的流水总结

    原地址:http://tieba.baidu.com/p/2817057297?pn=1 首先.unity 灯光烘焙 :Unity 3D FBX模型导入.选项Model 不导入资源球.Rig 不导入骨 ...

  2. MariaDB集群Galera Cluster的研究与测试

    MariaDB集群Galera Cluster的研究与测试 Galera Cluster是MariaDB的一个双活多主集群,其可以使得MariDB的所有节点保持同步,Galera为MariaDB提供了 ...

  3. 总结 | 如何测试你自己的 RubyGem

    如何测试一个Gem gem 开发完了,想要给别人用,那就需要测试啊,测试一个 gem 其实很简单,这里我们用 minitest 为例, rspec 也一样适用.先来看看我们当前这个 gem 的目录结构 ...

  4. 深入浅出ES6(十五):子类 Subclassing

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 在之前的文章<深入浅出ES6(十三):类 Class>中,我们一起深 ...

  5. 深入浅出ES6(十四):let和const

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 回溯到1995年,当Brendan Eich在设计第一版JavaScript时, ...

  6. php curl 分离header和body信息

    php中可以通过curl来模拟http请求,同时可以获取http response header和body,当然也设置参数可以只获取其中的某一个.当设置同时获取response header和body ...

  7. 二叉查找树的查找、插入和删除 - Java实现

    http://www.cnblogs.com/yangecnu/p/Introduce-Binary-Search-Tree.html 作者: yangecnu(yangecnu's Blog on ...

  8. hibernate中openSession()跟getCurrentSession()方法之间的区别

    Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...

  9. 百度首页html代码

          把百度设为主页 关于百度 About Baidu ©2015 Baidu 使用百度前必读 意见反馈 京ICP证030173号

  10. 【图像算法】图像特征:GLCM灰度共生矩阵,纹理特征

    [图像算法]图像特征:GLCM SkySeraph Aug 27th 2011  HQU Email:zgzhaobo@gmail.com    QQ:452728574 Latest Modifie ...