ASP.NET 生成图形验证码 (C#版本)

命名空间:

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          private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            string checkCode = CreateRandomCode(4);
InBlock.gif            Session[
"CheckCode"= checkCode;
InBlock.gif            CreateImage(checkCode);
ExpandedBlockEnd.gif        }

None.gif        
private   string  CreateRandomCode( int  codeCount)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            
// 函数功能:产生数字和字符混合的随机字符串
InBlock.gif
            string allChar = "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,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" ;
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(35);
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        }

None.gif
None.gif
None.gif        
private   void  CreateImage( string  checkCode)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.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.Azure);//字母白色
InBlock.gif            
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
InBlock.gif
            g.Clear(Color.Gray);//背景灰色
InBlock.gif
            g.DrawString(checkCode, f, b, 33);
InBlock.gif
InBlock.gif            Pen blackPen 
= new Pen(Color.Black, 0);
InBlock.gif            Random rand 
= new Random();
InBlock.gif
InBlock.gif
InBlock.gif            System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
InBlock.gif            image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
InBlock.gif            Response.ClearContent();
InBlock.gif            Response.ContentType 
= "image/Jpeg";
InBlock.gif            Response.BinaryWrite(ms.ToArray());
InBlock.gif            g.Dispose();
InBlock.gif            image.Dispose();
InBlock.gif
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/xzlang/archive/2006/10/02/520361.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值