验证码的三种做法[纯数字][纯汉字][字母\数字\汉字混合]C#

效果如图(我确实懒了点,按纽的名字都没有改,哈哈)
需要说明的是,因为提交对比后,页面是重新刷新了,所以文本框里面看见的和后面图片显示不一样,这个不需要太多说明吧!!
 混合模式刷新出现约界错误 已经被 stone(QQ9337874) 修正!!
aaaa.jpg
bbb.jpg
ccc.JPG
ddd.JPG

下面是GDI+绘图的代码段,以及显示的调用等
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Drawing.Drawing2D;
None.gif
using  System.Drawing.Imaging;
None.gif
using  System.Drawing.Text;
None.gif
using  System.Drawing;
None.gif
using  System.Text; 
None.gif
None.gif
public  partial  class  png : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private readonly string ImagePath = "Validator.jpg";
InBlock.gif    
private static string gif = "";
InBlock.gif
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
switch (Request.QueryString["aa"])
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
case "1":
InBlock.gif                gif 
= stxt();
InBlock.gif                Session[
"gif"]= stxt();
InBlock.gif                
break;
InBlock.gif            
case "2":
InBlock.gif                gif 
= GetRandomint();
InBlock.gif                 Session[
"gif"]= GetRandomint();
InBlock.gif                
break;
InBlock.gif            
case "3":
InBlock.gif                gif 
= RndNum(3);
InBlock.gif                 Session[
"gif"]= RndNum(3);
InBlock.gif                
break;
InBlock.gif            
default:
InBlock.gif                gif 
= RndNum(3);
InBlock.gif                 Session[
"gif"]= GetRandomint();
InBlock.gif                
break;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////创建Bmp位图
InBlock.gif        Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(ImagePath));
InBlock.gif        Graphics graphicImage 
= Graphics.FromImage(bitMapImage);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////设置画笔的输出模式
InBlock.gif        graphicImage.SmoothingMode = SmoothingMode.HighSpeed;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////添加文本字符串
InBlock.gif        graphicImage.DrawString(gif, new Font("Arial"20, FontStyle.Bold), SystemBrushes.WindowText, new Point(00));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////设置图像输出的格式
InBlock.gif        Response.ContentType = "image/jpeg";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////保存数据流
InBlock.gif        bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////释放占用的资源
InBlock.gif        graphicImage.Dispose();
InBlock.gif        bitMapImage.Dispose();
ExpandedSubBlockEnd.gif    }

返回纯数字

None.gif      private  String GetRandomint()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Random random 
= new Random();
InBlock.gif        
return (random.Next(100000999999).ToString());
ExpandedBlockEnd.gif    }


返回文字\数字\字母混合的

None.gif public   static  String RndNum( int  VcodeNum)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        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,W,X,Y,Z,呵,哈,弹,簧,秤,嬉,戏";
InBlock.gif        String[] VcArray 
= Vchar.Split(',');
InBlock.gif        String VNum 
= "";
InBlock.gif        Random random 
= new Random();
InBlock.gif        
for (int i = 1; i <= VcodeNum; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int iNum = 0;
InBlock.gif            
while ((iNum = Convert.ToInt32(VcArray.Length * random.NextDouble())) ==VcArray.Length )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                iNum 
= Convert.ToInt32(VcArray.Length * random.NextDouble());
ExpandedSubBlockEnd.gif            }

InBlock.gif            VNum 
+= VcArray[iNum];
InBlock.gif            
// VNum += VcArray[Convert.ToInt32(VcArray.Length*random.NextDouble())];
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
return VNum;
ExpandedBlockEnd.gif    }
 

返回汉字的
None.gif         public   static   object [] CreateRegionCode( int  strlength) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            
//定义一个字符串数组储存汉字编码的组成元素 
ExpandedSubBlockStart.gifContractedSubBlock.gif
            string[] rBase=new String [16]dot.gif{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}
InBlock.gif             
InBlock.gif            Random rnd
=new Random(); 
InBlock.gif         
InBlock.gif            
//定义一个object数组用来 
InBlock.gif
            object[] bytes=new object[strlength]; 
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**//*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中 
InBlock.gif             每个汉字有四个区位码组成 
InBlock.gif             区位码第1位和区位码第2位作为字节数组第一个元素 
InBlock.gif             区位码第3位和区位码第4位作为字节数组第二个元素 
ExpandedSubBlockEnd.gif            
*/
 
InBlock.gif            
for(int i=0;i<strlength;i++
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
//区位码第1位 
InBlock.gif
                int r1=rnd.Next(11,14); 
InBlock.gif                
string str_r1=rBase[r1].Trim(); 
InBlock.gif 
InBlock.gif                
//区位码第2位 
InBlock.gif
                rnd=new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);//更换随机数发生器的种子避免产生重复值 
InBlock.gif
                int r2; 
InBlock.gif                
if (r1==13
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    r2
=rnd.Next(0,7); 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    r2
=rnd.Next(0,16); 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
string str_r2=rBase[r2].Trim(); 
InBlock.gif 
InBlock.gif                
//区位码第3位 
InBlock.gif
                rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i); 
InBlock.gif                
int r3=rnd.Next(10,16); 
InBlock.gif                
string str_r3=rBase[r3].Trim(); 
InBlock.gif 
InBlock.gif                
//区位码第4位 
InBlock.gif
                rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i); 
InBlock.gif                
int r4; 
InBlock.gif                
if (r3==10
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    r4
=rnd.Next(1,16); 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
else if (r3==15
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    r4
=rnd.Next(0,15); 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    r4
=rnd.Next(0,16); 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
string str_r4=rBase[r4].Trim(); 
InBlock.gif 
InBlock.gif                
//定义两个字节变量存储产生的随机汉字区位码 
InBlock.gif
                byte byte1=Convert.ToByte(str_r1 + str_r2,16); 
InBlock.gif                
byte byte2=Convert.ToByte(str_r3 + str_r4,16); 
InBlock.gif                
//将两个字节变量存储在字节数组中 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                byte[] str_r=new byte[]dot.gif{byte1,byte2}
InBlock.gif 
InBlock.gif                
//将产生的一个汉字的字节数组放入object数组中 
InBlock.gif
                bytes.SetValue(str_r,i); 
InBlock.gif                 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif 
InBlock.gif            
return bytes; 
InBlock.gif 
ExpandedBlockEnd.gif            }

None.gif    
private   string  stxt()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Encoding gb 
= Encoding.GetEncoding("gb2312");
InBlock.gif
InBlock.gif        
//调用函数产生4个随机中文汉字编码 
InBlock.gif
        object[] bytes = CreateRegionCode(3);
InBlock.gif
InBlock.gif        
//根据汉字编码的字节数组解码出中文汉字 
InBlock.gif
        string str1 = gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
InBlock.gif        
string str2 = gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
InBlock.gif        
string str3 = gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
InBlock.gif
InBlock.gif        
string txt = str1 + str2 + str3;
InBlock.gif        
return txt;
ExpandedBlockEnd.gif    }

我们调用页的代码如下

HTML代码
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" AutoEventWireup="true" CodeFile="view.aspx.cs" Inherits="view"  %>
None.gif
None.gif
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
None.gif
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
None.gif
< head  runat ="server" >
None.gif    
< title > 无标题页 </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form  id ="form1"  runat ="server" >
None.gif    
< div >
None.gif        
&nbsp; < asp:TextBox  ID ="TextBox1"  runat ="server" ></ asp:TextBox >
None.gif        
< asp:Image  ID ="Image1"  runat ="server"  ImageUrl ="png.aspx"   />< br  />
None.gif        
< br  />
None.gif        
&nbsp; < asp:Button  ID ="Button2"  runat ="server"  OnClick ="Button2_Click"  Text ="Button"   />
None.gif        
< asp:DropDownList  ID ="DropDownList1"  runat ="server"  AutoPostBack ="True"  OnSelectedIndexChanged ="DropDownList1_SelectedIndexChanged" >
None.gif            
< asp:ListItem  Value ="3" > 默认 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="1" > 文字 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="2" > 数字 </ asp:ListItem >
None.gif            
< asp:ListItem  Value ="3" > 混合 </ asp:ListItem >
None.gif        
</ asp:DropDownList ></ div >
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >

CS代码
None.gif      protected   void  DropDownList1_SelectedIndexChanged( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
switch (DropDownList1.SelectedValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
case "1":
InBlock.gif                Image1.ImageUrl 
= "png.aspx?aa=1";
InBlock.gif                
break;
InBlock.gif            
case "2":
InBlock.gif                Image1.ImageUrl 
= "png.aspx?aa=2";
InBlock.gif                
break;
InBlock.gif            
case "3":
InBlock.gif                Image1.ImageUrl 
= "png.aspx?aa=3";
InBlock.gif                
break;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif    
protected   void  Button2_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
if (TextBox1.Text == Session["gif"].ToString())
InBlock.gif            Response.Write(
"OK,正确");
InBlock.gif        
else
InBlock.gif            Response.Write(
"验证码不符合");
ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/hzuIT/articles/473537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值