http://zhidao.baidu.com/question/39530893.html?fr=qrl
首先,新建文件:CreateImage.aspx
里面不用写代码,
CreateImage.aspx.cs 如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace CreateImage
{
/// <summary>
/// CreateImage 的摘要说明。
/// </summary>
public class CreateImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string checkCode = CreateCode(4);
//用于验证
Session["CheckCode"] = checkCode;
CreateImages(checkCode);
}
/*产生验证码*/
public string CreateCode(int codeLength)
{
string so = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] strArr=so.Split(',');
string code = "";
Random rand=new Random();
for (int i = 0; i < codeLength; i++)
{
code+=strArr[rand.Next(0, strArr.Length)];
}
return code;
}
/*产生验证图片*/
public void CreateImages(string code)
{
Bitmap image = new Bitmap(60, 20);
Graphics g = Graphics.FromImage(image);
WebColorConverter ww=new WebColorConverter();
g.Clear((Color)ww.ConvertFromString("#FAE264"));
Random random = new Random();
//画图片的背景噪音线
for (int i = 0; i < 12; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
}
Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0,0,image.Width,image.Height),Color.Blue,Color.Gray,1.2f,true);
g.DrawString(code, font, brush, 0, 0);
//画图片的前景噪音点
for (int i = 0; i < 10; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.White);
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
之后只需要在调用的时候 填入<img src="CreateImage.aspx" align="middle">就可以
例如:新建WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="CreateImage.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<img src="CreateImage.aspx" align="middle"><BR>
</form>
</body>
</html>
ASP.NET下英文及中文汉字的验证码控件
http://www.msproject.cn/article/ImageValidatorChinese.aspx
ivanx 著于2007-11-13 23:32:54
本文实现了一个非常好的验证码控件,它向客户端浏览器传递一个包含随机产生的文字的图片,并验证用户的输入。另外,译者还在修改了原文中的代码,实现了中文汉字的验证码控件。
ASP.NET 2.0 下的验证码控件
http://www.msproject.cn/article/ValidateControl.aspx
icscs 著于2007-8-2 20:09:49
验证码是阻止恶意用户采用自动注册机/发帖机的一个好方法,也许你已经在Google,yahoo等大型网站上见到它的应用了。本文会给你一个这样的控件。