功能:
可以选择输出图象的文本;
可以选择文本的颜色;
可以选择背景的颜色;
可以选择字体;
可以选择字号;
用途:比如显示自己的一个计数器,或者验证码等等。
第一个aspx——Start.aspx:这是一个简单的用来设定图象属性的页面,如文本,颜色,大小等等,放置了3个HtmlTextBox,后来发现在codebehind中不能识别控件名称,呵呵,失误,只好加了runat="Server";然后是两个服务器端的DropDownList,来设置字体颜色和背景色。
<%@ Page Language="C#" %>
http://www.w3.org/1999/xhtml" >
Untitled Page
第2个aspx——PageCounter.aspx:起的名字是用作计数器,这里只是显示简单的自己输入的文本而已。
在原有的命名空间基础上添加以下引用:
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
同样,是写一个Page_Load事件:
protected void Page_Load(object sender, EventArgs e)
{
Response.Expires = 0;
Bitmap pic = null;
Graphics g = null;
//得到传递的参数,为空则赋默认值
string str2Render = Request.QueryString.Get("HitCount");
if (str2Render == null)
str2Render = "no count specified";
string strFont = Request.QueryString.Get("HitFontName");
if (strFont == null)
strFont = "Lucida Sans Unicode";
int nFontSize = 12;
try
{
nFontSize = Int32.Parse(Request.QueryString.Get("HitFontSize"));
}
catch
{
}
string strBgColorName = Request.QueryString.Get("HitBackgroundColor");
Color clrBg = Color.White;
try
{
if (strBgColorName != null)
clrBg = ColorTranslator.FromHtml(strBgColorName);
}
catch
{
}
string strFontColorName = Request.QueryString.Get("HitFontColor");
Color clrFont = Color.Black;
try
{
if (strFontColorName != null)
clrFont = ColorTranslator.FromHtml(strFontColorName);
}
catch
{
}
//画图
try
{
Font fontCounter = new Font(strFont, nFontSize);
pic = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
g = Graphics.FromImage(pic);
SizeF strSize = g.MeasureString(str2Render, fontCounter);
int nWidth = (int)strSize.Width;
int nHeight = (int)strSize.Height;
g.Dispose();
pic.Dispose();
pic = new Bitmap(nWidth, nHeight, PixelFormat.Format32bppArgb);
g = Graphics.FromImage(pic);
g.FillRectangle(new SolidBrush(clrBg), new Rectangle(0, 0, nWidth, nHeight));
g.DrawString(str2Render, fontCounter, new SolidBrush(clrFont), 0, 0);
MemoryStream tempStream = new MemoryStream();
pic.Save(tempStream, ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/png";
Response.BinaryWrite(tempStream.ToArray());
Response.End();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
if (g != null) g.Dispose();
if (pic != null) pic.Dispose();
}
}
代码copy过去,你就可以看到运行结果了,具体的函数可以很容易通过提示得到含义,这里需要注意的问题是,如何在两个aspx页面之间,提交一个表单到另一个aspx,虽然不是什么大问题,但是,对于很多初学者还是需要注意一下的,这里要注意控件的runat属性,在本例子中传递参数时,需要得到控件的value,如果不是服务器端控件,则不能识别控件名。例子中只是一种提交表单的方式,现将两个aspx间传递值的方式总结如下:
1.Session,Application都可用于传值。
2.使用HyperLink
3.string url="request.aspx?id="+tbID.Text;//("转向的页面?参数列表"+参数)
Response.Redirect(url);
4.string url="request.aspx?id="+tbID.Text;//("转向的页面?参数列表"+参数)
Server.Transfer(url);
5.通过自定义属性。——这个没有研究过,高手路过,请指点!
本文作者:windfly. 来源:http://windfly.blog.ccidnet.com/
CIO之家 www.ciozj.com 微信公众号:imciow