2011-10-24

团队本周启动仿京东项目,成员分工明确,重点攻克登录、注册页面搭建及后台代码编写。项目涉及验证码生成,采用C#实现,包括随机字符生成、图片绘制等功能。

项目周啊!!!

这星期,我们开始做项目了,非常的兴奋啊,仿京东的项目看似简单,实则不然,我们小组用了整整一上午在确定好每个成员的任务,下午便开始了紧张的编写、、、

我负责的是页面的搭建工作,头疼得要死,本来就差的我,很是晕头转向,不过“拜师学艺”后,还是弄出了点东西的。

经过一下午+一晚上的恶战,我们小组成功达到了预期目标,做好了登录、注册等页面的搭建以及相应后台的代码编写。

我呢,则继续研究我的主页面的搭建,做好一个美工不容易啊!!!

我们创建一个Web窗体应用程序,以CheckCode.aspx命名

在CheckCode.aspx中不用写入任何代码,

<%@ Page Language="C#"AutoEventWireup="true" CodeFile="CheckCode.aspx.cs"Inherits="CheckCode" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

下面进入CheckCode.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;
//这是一个生成验证码的网页
public partial class CheckCode : System.Web.UI.Page
{
    private string GenerateCheckCode()
    {
        int num;
        char code;
        string checkCode = string.Empty;
        Random random = new Random();
        for (int i = 0; i < 6; i++)//循环次数决定验证码的位数
        {
            num =random.Next();
            if (num % 2== 0)
            {
               code = (char)('0' + (char)(num % 10));
            }
            else
            {
               code = (char)('A' + (char)(num % 26));
            }

           checkCode += code.ToString();
        }
        Response.Cookies.Add(newHttpCookie("CheckCode", checkCode));
        return checkCode;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
       CreateCheckCodeImg(GenerateCheckCode());
    }

    private void CreateCheckCodeImg(stringcheckCode)
    {
        if (checkCode == null ||checkCode.Trim() == String.Empty)
            return;
        Bitmap img = newBitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
       
        Graphics g = Graphics.FromImage(img);
        try
        {
            Randomrandom = new Random();
           g.Clear(Color.White);


            //画图片的背景线
            for (int i =0; i < 2; i++)
            {
               int x1 = random.Next(img.Width);
               int x2 = random.Next(img.Width);
               int y1 = random.Next(img.Width);
               int y2 = random.Next(img.Width);
               g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
            }

           //画出指定的字符
            Font font =new Font("Arial", 12, (FontStyle.Bold));
           System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(newRectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Red, 1.2f, true);
           g.DrawString(checkCode, font, brush, 2, 2);

           //画图片的前景噪点
            for (int i =0; i < 100; i++)
            {
               int x = random.Next(img.Width);
               int y = random.Next(img.Height);
               img.SetPixel(x, y, Color.FromArgb(random.Next()));
            }
           g.DrawRectangle(new Pen(Color.Silver),0,0,img.Width-1,img.Height-1);
           System.IO.MemoryStream ms=new System.IO.MemoryStream();
           img.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
           Response.ClearContent();
           Response.ContentType="image/Gif";
           Response.BinaryWrite(ms.ToArray());
       
        }  
        finally
        {
            g.Dispose();
           img.Dispose();
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值