日期:2008-08-17  浏览次数:20387 次

File 1: png.ASPx
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Drawing"%>
<%@ Import Namespace="System.Drawing.Imaging"%>
<%@ Import Namespace="System.Text"%>

<script language="C#" runat="Server">
public void Page_Load(Object sender, EventArgs e){
String vnum = GetRndNumber(4);
//Session.Add("vnumr_contact", vnum);
//Uncomment the preceding line in an actual Web app
GenPngImage(vnum);
}

public string GetRndNumber(int len){
string Vchar = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder buff = new StringBuilder(len);
Random random = new Random(unchecked((int)DateTime.Now.Ticks));
for(int i=0; i<len; i++){
buff.Append(Vchar[random.Next(36)]);
}
return buff.ToString();
}

public void GenPngImage(string vstr){
Bitmap bmp;
int bw = (int)(11.5*vstr.Length);
bmp = new Bitmap(bw, 20);
Graphics g = Graphics.FromImage(bmp);
g.DrawString(vstr, new Font("Palatino Linotype", 10), new SolidBrush(Color.Blue), 3, 3);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "Image/Png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
bmp.Dispose();
Response.End();
}
</script>




File 2:

......
<ASP:Image id="Image1" runat="server" ImageUrl="png.ASPx" />
......