登录验证码

// RandomImage.aspx.cs
     public   class  RandomImage : System.Web.UI.Page
    
{
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
//输出带有随机验证码的图片
            this.CreateCheckCodeImage(this.GenerateCheckCode());
        }


        
private string GenerateCheckCode()
        
{
            
//验证码长度
            int CODELENGTH = 4;
            
int number;
            
string strCode = string.Empty;

            Random r 
= new Random();

            
for(int i = 0; i < CODELENGTH; i++)
            
{
                number 
= r.Next();

                
//字符从0~9, A~Z中随机产生,对应的ASCII码分别为48~57, 65~90
                number = number % 36;
                
if(number < 10)
                    number 
+= 48;
                
else
                    number 
+= 55;

                strCode 
+= ((char)number).ToString();
            }


            
//在Cookie中保存验证码
            Response.Cookies.Add(new HttpCookie("CheckCode", strCode));
            
return strCode;
        }


        
private void CreateCheckCodeImage(string checkCode)
        
{
            
//若验证码为空,则直接返回
            if(checkCode == null || checkCode.Trim() == string.Empty)
                
return;

            
//根据验证码的长度确定输出图片的长度
            System.Drawing.Bitmap image = new Bitmap((int)Math.Ceiling(checkCode.Length * 15), 20);
        
            Graphics g 
= Graphics.FromImage(image);

            
try
            
{
                Random r 
= new Random();

                
//清空图片背景色
                g.Clear(Color.White);

                
//画图片的背景噪音线10条
                for(int i = 0; i < 10; i ++)
                
{
                    
int x1 = r.Next(image.Width);
                    
int x2 = r.Next(image.Width);
                    
int y1 = r.Next(image.Height);
                    
int y2 = r.Next(image.Height);

                    
//用银色画出噪音线
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }


                Font f 
= new Font("Arial"12, (FontStyle.Bold|FontStyle.Italic));

                
//线性渐变画刷
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.Purple, 1.2ftrue);
                g.DrawString(checkCode, f, brush, 
22);

                
//画图片的前景噪音点50个
                for(int i = 0; i < 50; i++)
                
{
                    
int x = r.Next(image.Width);
                    
int y = r.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(r.Next()));
                }


                
//画图片的框线
                g.DrawRectangle(new Pen(Color.SaddleBrown), 00, image.Width - 1, image.Height - 1);

                
//创建内存流用于输出图片
                using(System.IO.MemoryStream ms = new System.IO.MemoryStream())
                
{
                    
//图片格式制定为png
                    image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                    
//清除缓冲区流中的所有输出
                    Response.ClearContent();

                    
//输出流的HTTP MIME类型设置为"image/Png"
                    Response.ContentType = "image/Png";

                    
//输出图片的二进制流
                    Response.BinaryWrite(ms.ToArray());
                }

            }

            
finally
            
{
                
//释放Bitmap对象和Graphics对象
                g.Dispose();
                image.Dispose();
            }

        }


        
Web 窗体设计器生成的代码
    }


使用页面

< IMG id = " image1 "  alt = " 验证码 "  src = " RandomImage.aspx "   />
 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值