当前位置:编程学习 > C#/ASP.NET >>

ASP.net中验证码的代码是多少?

答案:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Background_Code : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str2 = GenerateCheckCode();
CreateCheckCodeImage(str2);
}
/// <summary>
/// 产生数
/// </summary>
/// <returns></returns>
public string GenerateCheckCode()
{
Random temp = new Random();
string str1 = "0123456789abcdefghijklmnopqrstuvwxyz";
string str = "";
for (int i = 0; i < 4; i++)
{
str = str + str1.Substring(temp.Next(36), 1);
}
Session["Val"] = str;
return str;
}
/// <summary>
/// 产生图片
/// </summary>
/// <param name="num">随机产生的数字字符串</param>
public void CreateCheckCodeImage(string num)
{
if (num == null || num.Trim() == String.Empty)
return;
System.Drawing.Bitmap img = new System.Drawing.Bitmap((int)Math.Ceiling((num.Length * 12.5)), 22);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
try
{
Random ra = new Random();
g.Clear(System.Drawing.Color.White);
for (int j = 0; j < 2; j++)
{
int x1 = ra.Next(img.Width);
int x2 = ra.Next(img.Width);
int y1 = ra.Next(img.Height);
int y2 = ra.Next(img.Height);
g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Black), x1, y1, x2, y2);
}
System.Drawing.Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true);
g.DrawString(num, font, brush, 2, 2);
for (int k = 0; k < 100; k++)
{
int x = ra.Next(img.Width);
int y = ra.Next(img.Height);
img.SetPixel(x, y, System.Drawing.Color.FromArgb(ra.Next()));
}
//图片边缘
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.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();
}
}
}
答案补充
新建一个网页  写个img标签 src=上面的路径就可以了
http://hi.zhaoxi.net/cyap/blog/item/ebc46d2422a5e2358644f910.html
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public partial class images_code : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
Color[] color ={ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange,Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] font ={ "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh","PMingLiU", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character ={ '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E','F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
//保存验证码的Cookie
HttpCookie anycookie = new HttpCookie("validateCookie");
anycookie.Values.Add("ChkCode", chkCode);
HttpContext.Current.Response.Cookies["validateCookie"].Values["ChkCode"] = chkCode;
Bitmap bmp = new Bitmap(80,20);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i < 3; i++)
{
int x1 = rnd.Next(80);
int y1 = rnd.Next(20);
int x2 = rnd.Next(80);
int y2 = rnd.Next(20);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 11);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 6, (float)2);
}
//画噪点
for (int i = 0; i < 50; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
//清除该页输出缓存,设置该页无缓存
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
//将验证码图片写入内存流,并将其以"image/Png" 格式输出
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
}
finally
{
bmp.Dispose(); //显式释放资源
g.Dispose();
}
}
}

上一个:如何用ASP.net实现窗口悬浮的效果
下一个:关于asp.net 2.0 C# 运算符的解释

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,