博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中实现验证码的参考代码
阅读量:6157 次
发布时间:2019-06-21

本文共 3959 字,大约阅读时间需要 13 分钟。

生成验证码的方法

using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Drawing;using System.Drawing.Imaging;using System.IO;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;public partial class YanZhengMa : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        //  首先调用RndNum函数生成4位数字的验证码        string str_ValidateCode = RndNum(4);        //  然后把它保存在用于验证的Session里边        Session["imageCode"] = str_ValidateCode;        //调用CreateImage函数,把验证码绘制到图片中        CreateImage(str_ValidateCode);    }    private string RndNum(int VcodeNum)    {        string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +         ",q,r,s,t,u,v,w,x,y,z";        string[] VcArray = Vchar.Split(new Char[] { ',' });        string VNum = "";        //int temp = -1;        Random rand = new Random();        for (int i = 1; i < VcodeNum + 1; i++)        {            int t = rand.Next(36);            VNum += VcArray[t];        }        return VNum;    }    //生成随机颜色    private Color GetRandomColor()    {        Random RandomNum_First = new Random((int)DateTime.Now.Ticks);        //  对于C#的随机数,没什么好说的        System.Threading.Thread.Sleep(RandomNum_First.Next(50));        Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);        //  为了在白色背景上显示,尽量生成深色        int int_Red = RandomNum_First.Next(256);        int int_Green = RandomNum_Sencond.Next(256);        int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;        int_Blue = (int_Blue > 255) ? 255 : int_Blue;        return Color.FromArgb(int_Red, int_Green, int_Blue);    }    public void CreateImage(string str_ValidateCode)    {        int int_ImageWidth = str_ValidateCode.Length * 13;        Random newRandom = new Random();        //  图高20px        Bitmap theBitmap = new Bitmap(int_ImageWidth, 20);        Graphics theGraphics = Graphics.FromImage(theBitmap);        //  白色背景        theGraphics.Clear(Color.White);        //  灰色边框        theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, 19);        //  10pt的字体        Font theFont = new Font("Arial", 10);        for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)        {            string str_char = str_ValidateCode.Substring(int_index, 1);            Brush newBrush = new SolidBrush(GetRandomColor());            Point thePos = new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));            theGraphics.DrawString(str_char, theFont, newBrush, thePos);        }        //  将生成的图片发回客户端        MemoryStream ms = new MemoryStream();        theBitmap.Save(ms, ImageFormat.Jpeg);        Response.ClearContent(); //需要输出图象信息 要修改HTTP头         Response.ContentType = "image/Png";        Response.BinaryWrite(ms.ToArray());        theGraphics.Dispose();        theBitmap.Dispose();        Response.End();    }}

在前台调用的时候的部分代码

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
1
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeBehind
=
"
Default.aspx.cs
"
Inherits
=
"
CRM._Default
"
%>
2
3
 
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
4
5
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
6
<
head runat
=
"
server
"
>
7
<
title
>
无标题页
</
title
>
8
<
script type
=
"
text/javascript
"
>
9
function UpdateImage()
10
{
11
var n
=
Math.random();
12
document.getElementById(
"
image
"
).src
=
"
System/CheckCode.aspx?r=
"
+
n;
13
}
14
</
script
>
15
</
head
>
16
<
body
>
17
<
form id
=
"
form1
"
runat
=
"
server
"
>
18
<
div
>
19
20
21
<
img id
=
"
image
"
src
=
"
System/CheckCode.aspx
"
/>
22
<
a href
=
"
javascript:UpdateImage()
"
>
看不清,换一张
</
a
>
23
24
</
div
>
25
</
form
>
26
</
body
>
27
</
html
>

转载于:https://www.cnblogs.com/wsl2011/archive/2011/05/12/2044693.html

你可能感兴趣的文章
Windows DHCP Server基于MAC地址过滤客户端请求实现IP地址的分配
查看>>
命令查询每个文件文件数
查看>>
《跟阿铭学Linux》第8章 文档的压缩与打包:课后习题与答案
查看>>
RAC表决磁盘管理和维护
查看>>
Apache通过mod_php5支持PHP
查看>>
发布一个TCP 吞吐性能测试小工具
查看>>
java学习:jdbc连接示例
查看>>
PHP执行批量mysql语句
查看>>
Extjs4.1.x 框架搭建 采用Application动态按需加载MVC各模块
查看>>
Silverlight 如何手动打包xap
查看>>
建筑电气暖通给排水协作流程
查看>>
JavaScript面向对象编程深入分析(2)
查看>>
linux 编码转换
查看>>
POJ-2287 Tian Ji -- The Horse Racing 贪心规则在动态规划中的应用 Or 纯贪心
查看>>
Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月7日-1月14日)
查看>>
关于C#导出 文本文件
查看>>
maclean liu的oracle学习经历--长篇连载
查看>>
分享:动态库的链接和链接选项-L,-rpath-link,-rpath
查看>>
Javascript一些小细节
查看>>
禁用ViewState
查看>>