.net 利用GDI+绘制图像,这的确很好用。
两个aspx文件,一个用于绘制图像,一个用于验证
WebForm1.aspx 主页面,用于验证
gif.aspx 用户绘图
WebForm1.aspx 主页面,用于验证
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="1">
<TR>
<TD>
<asp:Image id="Image1" runat="server" ImageUrl="gif.aspx"></asp:Image></TD>
</TR>
<TR>
<TD><asp:textbox id="TextBox1" runat="server"></asp:textbox></TD>
</TR>
<TR>
<TD>
<asp:label id="Label1" runat="server">Label</asp:label></TD>
</TR>
<TR>
<TD><asp:button id="Button1" runat="server" Text="Button"></asp:button></TD>
</TR>
</TABLE>
</FONT>
</form>
</body>
</HTML>
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Image Image1;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
//验证输入的信息是否与session相同
if(TextBox1.Text.Trim()!=Session["validatecode"].ToString())
{Label1.Text="验证错误,验证码应为"+Session["validatecode"];
}
else
{Label1.Text="验证正确,验证码为"+Session["validatecode"];
}
}
}
}
gif.cs 用于绘制图像
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// gif 的摘要说明。
///
///程序首先生成一个4位随机数
///然后保存到session
///利用GDI+绘制图像
///
/// </summary>
public class gif : System.Web.UI.Page
{
protected int num;
private void Page_Load(object sender, System.EventArgs e)
{
Random a =new Random(); //用于生成随机数
num=a.Next(1000,9999);//懒得补0,所以从1000开始到9999
Session["validatecode"]=num;//保存到session
//绘制字符串
string str=num.ToString();
Bitmap image=new Bitmap(80,20); //创建Bitmap对象
Graphics g=Graphics.FromImage(image); //创建Graphics对象
g.Clear(Color.White); //清除画面,指定背景色
//绘制两种颜色
string str1=str.Substring(0,1);//取字符串的第一个字符
g.DrawString(str1,new Font("Courier New", 12),new SolidBrush(Color.Red),10,5); //在矩形内绘制字串(字串,字体,画笔颜色,左上x.昨上y)
str=str.Substring(1,3);//取后三个字符
g.DrawString(str,new Font("Courier New", 12),new SolidBrush(Color.Blue),20,5); //
MemoryStream tempStream = new MemoryStream(); //内存流
image.Save(tempStream,ImageFormat.Png);//将图像保存到MemoryStream
Response.ClearContent();
Response.ContentType = "image/png";
Response.BinaryWrite(tempStream.ToArray()); //将图像输出的页面
g.Dispose();//释放资源
image.Dispose();//释放资源
}
#region Web Form Designer generated code
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
}
}
两个aspx文件,一个用于绘制图像,一个用于验证
WebForm1.aspx 主页面,用于验证
gif.aspx 用户绘图
WebForm1.aspx 主页面,用于验证
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="1">
<TR>
<TD>
<asp:Image id="Image1" runat="server" ImageUrl="gif.aspx"></asp:Image></TD>
</TR>
<TR>
<TD><asp:textbox id="TextBox1" runat="server"></asp:textbox></TD>
</TR>
<TR>
<TD>
<asp:label id="Label1" runat="server">Label</asp:label></TD>
</TR>
<TR>
<TD><asp:button id="Button1" runat="server" Text="Button"></asp:button></TD>
</TR>
</TABLE>
</FONT>
</form>
</body>
</HTML>
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Image Image1;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
//验证输入的信息是否与session相同
if(TextBox1.Text.Trim()!=Session["validatecode"].ToString())
{Label1.Text="验证错误,验证码应为"+Session["validatecode"];
}
else
{Label1.Text="验证正确,验证码为"+Session["validatecode"];
}
}
}
}
gif.cs 用于绘制图像
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// gif 的摘要说明。
///
///程序首先生成一个4位随机数
///然后保存到session
///利用GDI+绘制图像
///
/// </summary>
public class gif : System.Web.UI.Page
{
protected int num;
private void Page_Load(object sender, System.EventArgs e)
{
Random a =new Random(); //用于生成随机数
num=a.Next(1000,9999);//懒得补0,所以从1000开始到9999
Session["validatecode"]=num;//保存到session
//绘制字符串
string str=num.ToString();
Bitmap image=new Bitmap(80,20); //创建Bitmap对象
Graphics g=Graphics.FromImage(image); //创建Graphics对象
g.Clear(Color.White); //清除画面,指定背景色
//绘制两种颜色
string str1=str.Substring(0,1);//取字符串的第一个字符
g.DrawString(str1,new Font("Courier New", 12),new SolidBrush(Color.Red),10,5); //在矩形内绘制字串(字串,字体,画笔颜色,左上x.昨上y)
str=str.Substring(1,3);//取后三个字符
g.DrawString(str,new Font("Courier New", 12),new SolidBrush(Color.Blue),20,5); //
MemoryStream tempStream = new MemoryStream(); //内存流
image.Save(tempStream,ImageFormat.Png);//将图像保存到MemoryStream
Response.ClearContent();
Response.ContentType = "image/png";
Response.BinaryWrite(tempStream.ToArray()); //将图像输出的页面
g.Dispose();//释放资源
image.Dispose();//释放资源
}
#region Web Form Designer generated code
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
}
}