ASP.NET 例程完全代码版(3)——随意创建图形信息
可以选择输出图象的文本;
可以选择文本的颜色;
可以选择背景的颜色;
可以选择字体;
可以选择字号;
用途:比如显示自己的一个计数器,或者验证码等等。
<%@ 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>
<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>
<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>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
在原有的命名空间基础上添加以下引用:
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
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间传递值的方式总结如下:
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)——随意创建图形信息相关推荐
- ASP.NET 例程完全代码版(5)——通过web.config配置数据库连接池
下面讲述在VS2005中如何使用web.config配置文件,进行数据库的连接操作,并启用SQL Server数据库的连接池,相信对于已经熟悉VS2003的.刚进入05的环境的朋友会有些帮助. ...
- 在ASP.NET中随意创建图形信息
如果没有一个外部组件的支持,在ASP中是不能动态创建图形的,不管它是一个图表,一个横幅或仅仅是一个图形计数器.可喜的是,这一点在ASP.NET中改变了.现在,我们只需要使用内置功能,就能够很容易动态创 ...
- ASP.NET 2.0 正式版中无刷新页面的开发
ASP.NET 2.0 正式版中无刷新页面的开发 在已经发布的 ASP.NET2.0 中,无刷新页面开发相关部分同 beta2 有不少改动.而且在越来越多的 Ajax 开发包被开发出来的情况下, AS ...
- 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 ...
- 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] ...
- asp php微信支付,Asp微信支付接口代码 微信中生成订单后可以直接调出微信钱包直接付款_随便下源码网...
Asp微信支付接口代码 微信中生成订单后,可以直接调出微信钱包直接付款 软件介绍: 众所周到,目前微信支付已经十分普及,无论是商场.超市.网站上,微信支付的发展十分迅速,而ASP版微信支付在微信公众平 ...
- ASP.NET Maker 2019破解版(ASP.NET代码生成工具)
点击下载来源:ASP.NET Maker 2019破解版(ASP.NET代码生成工具) ASP.NET Maker 2019是一款最新版本的ASP.NET代码生成工具,它具有组织和维护代码的过程,并从 ...
- [转]ASP.NET(C#)常用代码30例
[转]ASP.NET(C#)常用代码30例 1. 打开新的窗口并传送参数: 传送参数: response.write("<script>window.open('*.aspx?i ...
- 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 ...
最新文章
- 深入剖析MobileNet和它的变种
- IDEA 构建为了打 jar 包的工程,包含 maven 打 jar 包的过程
- NR 5G 安全架构概述
- Check for Palindromes(算法)
- 7-14 电话聊天狂人 (25 分)map做法 + 详解 + 思路分析
- 《C++ Primer》1.52节练习
- 使用.net Stopwatch class 来分析你的代码
- 2007-2019中国城市竞争力排行榜Top10,你的家乡上榜了嘛?
- 处理器性能越来越强,但电脑为什么没有手机流畅?
- 有下界的最小费用可行流2.0(bzoj 3876: [Ahoi2014]支线剧情)
- varchar长度可以任意设置吗_户内金属软管长度可以超过2m吗?
- wxpython列表控件listctrl设置某行颜色_改变ListCtrl某行的背景色或者字体颜色
- LA4487 Exclusive-OR (加权并查集)
- 现代计算机网络的前沿技术,现代计算机网络的前沿技术分析
- 为什么快手不能左右滑了_快手上滑切换下一个视频怎么设置
- R语言(五) Plotly绘图基本命令介绍
- 《Kali Linux渗透测试的艺术》—第2章2.3节安全测试方法论
- CreateFontIndirect
- 新闻页面数据分页and添加新闻评论
- VSCode中vue拓展vetur整理代码时的尖括号换行问题