ASP.NET 例程完全代码版(3)——随意创建图形信息

随意创建图形信息
功能:
   可以选择输出图象的文本;
   可以选择文本的颜色;
   可以选择背景的颜色;
   可以选择字体;
   可以选择字号;
用途:比如显示自己的一个计数器,或者验证码等等。
第一个aspx——Start.aspx:这是一个简单的用来设定图象属性的页面,如文本,颜色,大小等等,放置了3个HtmlTextBox,后来发现在codebehind中不能识别控件名称,呵呵,失误,只好加了runat="Server";然后是两个服务器端的DropDownList,来设置字体颜色和背景色。
<%@ Page Language="C#" %>
<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        string url = "PageCounter.aspx?HitCount=" + HitCount.Value + "&HitFontName=" + HitFontName.Value + "&HitFontSize=" + HitFontSize.Value + "&HitBackgroundColor=" + HitBackgroundColor.SelectedValue + "&HitFontColor=" + HitFontColor.SelectedValue;
        Response.Redirect(url);
    }
</script>
<html xmlns=" [url]http://www.w3.org/1999/xhtml[/url]" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
<div>
        <table style="width: 657px; height: 427px">
            <tr>
                <td style="width: 287px">
                    HitCounter</td>
                <td>
                    <input id="HitCount" type="text" name="HitCount" runat="server" /></td>
                <td>
                </td>
            </tr>
            <tr>
                <td style="width: 287px">
                    HitFontName</td>
                <td>
                    <input id="HitFontName" type="text" name="HitFontName" runat="server" /></td>
                <td>
                </td>
            </tr>
            <tr>
                <td style="width: 287px" >
                    HitFontSize</td>
                <td>
                    <input id="HitFontSize" type="text" name="HitFontSize" runat="server" /></td>
                <td>
                </td>
            </tr>
            <tr>
                <td style="width: 287px">
                    HitBackgroundColor</td>
                <td>
                    &nbsp;
<asp:DropDownList ID="HitBackgroundColor" runat="server">
                        <asp:ListItem>Red</asp:ListItem>
                        <asp:ListItem>Blue</asp:ListItem>
                        <asp:ListItem>Black</asp:ListItem>
                        <asp:ListItem>White</asp:ListItem>
                    </asp:DropDownList></td>
                <td>
                </td>
            </tr>
            <tr>
                <td style="width: 287px">
                    HitFontColor</td>
                <td>
<asp:DropDownList ID="HitFontColor" runat="server">
                        <asp:ListItem>Red</asp:ListItem>
                        <asp:ListItem>Blue</asp:ListItem>
                        <asp:ListItem>Black</asp:ListItem>
                        <asp:ListItem>White</asp:ListItem>
                    </asp:DropDownList></td>                   <td>
                    &nbsp;<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></td>
            </tr>
        </table>
        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
    </div>
    </form>
</body>
</html>
第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  <a href="#?参数列表"+参数值></a>
3.string url="request.aspx?id="+tbID.Text;//("转向的页面?参数列表"+参数)
  Response.Redirect(url);
4.string url="request.aspx?id="+tbID.Text;//("转向的页面?参数列表"+参数)
  Server.Transfer(url);
5.通过自定义属性。——这个没有研究过,高手路过,请指点!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值