1.代码一:
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;
using System.Data.SqlClient;
using System.Drawing;
using System.Web.SessionState;
using System.Drawing.Imaging;
public partial class xxbbsmain : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.LinkButton1.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
this.LinkButton1.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#777899'");
this.LinkButton2.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
this.LinkButton2.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#777899'");
this.LinkButton3.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
this.LinkButton3.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#777899'");
this.LinkButton4.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
this.LinkButton4.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#777899'");
this.LinkButton5.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
this.LinkButton5.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#777899'");
this.LinkButton6.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
this.LinkButton6.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#777899'");
this.LinkButton7.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
this.LinkButton7.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#777899'");
if (!IsPostBack)
{ }
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
}
private string GenCode(int num)
{
string[] source={"0","1","2","3","4","5","6","7","8","9",
"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 code="";
Random rd=new Random();
for(int i=0;i < num;i++)
{
code += source[rd.Next(0,source.Length)];
}
return code;
}
private void GenImg(string code)
{
Bitmap myPalette = new Bitmap(85, 20);//定义一个画板
Graphics gh = Graphics.FromImage(myPalette);//在画板上定义绘图的实例
Rectangle rc = new Rectangle(0, 0, 85, 20);//定义一个矩形
gh.FillRectangle(new SolidBrush(Color.Blue), rc);//填充矩形
gh.DrawString(code, new Font("宋体", 16), new SolidBrush(Color.White), rc);//在矩形内画出字符串
myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图片显示出来
Session["ValidateCode"] = code;//将字符串保存到Session中,以便需要时进行验证
gh.Dispose();
myPalette.Dispose();
}
protected void Button1_Click(object sender, EventArgs e)
{
this.GenImg(this.GenCode(6));
}
}
2.在html中就是:
<asp:Image ID="Image1" runat="server" Height="31px" ImageUrl="xxbbsmain.aspx" Style="z-index: 109;
left: 131px; position: absolute; top: 138px" Width="44px" />