ascx中写html代码,ASPX向ASCX传值以及文本创建图片(附源码)

网页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 description for Itransmitable

///

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接入页面。

B.ascx:

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;

}

}

运行效果:

a8e2d8536634e44e5d628e79dd2e4d5d.gif 

Demo code download(.NET 4.5)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值