在前台放在如下四个控件

<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-★ 制作图片验证码 ★的更多相关文章

  1. Python简单的制作图片验证码

    -人人可以学Python--这里示范的验证码都是简单的,你也可以把字符扭曲 人人可以学Python.png Python第三方库无比强大,PIL 是python的一个d第三方图片处理模块,我们也可以使 ...

  2. webform:图片水印、验证码制作

    一.图片水印 1:引命名空间System.Drawing; 前端代码 <div> <asp:FileUpload ID="FileUpload1" runat=& ...

  3. webform(十)——图片水印和图片验证码

    两者都需要引入命名空间:using System.Drawing; 一.图片水印 前台Photoshuiyin.aspx代码: <div> <asp:FileUpload ID=&q ...

  4. WebForm 【上传图片】【图片验证码】

     上传图片(带水印)  1.获取要上传的图片 2.加水印 3.保存下来 using System.Drawing;   --绘画类命名空间 图片最后要用绝对路径保存       Server.MapP ...

  5. Webform 文件上传、 C#加图片水印 、 图片验证码

    文件上传:要使用控件 - FileUpload 1.如何判断是否选中文件? FileUpload.FileName - 选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.val ...

  6. Atitit 图片 验证码生成attilax总结

    Atitit 图片 验证码生成attilax总结 1.1. 图片验证码总结1 1.2. 镂空文字  打散 干扰线 文字扭曲 粘连2 1.1. 图片验证码总结 因此,CAPTCHA在图片验证码这一应用点 ...

  7. ASP.NET -- WebForm -- 给图片添加水印标记

    ASP.NET -- WebForm: 给图片添加水印标记 ASP.NET:使用 WebForm(C#) 制作一个简单的为图片添加水印的页面. 1. Test2.aspx文件 <%@ Page ...

  8. python之使用PIL模块制作随机验证码

    制作随机验证码,需要如下知识点: 1.随机验证码的制作(这里用的是random模块随机产生字符) 2.图片的制作 3.随机直线干扰项 4.其他随机干扰项 代码如下: from PIL import I ...

  9. int.TryParse非预期执行引发的思考 ASP.NET -- WebForm -- 给图片添加水印标记 Windows -- 使用批处理文件.bat删除旧文件

    int.TryParse非预期执行引发的思考   问题出现 这天在写一个页面,想谨慎些就用了int.TryParse,结果出问题了. 代码如下: Copy int id = 1000; //Reque ...

随机推荐

  1. U3D中GameObject.Find无法找到元件

    U3D中GameObject.Find 如果某元件SetActive(false)了,Find()无法找到 因为Find()只会帮你找出正在活动中的物件,所以在将物件关闭前,我们必须将此物件放至预先定 ...

  2. XBOX ONE游戏开发之登陆服务器(一)

    XBOX ONE游戏开发之登陆服务器(一) XBOX LIVE是微软自已的认证服务器, 当我们开发游戏时,如果是联网游戏,需要自已架设单点登陆(SSO)服务器 这个需要微软提供Relying Part ...

  3. 近几日小学flare3d,

    前言: Adobe虽然前2年砍掉了移动版flash player,以致H5大有可为, PC和移动端的2D世界不断被H5占领 不过FLASH已在3D方面,扩展出了新天地 FLARE 3D是 网页3D的新 ...

  4. red5安装时候出现服务不能启动异常

    Exception java.lang.ClassCastException: org.slf4j.helpers.BasicMDCAdapter cannot be cast to ch.qos.l ...

  5. 关于oracle with as用法

    with as语法–针对一个别名with tmp as (select * from tb_name) –针对多个别名with   tmp as (select * from tb_name),   ...

  6. DEV控件:gridControl常用属性设置

    1.隐藏最上面的GroupPanel  gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值  sValue=Table.Rows[g ...

  7. flask表单提交的两种方式

    一.通用方式 通用方式就是使用ajax或者$.post来提交. 前端html <form method="post" action="/mockservice&qu ...

  8. ViewModelLocator

    ViewModelLocator 这里先鼓舞下士气,ViewModelLocator很简单,甚至可以去掉,它不是Mvvm必须的.在初学Mvvm时,一般都是使用NuGet安装 MvvmLight框架,总 ...

  9. opencv8-GPU之相似性计算

    Opencv支持GPU计算,并且包含成一个gpu类用来方便调用,所以不需要去加上什么__global__什么的很方便,不过同时这个类还是有不足的,待opencv小组的更新和完善. 这里先介绍在之前的& ...

  10. JS运动从入门到兴奋1

    hello,我是沐晴,一个充满了才华,却靠了照骗走江湖的前端妹子.在这个充满PS的年代,这你们都信,哈哈,废话不多说,今天要分享的是关注JS运动的知识.楼主一直认为,不管学习什么,核心思想才是王道,掌 ...