ASP.NET 实现登录界面(生成验证码)

 这周末也没干啥,真正开始ASP,做了个学籍管理系统的登录界面,登录界面主要包括用户名、密码、验证码,界面字体用了 <font size="5" color="blue" font-family:"华文琥珀";></font> 改变字体,生成验证码控件  ImageButton   (例如: <asp:ImageButton ID="ImageButton1" runat="server" />)
 还学会了图片按钮 HyperLink (例如:  <asp:HyperLink  ID="HyperLink1" runat="server" Text="link to Default" NavigateUrl="~/Default.aspx" ImageUrl="~/Image/1.gif"></asp:HyperLink>)
其中我觉得很难的就是验证码的生成,你首先在站点中添加pivture.aspx 的窗体,打开代码页面(即pivture.aspx.cs)并录入下面代码(完整的)
复制代码

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

public partial class Picture : System.Web.UI.Page
{
    Random ran = new Random();
    protected void Page_Load(object sender, EventArgs e)
    {
        string str = getRandomValidate(4);
        Session["CheckCode"] = str;//这一部是Wie了验证码写入Session,进行验证,也可以使用cookie
        getImageValidate(str);   
    }
    //得到随机字符串,长度自定义
    private string getRandomValidate(int len)
    {
        int num;
        int tem;
        string rtuStr="";
        for (int i = 0; i < len;i++ )
        {
            num = ran.Next();
            tem = num % 10 + '0';//生成数字
            //tem = num % 26 + 'A';//生成字符
            rtuStr += Convert.ToChar(tem).ToString();

        }
        return rtuStr;
    }
    //生成图像
    private void getImageValidate(string strValue)
    { 
        //string str=oo00;前两个为字母o,后两个数为0 
        int width = Convert.ToInt32(strValue.Length*12);
        Bitmap img = new Bitmap(width,23);
        Graphics gfc = Graphics.FromImage(img);
        gfc.Clear(Color.White);
        drawLine(gfc,img);
        //写验证码,要定义Font
        Font font = new Font("arial",12,FontStyle.Bold);
        //Font font = new Font("宋体",12,FontStyle.Bold|FontStyle.Italic);
        System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,img.Width,img.Height),Color.DarkOrchid,Color.Blue,1.5f,true);
        gfc.DrawString(strValue,font, brush ,3,2);
        drawPoint(img);
        gfc.DrawRectangle(new Pen(Color.DarkBlue),0,0,img.Width-1,img.Height-1);
        //将图像添加到页面
        MemoryStream ms = new MemoryStream();
        img.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
        //更改HTTP
        Response.ClearContent();
        Response.ContentType = "image/gif";
        Response.BinaryWrite(ms.ToArray());
        //Dispose
        gfc.Dispose();
        img.Dispose();
        Response.End();


    }

    private void drawLine(Graphics gfc,Bitmap img)
    {
        //选择画10条线,也可以增加,也可以不要线,只要随机杂点就行
        for (int i = 0; i < 10;i++ )
        {
            int x1 = ran.Next(img.Width);
            int y1 = ran.Next(img.Height);
            int x2 = ran.Next(img.Width);
            int y2 = ran.Next(img.Height);
            gfc.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);//注意画笔要淡,不然看不清
        }
 
    }

    //private void drawPoint(Bitmap img)
    //{ 
    
    //}

    private void drawPoint(Bitmap img)
    {
        int col = ran.Next();//在一次的图片中杂点颜色相同
        for (int i = 0; i < 100; i++)
        {
            int x = ran.Next(img.Width);
            int y = ran.Next(img.Height);
            img.SetPixel(x,y,Color.FromArgb(col));
        }
    }
   
}

复制代码
录完后,需要在 登录界面显示验证码,则需要在
登录界面的代码文件 Page_Load 中加入  ImageButton1.ImageUrl = "~/Picture.aspx";
其登陆页面的代码Default.aspx.cs中的代码为
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值