在前台放在如下四个控件

<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. 南邮CTF隐写之丘比龙的女神

    刚开始下载下图片来 习惯性的binwalk一下 没发现东西 formost一下也没分离出来 扔进c32asm中发现有nvshen.jpg 于是改后缀名字为.zip 解压nvshen.jpg发现无法解压 ...

  2. HTML5网站如何做到完全不需要jQuery

    jQuery是现在最流行的JavaScript工具库. 据统计,目前全世界57.3%的网站使用它.也就是说,10个网站里面,有6个使用jQuery.如果只考察使用工具库的网站,这个比例就会上升到惊人的 ...

  3. background-origin 设置背景图片原始起始位置

  4. HTML 学习笔记 CSS样式(边框)

    元素的边框(border)是围绕元素内容和内边距的一条或多条线 CSS border 属性允许你规定边框的样式 宽度和颜色 CSS 边框 在 HTML 中,我们使用表格来创建文本周围的边框,但是通过使 ...

  5. C# 根据正则表达式来判断输入的是不是数字

    最近在做输入判断的时候出现了一个需要判断输入合法性的问题,就是判断输入的是不是数字,判断方法是根据正则表达式来判断,具体方法如下: private bool IsRightNum(string str ...

  6. Notes: Principles of fMRI 1 (Coursera)

    course link: https://class.coursera.org/fmri1-001 Part 1  ❤ Three fundmental goals in fMRI: localiza ...

  7. Linux常用命令笔记

    ~ 我的home目录/ 系统根目录进入home目录:cd \进入跟目录:cd /Maven编译:mvn clean deploy -U -Dmaven.test.skip=true dependenc ...

  8. Linux Linux程序练习十六(进程间的通信信号版)

    /* * 题目: * 编写程序,要去实现如下功能: 父进程创建子进程1和子进程2.子进程1向子进程2发送可靠信号,并传送额外数据为子进程1的pid*2; 子进程2接受可靠信号的值,并发送给父进程,父进 ...

  9. QT QMianWindow类

    QMianWindow是一个为用户提供主窗口程序的类,包含一个菜单栏(menu bar).及一个中心部件(central widget),是许多应用程序的基础,如文本编辑器等. QMainWindow ...

  10. EntityFramework 启用迁移 Enable-Migrations 报异常 "No context type was found in the assembly"

    转自:http://www.cnblogs.com/stevenhqq/archive/2013/04/18/3028350.html 以前做项目的时候,没有采用分类库的形式,所以迁移一致非常顺利,没 ...