生成验证码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateCheckCodeImage(this.GenerateCheckCode());
    }

    private string GenerateCheckCode()
    {
        string str = string.Empty;
        Random random = new Random();
        for (int i = 0; i 
<  5 ; i++)
        {
            int num 
= random.Next();
            
str  = str  + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
        }
        Response.Cookies.Add(new HttpCookie("ValidateCode", str));
        return str;
    }

    private void CreateCheckCodeImage(string checkCode)
    {
        if ((checkCode !
= null)  && (checkCode.Trim() ! = string.Empty))
        
{
            Bitmap image 
= new  Bitmap((int)Math.Ceiling(checkCode.Length * 12.5), 20);
            Graphics graphics 
= Graphics.FromImage(image);
            
try
            {
                Random random 
= new  Random();
                graphics.Clear(Color.White);
                for (int i 
= 0;  i < 0x19; i++)
                {
                    int num2 
= random.Next(image.Width);
                    
int num3  = random.Next(image.Width);
                    
int num4  = random.Next(image.Height);
                    
int num5  = random.Next(image.Height);
                    
graphics.DrawLine(new Pen(Color.Silver), num2, num4, num3, num5);
                }
                Font font 
= new  Font("Arial", 12f, FontStyle.Italic | FontStyle.Bold);
                System.Drawing.Drawing2D.LinearGradientBrush brush 
= new  System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                graphics.DrawString(checkCode, font, brush, (float)2f, (float)2f);
                for (int j 
= 0;  j < 100; j++)
                {
                    int x 
= random.Next(image.Width);
                    
int y  = random.Next(image.Height);
                    
image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                MemoryStream stream 
= new  MemoryStream();
                image.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
                base.Response.ClearContent();
                base.Response.ContentType 
= "image/Gif" ;
                base.Response.BinaryWrite(stream.ToArray());
            }
            finally
            {
                graphics.Dispose();
                image.Dispose();
            }
        }
    }

 

 


}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值