简单的生成登录验证码的程序

Function.cs
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif        
/// 生成随机数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="codeCount">要生成的随机数位数</param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          public   static   string  CreateRandomCode( int  codeCount)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string allChar = "0,1,2,3,4,5,6,7,8,9";
InBlock.gif            
string[] allCharArray = allChar.Split(',');
InBlock.gif            
string randomCode = "";
InBlock.gif            
int temp = -1;
InBlock.gif
InBlock.gif            Random rand 
= new Random();
InBlock.gif            
for(int i = 0; i < codeCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(temp != -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    rand 
= new Random(i*temp*((int)DateTime.Now.Ticks));
ExpandedSubBlockEnd.gif                }

InBlock.gif                
int t = rand.Next(9);
InBlock.gif                
if(temp == t)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return CreateRandomCode(codeCount);
ExpandedSubBlockEnd.gif                }

InBlock.gif                temp 
= t;
InBlock.gif                randomCode 
+= allCharArray[t];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return randomCode;
ExpandedBlockEnd.gif        }

ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 生成验证码图片
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="checkCode"></param>

None.gif          public   static   void  CreateImage( string  checkCode)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
int iwidth = (int)(checkCode.Length * 11.5);
InBlock.gif            System.Drawing.Bitmap image 
= new System.Drawing.Bitmap(iwidth, 20);
InBlock.gif            Graphics g 
= Graphics.FromImage(image);
InBlock.gif            Font f 
= new System.Drawing.Font("Arial"10, System.Drawing.FontStyle.Bold);
InBlock.gif            Brush b 
= new System.Drawing.SolidBrush(Color.White);
InBlock.gif            
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
InBlock.gif
            g.Clear(Color.Blue);
InBlock.gif            g.DrawString(checkCode, f, b, 
33);
InBlock.gif
InBlock.gif            Pen blackPen 
= new Pen(Color.Brown,0);
InBlock.gif            Random rand 
= new Random();
InBlock.gif            
for (int i=0;i<5;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int y = rand.Next(image.Height);
InBlock.gif
InBlock.gif                
//g.DrawLine(blackPen,0,y,image.Width,y);
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
InBlock.gif            System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
InBlock.gif            
for(int i=0; i<90; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int x = rand.Next(image.Width);
InBlock.gif                
int y = rand.Next(image.Height);
InBlock.gif                image.SetPixel(x, y, Color.FromArgb(rand.Next()));
ExpandedSubBlockEnd.gif            }

InBlock.gif            image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
InBlock.gif            System.Web.HttpContext.Current.Response.ClearContent();
InBlock.gif            System.Web.HttpContext.Current.Response.ContentType 
= "image/Jpeg";
InBlock.gif            System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
InBlock.gif            g.Dispose();
InBlock.gif            image.Dispose();
ExpandedBlockEnd.gif        }

ValidateCode.aspx.cs
None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            string checkCode = LemongTree.BLL.Function.CreateRandomCode(4);
InBlock.gif            Session[
"CheckCode"= checkCode;
InBlock.gif            LemongTree.BLL.Function.CreateImage(checkCode);
ExpandedBlockEnd.gif        }

.aspx文件的调用
<?xml:namespace prefix = asp /><asp:image id="Image1" runat="server" ImageUrl="validatecode.aspx" />

转载于:https://www.cnblogs.com/lemongtree/archive/2006/01/10/314865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值