C#-WebForm-★ 制作图片验证码 ★
在前台放在如下四个控件
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <%--TextBox-等待输入验证码--%>
<asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" /> <%--Image-显示验证码图片--%>
<asp:Button ID="Button1" runat="server" Text="提交验证" /> <%--Button-提交进行验证--%>
<asp:Label ID="Label1" runat="server" Text="验证中..."></asp:Label> <%--Label-显示验证结果--%>
</div>

此时验证码为空,不显示任何东西
步骤:
一、给验证码图片控件加一个连接,此连接是.aspx网页,此网页不需要前台,只需要打开时后台做一个验证码图片展示出来即可
<asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" />
二、在YZM.aspx后台中制作简单验证码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing; public partial class YZM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Random r = new Random(); //制作“位图”(指定长宽的矩形区域)
Bitmap img = new Bitmap(,); //准备制作-
//设定画布
Graphics g = Graphics.FromImage(img);
//输出的字符串
string all = "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string s = "";
for (int i = ; i <= ; i++)
{
s += all.Substring(r.Next(all.Length),);
}
//字符串的字体
Font f=new Font ("微软雅黑",);
//字体的颜色
Brush b=new SolidBrush(Color.Red);
//起始位置
PointF p=new PointF (,);
//进行制作-
g.DrawString(s, f, b, p); //进行保存(保存到流中)
img.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Png);
Response.End();
}
}
YZM.aspx后台代码
效果:

三、设置<提交验证>功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btn_verification.Click+=btn_verification_Click;
}
//<提交验证>按钮点击事件
void btn_verification_Click(object sender, EventArgs e)
{
string s1 = txt_userYZM.Text;
string s2 = Session["YZM"].ToString();
if (s1.ToUpper() == s2.ToUpper())
{
Label1.Text = "验证成功!";
}
else
{
Label1.Text = "验证失败!";
}
}
}
<提交验证>按钮事件
效果:

验证成功自动刷新
四、设置点击验证码切换验证码 - 前端JS
<script type="text/javascript">
var a = ;
document.getElementById("Image1").onclick = function () {
this.setAttribute("src", "yzm.aspx?id=" + a);
a++;
}
</script>
设置点击验证码切换验证码
五、设置验证码背景色和干扰线 填充矩形区域:FillRectangle
//设定背景色
g.FillRectangle(new SolidBrush(clist[r.Next(clist.Count)]), , , , ); //设置干扰线
for (int i = ; i <= ; i++)
{
//随机颜色
Color c_line = clist[r.Next(, clist.Count)];
//干扰线颜色、粗细
Pen p_line = new Pen(new SolidBrush(c_line), r.Next(, ));
//画干扰线
g.DrawLine(p_line, new PointF(r.Next(, ), r.Next(, )), new PointF(r.Next(, ), r.Next(, )));
}
设置验证码背景色和干扰线
===========================================
验证码图片后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing; public partial class YZM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Random r = new Random();
//颜色集合
List<Color> clist = new List<Color>();
clist.Add(Color.Yellow);
clist.Add(Color.Pink);
clist.Add(Color.Blue);
clist.Add(Color.Green);
clist.Add(Color.Orange);
clist.Add(Color.Black); //制作“位图”(指定长宽的矩形区域)
Bitmap img = new Bitmap(, ); //设定画布
Graphics g = Graphics.FromImage(img); //设定背景色
g.FillRectangle(new SolidBrush(clist[r.Next(clist.Count)]), , , , ); //设置干扰线
for (int i = ; i <= ; i++)
{
//随机颜色
Color c_line = clist[r.Next(, clist.Count)];
//干扰线颜色、粗细
Pen p_line = new Pen(new SolidBrush(c_line), r.Next(, ));
//画干扰线
g.DrawLine(p_line, new PointF(r.Next(, ), r.Next(, )), new PointF(r.Next(, ), r.Next(, )));
} //输出的字符串
string all = "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string s = "";
for (int i = ; i <= ; i++)
{
s += all.Substring(r.Next(all.Length), );
}
//生成的验证码放入全局变量中
Session["YZM"] = s;
//字符串的字体
Font f = new Font("微软雅黑", );
//字体的颜色
Brush b = new SolidBrush(Color.Red);
//起始位置
PointF p = new PointF(, );
//进行制作-
g.DrawString(s, f, b, p); //进行保存(保存到流中)
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
Response.End();
}
}
验证码图片的后台代码

C#-WebForm-★ 制作图片验证码 ★的更多相关文章
- Python简单的制作图片验证码
-人人可以学Python--这里示范的验证码都是简单的,你也可以把字符扭曲 人人可以学Python.png Python第三方库无比强大,PIL 是python的一个d第三方图片处理模块,我们也可以使 ...
- webform:图片水印、验证码制作
一.图片水印 1:引命名空间System.Drawing; 前端代码 <div> <asp:FileUpload ID="FileUpload1" runat=& ...
- webform(十)——图片水印和图片验证码
两者都需要引入命名空间:using System.Drawing; 一.图片水印 前台Photoshuiyin.aspx代码: <div> <asp:FileUpload ID=&q ...
- WebForm 【上传图片】【图片验证码】
上传图片(带水印) 1.获取要上传的图片 2.加水印 3.保存下来 using System.Drawing; --绘画类命名空间 图片最后要用绝对路径保存 Server.MapP ...
- Webform 文件上传、 C#加图片水印 、 图片验证码
文件上传:要使用控件 - FileUpload 1.如何判断是否选中文件? FileUpload.FileName - 选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.val ...
- Atitit 图片 验证码生成attilax总结
Atitit 图片 验证码生成attilax总结 1.1. 图片验证码总结1 1.2. 镂空文字 打散 干扰线 文字扭曲 粘连2 1.1. 图片验证码总结 因此,CAPTCHA在图片验证码这一应用点 ...
- ASP.NET -- WebForm -- 给图片添加水印标记
ASP.NET -- WebForm: 给图片添加水印标记 ASP.NET:使用 WebForm(C#) 制作一个简单的为图片添加水印的页面. 1. Test2.aspx文件 <%@ Page ...
- python之使用PIL模块制作随机验证码
制作随机验证码,需要如下知识点: 1.随机验证码的制作(这里用的是random模块随机产生字符) 2.图片的制作 3.随机直线干扰项 4.其他随机干扰项 代码如下: from PIL import I ...
- int.TryParse非预期执行引发的思考 ASP.NET -- WebForm -- 给图片添加水印标记 Windows -- 使用批处理文件.bat删除旧文件
int.TryParse非预期执行引发的思考 问题出现 这天在写一个页面,想谨慎些就用了int.TryParse,结果出问题了. 代码如下: Copy int id = 1000; //Reque ...
随机推荐
- Flex数据绑定陷阱(一)
Flex数据绑定陷阱:常见的误用和错误 当构建Flex或者Adobe AIR程序时,将一个对象的值自动的传递给另一个对象这种处理是数据绑定最常 用并最有用的特征之一. 尽管如此,同时数据绑定会减缓程序 ...
- vijos[1355]车队过桥问题
描述 现有N辆车要按顺序通过一个单向的小桥,由于小桥太窄,不能有两辆车并排通过.另外,由于小桥建造的时间已经很久,只能承受有限的重量,记为Max(吨).管理员将N辆车按初始的顺序分组,每次让一个组过桥 ...
- bzoj1036 [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 12646 Solved: 5085 [Subm ...
- Android M新的运行时权限开发者需要知道的一切
android M 的名字官方刚发布不久,最终正式版即将来临!android在不断发展,最近的更新 M 非常不同,一些主要的变化例如运行时权限将有颠覆性影响.惊讶的是android社区鲜有谈论这事儿, ...
- Notes: sensitivity & specificity
terminology: True positive (TP); False positive (FP): originally negative; True negative (TN); False ...
- java script 确认框
<input name="Submit" type="submit" class="inputedit" value="删除 ...
- vs2013怎么打开vs2010的解决方案
1.直接用vs2013打开解决方案的sln文件,vs会自动进行转换的2.或者你用记事本的方式打开sln文件 将版本号改一下Microsoft Visual Studio Solution File, ...
- 基于JSch的Sftp工具类
本Sftp工具类的API如下所示. 1)构造方法摘要 Sftp(String host, int port, int timeout, String username, String password ...
- iOS开发 传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- Android动画学习笔记-Android Animation
Android动画学习笔记-Android Animation 3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...