网页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="/UploadFiles/2021-04-02/B.ascx"><!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="/UploadFiles/2021-04-02/B.ascx"><!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,ASCX,传值
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“ASPX向ASCX传值以及文本创建图片(附源码)”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。