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 ...
随机推荐
- C++ create_task详解
IAsyncOperation<T>^ asyncOperation = create_async( []() { return create_task(FirstAsync(...)) ...
- [No000058]一口气读完一本英语书
个人:"如何学好英语?"99个人会回答:"去,读英文原著." 然而这事儿吧,恐怕比坚持背单词还难.无论少侠们背过多少单词,最后都会败在生词太多.句子太难的坎儿上 ...
- [No000049]狗日的中年——姜文
文件名 大小 [No000049]狗日的中年——姜文.7z 228KB
- 渗透攻防工具篇-后渗透阶段的Meterpreter
作者:坏蛋链接:https://zhuanlan.zhihu.com/p/23677530来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 前言 熟悉Metasploit ...
- 内裤:DataTable转Model
public class ConvertHelper<T> where T : new() { /// <summary> /// 利用反射和泛型 /// </summa ...
- Java多态:upcast和downcast
upcast例: public class Test { public static void main(String[] args) { Cup aCup = new BrokenCup(); aC ...
- 根据Unicode编码用C#语言把它转换成汉字的代码
rt 根据所具有的Unicode编码用C#语言把它转换成汉字的代码 var s = System.Web.HttpUtility.HtmlDecode(Utf8Str); var o = Newton ...
- 转:如何在32位程序中突破地址空间4G的限制
//如何在32位程序中突破地址空间4G的限制 //首先要获得内存中锁定页的权限 #define _WIN32_WINNT 0x0501 //xp系统 #include <windows.h> ...
- filestream read方法 循环读取固定文件
1.循环读取啊,byte[]可以定义为1024或者2049等等,不要超过int的maxvalue就可以.然后取出来操作完再去取. FileStream stream = new FileStream( ...
- zabbix-proxy3.0.4编译安装
数据库配置: innodb_file_per_table=1 安装mysql rpm -ivh http://dev.mysql.com/get/mysql-community-release-e ...