ASPX向ASCX传值以及文本创建图片(附源码)
程序员文章站
2024-03-02 19:32:40
网页aspx有一个textbox,另一个ascx有一个imagebutton,用户点一点这个铵钮,把用户在textbox输入的文字创建为一个图片,ascx的imagebut...
网页aspx有一个textbox,另一个ascx有一个imagebutton,用户点一点这个铵钮,把用户在textbox输入的文字创建为一个图片,ascx的imagebutton的imageurl重新指向这刚产生的图片。
为了传值,写一个接口,返回aspx的textbox函数:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui.webcontrols;
/// <summary>
/// summary description for itransmitable
/// </summary>
namespace insus.net
{
public interface itransmitable
{
textbox gettextboxcontrol();
}
}
a.aspx.cs,并实现接口。
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using insus.net;
public partial class a : system.web.ui.page,itransmitable
{
protected void page_load(object sender, eventargs e)
{
}
public textbox gettextboxcontrol()
{
return this.tbhid;
}
}
a.aspx,把用户控件b.ascx接入页面。
<%@ page language="c#" autoeventwireup="true" codefile="a.aspx.cs" inherits="a" %>
<%@ register src="b.ascx" tagname="b" tagprefix="uc1" %>
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="tbhid" runat="server" />
<uc1:b id="b1" runat="server" />
</div>
</form>
</body>
</html>
b.ascx:
<%@ control language="c#" autoeventwireup="true" codefile="b.ascx.cs" inherits="b" %>
<asp:imagebutton runat="server" id="imgbmp" onclick="imgbmp_click" borderwidth="1" />
b.ascx.cs:
using system;
using system.collections.generic;
using system.drawing;
using system.drawing.drawing2d;
using system.drawing.imaging;
using system.drawing.text;
using system.io;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using insus.net;
public partial class b : system.web.ui.usercontrol
{
protected void page_load(object sender, eventargs e)
{
this.imgbmp.imageurl = getimagepath("insus.net"); //默认值。
}
protected void imgbmp_click(object sender, imageclickeventargs e)
{
itransmitable textbox = (itransmitable)this.page; //把page转换为接口。
this.imgbmp.imageurl = getimagepath(textbox.gettextboxcontrol().text.trim());
}
//创建图片
private string getimagepath(string _text)
{
bitmap bitmap = new bitmap(1, 1);
font font = new font("arial", 25, fontstyle.regular, graphicsunit.pixel);
graphics graphics = graphics.fromimage(bitmap);
int width = (int)graphics.measurestring(_text, font).width;
int height = (int)graphics.measurestring(_text, font).height;
bitmap = new bitmap(bitmap, new size(width, height));
graphics = graphics.fromimage(bitmap);
graphics.clear(color.white);
graphics.smoothingmode = smoothingmode.antialias;
graphics.textrenderinghint = textrenderinghint.antialias;
graphics.drawstring(_text, font, new solidbrush(color.fromargb(0, 0, 0)), 0, 0);
graphics.flush();
graphics.dispose();
string filename = path.getfilenamewithoutextension(path.getrandomfilename()) + ".jpg";
bitmap.save(server.mappath("~/imagelib/") + filename, imageformat.jpeg);
return "~/imagelib/" + filename;
}
}
运行效果:
demo code download(.net 4.5)
为了传值,写一个接口,返回aspx的textbox函数:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui.webcontrols;
/// <summary>
/// summary description for itransmitable
/// </summary>
namespace insus.net
{
public interface itransmitable
{
textbox gettextboxcontrol();
}
}
a.aspx.cs,并实现接口。
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using insus.net;
public partial class a : system.web.ui.page,itransmitable
{
protected void page_load(object sender, eventargs e)
{
}
public textbox gettextboxcontrol()
{
return this.tbhid;
}
}
a.aspx,把用户控件b.ascx接入页面。
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="a.aspx.cs" inherits="a" %>
<%@ register src="b.ascx" tagname="b" tagprefix="uc1" %>
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="tbhid" runat="server" />
<uc1:b id="b1" runat="server" />
</div>
</form>
</body>
</html>
b.ascx:
复制代码 代码如下:
<%@ control language="c#" autoeventwireup="true" codefile="b.ascx.cs" inherits="b" %>
<asp:imagebutton runat="server" id="imgbmp" onclick="imgbmp_click" borderwidth="1" />
b.ascx.cs:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.drawing;
using system.drawing.drawing2d;
using system.drawing.imaging;
using system.drawing.text;
using system.io;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using insus.net;
public partial class b : system.web.ui.usercontrol
{
protected void page_load(object sender, eventargs e)
{
this.imgbmp.imageurl = getimagepath("insus.net"); //默认值。
}
protected void imgbmp_click(object sender, imageclickeventargs e)
{
itransmitable textbox = (itransmitable)this.page; //把page转换为接口。
this.imgbmp.imageurl = getimagepath(textbox.gettextboxcontrol().text.trim());
}
//创建图片
private string getimagepath(string _text)
{
bitmap bitmap = new bitmap(1, 1);
font font = new font("arial", 25, fontstyle.regular, graphicsunit.pixel);
graphics graphics = graphics.fromimage(bitmap);
int width = (int)graphics.measurestring(_text, font).width;
int height = (int)graphics.measurestring(_text, font).height;
bitmap = new bitmap(bitmap, new size(width, height));
graphics = graphics.fromimage(bitmap);
graphics.clear(color.white);
graphics.smoothingmode = smoothingmode.antialias;
graphics.textrenderinghint = textrenderinghint.antialias;
graphics.drawstring(_text, font, new solidbrush(color.fromargb(0, 0, 0)), 0, 0);
graphics.flush();
graphics.dispose();
string filename = path.getfilenamewithoutextension(path.getrandomfilename()) + ".jpg";
bitmap.save(server.mappath("~/imagelib/") + filename, imageformat.jpeg);
return "~/imagelib/" + filename;
}
}
运行效果:
demo code download(.net 4.5)
上一篇: Android利用Chronometer实现倒计时功能
下一篇: 实例讲解Java并发编程之闭锁