ASP.NET 中图像验证码的实现

图像验证码在Web登录界面中很常见,以下是用C#写的一个简单例子。

1.首先创建一个 ValidateImage.aspx 页,注意引用System.Drawing和System.Drawing.Imaging两个命名空间。
代码如下:

None.gif      public   class  ValidateImage : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//生成验证码
InBlock.gif
            string validateCode=CreateValidateCode();
InBlock.gif            
//生成图像
InBlock.gif
            Bitmap bitmap=new Bitmap(7025);
InBlock.gif            
//设置图像背景色
InBlock.gif
            SetBgColor(bitmap,Color.Brown);
InBlock.gif            
// 绘制图像干扰
InBlock.gif
            DrawDisturb(bitmap);
InBlock.gif            
// 绘制验证码
InBlock.gif
            DrawValidateCode(bitmap, validateCode);
InBlock.gif            
// 保存验证码图像,等待输出
InBlock.gif
            bitmap.Save(Response.OutputStream, ImageFormat.Gif);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
// 生成 A-Z 的四位验证码
InBlock.gif
        private string CreateValidateCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string validateCode=string.Empty;
InBlock.gif            Random random
=new Random();
InBlock.gif
InBlock.gif            
for(int i=0; i<4; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//n=1~26
InBlock.gif
                int n=random.Next(26);
InBlock.gif                validateCode
+=(char)(n+65);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// 保存验证码
InBlock.gif
            Session["ValidateCode"]=validateCode;
InBlock.gif            
return validateCode;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private void SetBgColor(Bitmap bitmap,Color color)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for(int x=0; x<bitmap.Width; x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int y=0; y<bitmap.Height; y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    bitmap.SetPixel(x, y, color);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void DrawDisturb(Bitmap bitmap)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Random random
=new Random();
InBlock.gif
InBlock.gif            
for(int x=0; x<bitmap.Width; x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int y=0; y<bitmap.Height; y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// 50? 根据自己需要的干扰浓度进行设置
InBlock.gif
                    if(random.Next(100)<=50)
InBlock.gif                        bitmap.SetPixel(x, y, Color.White);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void DrawValidateCode(Bitmap bitmap, string validateCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 获取绘制器对象
InBlock.gif
            Graphics g=Graphics.FromImage(bitmap);
InBlock.gif
InBlock.gif            
// 设置绘制字体
InBlock.gif
            Font font=new Font("Arial"14, FontStyle.Bold | FontStyle.Italic);
InBlock.gif
InBlock.gif            
//绘制的起始位置
InBlock.gif
            int posX=2;
InBlock.gif            
int posY=2;
InBlock.gif
InBlock.gif            
// 绘制验证码图像
InBlock.gif
            g.DrawString(validateCode, font, Brushes.Black, posX, posY);
ExpandedSubBlockEnd.gif        }

2. 验证码的使用
在需要验证码的网页中,直接使用就可以了。
<img src="ValidateImage.aspx" style="border-color:#000000;border-width:1px;border-style:Solid">
相应的Session可以从Session["ValidateCode"]中取得

转载于:https://www.cnblogs.com/JohnXP/archive/2005/05/16/156383.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值