用户登录验证码代码(参考网友)

事先申明:以下代码是在参考网友blog修改而成,其中将所得到验证码字符串validatestr以Session使用,用于登录页面提交验证。

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  Easybuy
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// validatecode 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class validatecode : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            this.GenerateImage(MakeString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string MakeString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string validatestr="";
InBlock.gif            
char ch;
InBlock.gif            Random rdom
=new Random();
InBlock.gif            
int n;
InBlock.gif
InBlock.gif            
for(int i=0;i<4;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                n
=rdom.Next();
InBlock.gif                
if(n%2==0)
InBlock.gif                    ch
=(char)('0'+(n%10));
InBlock.gif                
else
InBlock.gif                    ch
=(char)('A'+(n%26));
InBlock.gif                validatestr 
+=ch.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Session[
"ValidateCode"]=validatestr;
InBlock.gif            
return validatestr;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void GenerateImage(string checkCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Bitmap image
=new Bitmap(checkCode.Length*18,30);
InBlock.gif            Graphics g
= Graphics.FromImage(image);
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//生成随机生成器
InBlock.gif
                Random random = new Random();
InBlock.gif
InBlock.gif                
//清空图片背景色
InBlock.gif
                g.Clear(Color.White);
InBlock.gif
InBlock.gif                
//画图片的背景噪音线
InBlock.gif
                for(int i=0; i<25; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int x1 = random.Next(image.Width);
InBlock.gif                    
int x2 = random.Next(image.Width);
InBlock.gif                    
int y1 = random.Next(image.Height);
InBlock.gif                    
int y2 = random.Next(image.Height);
InBlock.gif
InBlock.gif                    g.DrawLine(
new Pen(Color.Silver), x1, y1, x2, y2);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                Font font 
= new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
InBlock.gif                System.Drawing.Drawing2D.LinearGradientBrush brush 
= new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2ftrue);
InBlock.gif                g.DrawString(checkCode, font, brush, 
22);
InBlock.gif
InBlock.gif                
//画图片的前景噪音点
InBlock.gif
                for(int i=0; i<100; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int x = random.Next(image.Width);
InBlock.gif                    
int y = random.Next(image.Height);
InBlock.gif
InBlock.gif                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
//画图片的边框线
InBlock.gif
                g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);
InBlock.gif
InBlock.gif                System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
InBlock.gif                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
InBlock.gif                Response.ClearContent();
InBlock.gif                Response.ContentType 
= "image/Gif";
InBlock.gif                Response.BinaryWrite(ms.ToArray());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                g.Dispose();
InBlock.gif                image.Dispose();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/feiren1421/archive/2006/03/24/357290.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值