 
                    
                
         
    
    
    
	
	
	
![]()
![]() Code
Code
![]() <%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Page Language="C#" AutoEventWireup="True" %> 
![]() <%@ import Namespace="System"%>
<%@ import Namespace="System"%>
![]() <%@ import Namespace="System.Data"%>
<%@ import Namespace="System.Data"%>
![]() <%@ import Namespace="System.Configuration"%>
<%@ import Namespace="System.Configuration"%>
![]() <%@ import Namespace="System.Collections"%>
<%@ import Namespace="System.Collections"%>
![]() <%@ import Namespace="System.Web"%>
<%@ import Namespace="System.Web"%>
![]() <%@ import Namespace="System.Web.Security"%>
<%@ import Namespace="System.Web.Security"%>
![]() <%@ import Namespace="System.Web.UI"%>
<%@ import Namespace="System.Web.UI"%>
![]() <%@ import Namespace="System.Web.UI.WebControls"%>
<%@ import Namespace="System.Web.UI.WebControls"%>
![]() <%@ import Namespace="System.Web.UI.WebControls.WebParts"%>
<%@ import Namespace="System.Web.UI.WebControls.WebParts"%>
![]() <%@ import Namespace="System.Web.UI.HtmlControls"%>
<%@ import Namespace="System.Web.UI.HtmlControls"%>
![]() <%@ import Namespace="System.Drawing"%>
<%@ import Namespace="System.Drawing"%>
![]() <%@ import Namespace="System.Drawing.Imaging"%>
<%@ import Namespace="System.Drawing.Imaging"%>
![]() <%@ import Namespace="System.IO"%>
<%@ import Namespace="System.IO"%>
![]() <script runat="server">
<script runat="server"> 
![]() private void Page_Load(object sender, System.EventArgs e)
private void Page_Load(object sender, System.EventArgs e) 
![]() {
    {
![]() string tmp = RndNum(4);
        string tmp = RndNum(4);
![]() HttpCookie a = new HttpCookie("ImageV", tmp);
        HttpCookie a = new HttpCookie("ImageV", tmp);
![]() Response.Cookies.Add(a);
        Response.Cookies.Add(a);
![]() this.ValidateCode(tmp);
        this.ValidateCode(tmp); 
![]()
![]() }
    }
![]() private void ValidateCode(string VNum)
    private void ValidateCode(string VNum)
![]() {
    {
![]() Bitmap Img = null;
        Bitmap Img = null;
![]() Graphics g = null;
        Graphics g = null;
![]() MemoryStream ms = null;
        MemoryStream ms = null;
![]()
![]() int gheight = VNum.Length * 12;
        int gheight = VNum.Length * 12;
![]() Img = new Bitmap(gheight, 25);
        Img = new Bitmap(gheight, 25);
![]() g = Graphics.FromImage(Img);
        g = Graphics.FromImage(Img);
![]() //背景颜色
        //背景颜色 
![]() g.Clear(Color.LightSteelBlue);
        g.Clear(Color.LightSteelBlue);
![]() //文字字体
        //文字字体 
![]() Font f = new Font("Arial Black", 10);
        Font f = new Font("Arial Black", 10);
![]() //文字颜色
        //文字颜色 
![]() SolidBrush s = new SolidBrush(Color.RoyalBlue);
        SolidBrush s = new SolidBrush(Color.RoyalBlue);
![]() g.DrawString(VNum, f, s, 3, 3);
        g.DrawString(VNum, f, s, 3, 3);
![]() ms = new MemoryStream();
        ms = new MemoryStream();
![]() Img.Save(ms, ImageFormat.Jpeg);
        Img.Save(ms, ImageFormat.Jpeg);
![]() Response.ClearContent();
        Response.ClearContent();
![]() HttpContext.Current.Response.ContentType="image/Png";
        HttpContext.Current.Response.ContentType="image/Png"; 
![]() //Response.ContentType = "images/Jpeg";
        //Response.ContentType = "images/Jpeg";
![]() Response.BinaryWrite(ms.ToArray());
        Response.BinaryWrite(ms.ToArray());
![]() g.Dispose();
        g.Dispose();
![]() Img.Dispose();
        Img.Dispose();
![]() Response.End();
        Response.End();
![]() }
    }
![]()
![]() private string RndNum(int VcodeNum)
    private string RndNum(int VcodeNum)
![]() {
    {
![]() string Vchar = "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";
        string Vchar = "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";
![]() string[] VcArray = Vchar.Split(new Char[] { ',' });
        string[] VcArray = Vchar.Split(new Char[] { ',' });
![]() string VNum = "";
        string VNum = "";
![]() int temp = -1;
        int temp = -1;
![]()
![]() Random rand = new Random();
        Random rand = new Random();
![]()
![]() for (int i = 1; i < VcodeNum + 1; i++)
        for (int i = 1; i < VcodeNum + 1; i++)
![]() {
        {
![]() if (temp != -1)
            if (temp != -1)
![]() {
            {
![]() rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
                rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
![]() }
            }
![]()
![]() int t = rand.Next(35);
            int t = rand.Next(35);
![]() if (temp != -1 && temp == t)
            if (temp != -1 && temp == t)
![]() {
            {
![]() return RndNum(VcodeNum);
                return RndNum(VcodeNum);
![]() }
            }
![]() temp = t;
            temp = t;
![]() VNum += VcArray[t];
            VNum += VcArray[t];
![]() }
        }
![]() return VNum;
        return VNum;
![]() }
    } 
![]()
![]() </script>
</script>
![]() 
 
![]() 
  
	posted on 
2009-03-05 14:24 
seagreen7 
阅读(
162) 
评论() 
 
收藏 
举报