欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASPX向ASCX传值以及文本创建图片(附源码)

程序员文章站 2024-03-04 12:57:05
网页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;
}
}

运行效果:
ASPX向ASCX传值以及文本创建图片(附源码) 
demo code download(.net 4.5)