asp.net 编写验证码

首先准备一个类来实现对验证码的绘制功能。

createcode.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Drawing;
  6 using System.IO;
  7 using System.Configuration;
  8 
  9 
 10 namespace LoginProject
 11 {
 12     public class createcode
 13     {
 14         static string yanzheng = "";
 15         int length = 4;
 16         int fontsize = 25;
 17         string[] fontfamily = { "Arial Narrow", "Baskerville Old Face", "Algerian" };
 18         Color[] colors = { Color.Red, Color.Pink, Color.Plum, Color.Purple, Color.PaleTurquoise, Color.Orchid };//颜色
 19         string CodeValue = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,l,i,j,k,m,n,o,p,q,r,s,t,w,q,y,u,x,z"; //
 20         int imgwidth = 100; //图片宽
 21         int imgheight = 30; //图片高
 22         public static string YanZheng
 23         {
 24             get { return yanzheng; }
 25         }
 26         public int GetLength
 27         {
 28             get { return length; }
 29             set { length = value; }
 30         }
 31         public int FontSize
 32         {
 33             get { return fontsize; }
 34             set { fontsize = value; }
 35         }
 36         public string[] FontFamily
 37         {
 38             get { return fontfamily; }
 39             set { fontfamily = value; }
 40         }
 41         public Color[] GetColor
 42         {
 43             get { return colors; }
 44             set { colors = value; }
 45         }
 46         public string codevalue
 47         {
 48             get { return CodeValue; }
 49             set { CodeValue = value; }
 50         }
 51         public int ImaHeight
 52         {
 53             get { return imgheight; }
 54         }
 55         public int ImaWidth
 56         { get { return imgwidth; } }
 57         //产生随机字母
 58         public string createvalue()
 59         {
 60             string val = "";
 61             string[] arr = codevalue.Split(',');
 62             Random rand = new Random();
 63             for (int i = 0; i < GetLength; i++)
 64             {
 65                 int strindex = rand.Next(0, arr.Length - 1);
 66                 val = val + arr[strindex];
 67             }
 68             yanzheng = val;
 69             return val;
 70         }
 71         //绘制背景图像
 72         public void DrawImage(Bitmap map)
 73         {
 74             Random rand = new Random();
 75             for (int i = 0; i < map.Width; i++)
 76             {
 77                 for (int j = 0; j < map.Height; j++)
 78                 {
 79                     if (rand.Next(60, 90) < 80)
 80                     {
 81                         map.SetPixel(i, j, Color.Beige);
 82                     }
 83                 }
 84             }
 85         }
 86         //绘制验证码
 87         public void DrawValue(string code, Bitmap bmap)
 88         {
 89             Random rand = new Random();
 90             Graphics g = Graphics.FromImage(bmap);//获取背景图片(获取绘制器对象)
 91 
 92             System.Drawing.StringFormat sF = new StringFormat();
 93             sF.Alignment = StringAlignment.Center;
 94             for (int i = 0; i < code.Length; i++)
 95             {
 96                 System.Drawing.PointF point = new PointF(i * FontSize, 2);
 97                 string cellcode = code[i].ToString();
 98                 Font F = new Font(FontFamily[rand.Next(0, 2)], FontSize, FontStyle.Italic);
 99                 g.DrawString(cellcode, F, Brushes.BlueViolet, point, sF);
100             }
101         }
102         //输出
103         public void Output(string code, HttpContext type)
104         {
105            // System.IO.MemoryStream ms = new MemoryStream();
106             type.Response.ContentType = "image/jpeg";
107             Bitmap bit = new Bitmap(ImaWidth,ImaHeight);
108             this.DrawImage(bit);
109             this.DrawValue(code, bit);
110             bit.Save(type.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
111             //ms.Close();
112             //ms = null;
113             bit.Dispose();
114             bit = null;
115         }
116     }
117 }
View Code

然后准备一个页面来接收绘制出的验证码图片,如下

yanzhengma.aspx

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="yanzhengma.aspx.cs" Inherits="LoginProject.yanzhengma" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <title></title>
 8 </head>
 9 <body>
10     <form id="form1" runat="server">
11     <div>
12     <asp:Image  ID="imag" Width="100px" Height="50px" runat="server" ImageUrl="~/yanzhengma.aspx"/>
13     </div>
14     </form>
15 </body>
16 </html>

yanzhengma.aspx.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 namespace LoginProject
 9 {
10     public partial class yanzhengma : System.Web.UI.Page
11     {
12         protected void Page_Load(object sender, EventArgs e)
13         {
14             createcode code = new createcode();
15             string strcode = code.createvalue();
16             code.Output(strcode, this.Context);
17         }
18     }
19 }

如果想实现“看不清换一张”的功能则需要在另外使用一个接收页面。这个页面就是你的登陆页面代码如下

 1 <script type="text/javascript">
 2         function change() {
 3             var img = document.getElementById("Image1");
 4             img.src = img.src + '?';
 5         }
 6     </script>
 7 
 8 <td class="style1">验证码</td>
 9                 <td>
10                  <asp:TextBox ID="yanzheng" runat="server" Width="150px" Height="25px"></asp:TextBox><label><img  id="Image1" style=" Width:150px; Height:30px;" src="yanzhengma.aspx" alt="看不清换一张" onclick="change()"/></label>
11                 </td>

当然还有其他的页面排版呀什么的,我就不一一写出来,就把最主要的部分写出来。

最后的效果,图片比较丑,主要还是个人的美工不好哇!

希望能帮到你哟..........

posted on 2015-03-29 18:25  FightLi  阅读(136)  评论(0编辑  收藏  举报