C#.net 漂亮图片验证码

根据提供的信息,我们可以总结出以下关于“C#.net 漂亮图片验证码”的知识点: ### 知识点一:图片验证码的基本概念 图片验证码(CAPTCHA)是一种常用于验证用户是否为人类的操作者而非自动程序的技术。其主要目的是防止恶意机器人进行自动化操作,如批量注册、投票等行为。图片验证码通过生成随机的图像,并要求用户输入图像中的字符来完成验证。 ### 知识点二:使用C#实现图片验证码 #### 1. 引用必要的命名空间 ```csharp using System; using System.Drawing; using System.Drawing.Imaging; using System.Web; ``` 这些命名空间包含了创建和处理图片验证码所需的基础类库。 #### 2. 创建验证码图片 ```csharp public partial class CreateCode : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // 生成验证码图片 string img = re_img(); Session["img"] = img; } } ``` 这里的 `re_img` 方法负责生成具体的验证码字符串和对应的图片。 #### 3. 生成随机字符 ```csharp public string re_img() { string chkCode = string.Empty; char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' }; Random rnd = new Random(); for (int i = 0; i < 4; i++) { chkCode += character[rnd.Next(character.Length)]; } return chkCode; } ``` 这里使用了一个包含数字和字母的字符数组,通过随机索引选取字符组成验证码。 #### 4. 绘制验证码图片 ```csharp Bitmap bmp = new Bitmap(150, 30); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); // 绘制干扰线 for (int i = 0; i < 3; i++) { int x1 = rnd.Next(150); int y1 = rnd.Next(30); int x2 = rnd.Next(150); int y2 = rnd.Next(30); Color clr = color[rnd.Next(color.Length)]; g.DrawLine(new Pen(clr), x1, y1, x2, y2); } // 绘制验证码字符 for (int i = 0; i < chkCode.Length; i++) { string fnt = font[rnd.Next(font.Length)]; Font ft = new Font(fnt, 16); Color clr = color[rnd.Next(color.Length)]; g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 20, (float)6); } // 添加随机噪点 for (int i = 0; i < 50; i++) { int x = rnd.Next(bmp.Width); int y = rnd.Next(bmp.Height); Color clr = color[rnd.Next(color.Length)]; bmp.SetPixel(x, y, clr); } ``` 这里使用了 `Bitmap` 和 `Graphics` 类来绘制验证码图片,包括背景颜色、字符、干扰线以及噪点等元素。 #### 5. 设置响应头 ```csharp Response.Buffer = true; Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0); Response.Expires = 0; Response.Cache.SetCacheability(HttpCacheability.NoCache); ``` 这些设置确保了验证码图片不会被浏览器缓存,以避免安全风险。 #### 6. 输出图片 ```csharp Response.ContentType = "image/jpeg"; bmp.Save(Response.OutputStream, ImageFormat.Jpeg); Response.End(); ``` 这段代码将生成的图片以JPEG格式输出到客户端。 ### 知识点三:安全性与用户体验 在实现验证码的过程中,还需要注意以下几点: - **安全性**:确保验证码的随机性,避免使用容易被破解的算法。 - **用户体验**:验证码的设计应尽可能美观且易于识别,避免过于复杂的图案或字符导致用户难以辨认。 - **可访问性**:对于视觉障碍的用户,应提供其他形式的验证方式,如音频验证码。 通过使用C#语言可以较为简单地实现功能完善的图片验证码,从而增强网站的安全性和用户体验。











using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;
using System.Drawing.Imaging;
public partial class CreateCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//调用第二中验证码!
//CreateImage(CreateRandomCode(4));
//调用第一种验证码!
String img = "";
img=re_img();
Session["img"] = img;
}
//!!!!!!!!第一种验证码开始!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public String re_img()
{
string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
Color[] color ={ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange,
Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] font ={ "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh",
"PMingLiU", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character ={ '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
//保存验证码的Cookie
HttpCookie anycookie = new HttpCookie("validateCookie");
剩余6页未读,继续阅读

- fengzhilong12042013-02-19还行吧,不过不是偶想要的效果额 - -

- 粉丝: 437
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- AGC AVC 系统软件操作说明.doc
- 基于计算机视觉的牛肉颜色自动分级技术研究的开题报告.docx
- 基于移动GIS的林业数据采集系统的研究与实现的开题报告.docx
- Autodesk Robot 结构设计分析软件标准入门手册.doc
- 第九章系统安全性计算机操作系统修订版汤子瀛教学文案.ppt
- 大阪OGIS公司煤气管道管理系统的设计与实现的开题报告.docx
- 电气工程及其自动化中存在的问题及解决方案.docx
- 收取外部电子邮件先登入Webmail再依序选择电子教案.ppt
- 大数据时代廉政文化建设新路径研究.docx
- 4D打印技术在人工智能+教育中的融合路径探究.docx
- 基于大数据对企业管理决策影响研究.docx
- 人工智能及其在计算机网络技术中的运用(1).docx
- C语言改错题分类总结.doc
- 设计院所信息化专业特点的自我实现.docx
- 2023年泵CAD中的原理方案设计模型.doc
- 2020年度人工智能与健康答案.doc


