生成验证码

这是一个ASP.NET 2.0WEB生成验证码的后台代码,前台 <img id="IMG1" runat="server" src="checkcode.aspx" />

调用即可

 

 

 1 using System;
 2 using System.Data;
 3 using System.Configuration;
 4 using System.Collections;
 5 using System.Web;
 6 using System.Web.Security;
 7 using System.Web.UI;
 8 using System.Web.UI.WebControls;
 9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Drawing;
12 
13 public partial class checkcode : System.Web.UI.Page
14 {
15     protected void Page_Load(object sender, EventArgs e)
16     {
17         CreateCheckCodeImage(GenerateCheckCode());
18     }
19 
20     private string GenerateCheckCode()
21     {
22         int number;
23         char code;
24         string checkCode = String.Empty;
25         System.Random random = new Random();
26 
27         for (int i = 0; i < 4; i++)
28         {
29             number = random.Next();
30 
31             if (number % 2 == 0)
32                 code = (char)('0' + (char)(number % 10));
33             else
34                 code = (char)('A' + (char)(number % 26));
35 
36             checkCode += code.ToString();
37         }
38         Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
39         return checkCode;
40     }
41 
42     private void CreateCheckCodeImage(string checkCode)
43     {
44         if (checkCode == null || checkCode.Trim() == String.Empty)
45             return;
46         System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
47         Graphics g = Graphics.FromImage(image);
48         try
49         {
50             //生成随机生成器
51             Random random = new Random();
52             //清空图片背景色
53             g.Clear(Color.White);
54             //画图片的背景噪音线
55             for (int i = 0; i < 2; i++)
56             {
57                 int x1 = random.Next(image.Width);
58                 int x2 = random.Next(image.Width);
59                 int y1 = random.Next(image.Height);
60                 int y2 = random.Next(image.Height);
61                 g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
62             }
63             Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
64             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);
65             g.DrawString(checkCode, font, brush, 2, 2);
66             //画图片的前景噪音点
67             for (int i = 0; i < 100; i++)
68             {
69                 int x = random.Next(image.Width);
70                 int y = random.Next(image.Height);
71                 image.SetPixel(x, y, Color.FromArgb(random.Next()));
72             }
73             //画图片的边框线
74             g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
75             System.IO.MemoryStream ms = new System.IO.MemoryStream();
76             image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
77             Response.ClearContent();
78             Response.ContentType = "image/Gif";
79             Response.BinaryWrite(ms.ToArray());
80         }
81         finally
82         {
83             g.Dispose();
84             image.Dispose();
85         }
86     }
87 }
View Code

 

转载于:https://www.cnblogs.com/cnxinwa/p/3440896.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值