随意创建图形信息
功能:
   可以选择输出图象的文本;
   可以选择文本的颜色;
   可以选择背景的颜色;
   可以选择字体;
   可以选择字号;
用途:比如显示自己的一个计数器,或者验证码等等。
第一个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 = "p_w_picpath/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.通过自定义属性。——这个没有研究过,高手路过,请指点!

转载于:https://blog.51cto.com/august/6969

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

  1. ASP.NET 例程完全代码版(5)——通过web.config配置数据库连接池

    下面讲述在VS2005中如何使用web.config配置文件,进行数据库的连接操作,并启用SQL Server数据库的连接池,相信对于已经熟悉VS2003的.刚进入05的环境的朋友会有些帮助.     ...

  2. 在ASP.NET中随意创建图形信息

    如果没有一个外部组件的支持,在ASP中是不能动态创建图形的,不管它是一个图表,一个横幅或仅仅是一个图形计数器.可喜的是,这一点在ASP.NET中改变了.现在,我们只需要使用内置功能,就能够很容易动态创 ...

  3. ASP.NET 2.0 正式版中无刷新页面的开发

    ASP.NET 2.0 正式版中无刷新页面的开发 在已经发布的 ASP.NET2.0 中,无刷新页面开发相关部分同 beta2 有不少改动.而且在越来越多的 Ajax 开发包被开发出来的情况下, AS ...

  4. Asp.Net基础 - 4.ASP.Net揭秘之Input版自增 + 5.ViewState初探

    4.ASP.Net揭秘之Input版自增 4.2.思考:把IntValue1.html设为起始页 5.ViewState初探 5.1.只有设定了name的input.textarea.select的v ...

  5. ASP+MSSQL注入工具 web版 beta 3 final release

    ASP+MSSQL注入工具 web版 beta 3 final release 2007-10-01 12:17:14 标签:ASP web MSSQL [推送到技术圈] 文章作者:MIKA[EST] ...

  6. asp php微信支付,Asp微信支付接口代码 微信中生成订单后可以直接调出微信钱包直接付款_随便下源码网...

    Asp微信支付接口代码 微信中生成订单后,可以直接调出微信钱包直接付款 软件介绍: 众所周到,目前微信支付已经十分普及,无论是商场.超市.网站上,微信支付的发展十分迅速,而ASP版微信支付在微信公众平 ...

  7. ASP.NET Maker 2019破解版(ASP.NET代码生成工具)

    点击下载来源:ASP.NET Maker 2019破解版(ASP.NET代码生成工具) ASP.NET Maker 2019是一款最新版本的ASP.NET代码生成工具,它具有组织和维护代码的过程,并从 ...

  8. [转]ASP.NET(C#)常用代码30例

    [转]ASP.NET(C#)常用代码30例 1. 打开新的窗口并传送参数: 传送参数: response.write("<script>window.open('*.aspx?i ...

  9. Silverlight 2 Beta 1, IE 8 Beta 1, ASP.NET MVC 预览版2 可以下载了 - 思归呓语 - 博客堂

    1. Microsoft Silverlight Tools Beta 1 for Visual Studio 2008 http://www.microsoft.com/downloads/deta ...

最新文章

  1. 深入剖析MobileNet和它的变种
  2. IDEA 构建为了打 jar 包的工程,包含 maven 打 jar 包的过程
  3. NR 5G 安全架构概述
  4. Check for Palindromes(算法)
  5. 7-14 电话聊天狂人 (25 分)map做法 + 详解 + 思路分析
  6. 《C++ Primer》1.52节练习
  7. 使用.net Stopwatch class 来分析你的代码
  8. 2007-2019中国城市竞争力排行榜Top10,你的家乡上榜了嘛?
  9. 处理器性能越来越强,但电脑为什么没有手机流畅?
  10. 有下界的最小费用可行流2.0(bzoj 3876: [Ahoi2014]支线剧情)
  11. varchar长度可以任意设置吗_户内金属软管长度可以超过2m吗?
  12. wxpython列表控件listctrl设置某行颜色_改变ListCtrl某行的背景色或者字体颜色
  13. LA4487 Exclusive-OR (加权并查集)
  14. 现代计算机网络的前沿技术,现代计算机网络的前沿技术分析
  15. 为什么快手不能左右滑了_快手上滑切换下一个视频怎么设置
  16. R语言(五) Plotly绘图基本命令介绍
  17. 《Kali Linux渗透测试的艺术》—第2章2.3节安全测试方法论
  18. CreateFontIndirect
  19. 新闻页面数据分页and添加新闻评论
  20. VSCode中vue拓展vetur整理代码时的尖括号换行问题

热门文章

  1. 企业级LNMP架构搭建实例(基于Centos6.x)
  2. Linux字体显示不同颜色
  3. 微博feed系统的推(push)模式和拉(pull)模式和时间分区拉模式架构探讨
  4. 微软(MICROSOFT)试用版企业软件下载地址
  5. BE镜像还原系统过程
  6. PLSQL常用方法汇总(转载)
  7. ASP.NET十七种正则表达试
  8. switch和枚举的小坑
  9. C#网页数据采集(三)HttpWebRequest
  10. 杭电1276--士兵队列训练问题