溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ASPX如何向ASCX傳值以及文本創(chuàng)建圖片

發(fā)布時(shí)間:2021-10-13 09:59:01 來(lái)源:億速云 閱讀:135 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“ASPX如何向ASCX傳值以及文本創(chuàng)建圖片”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“ASPX如何向ASCX傳值以及文本創(chuàng)建圖片”這篇文章吧。

網(wǎng)頁(yè)ASPX有一個(gè)TextBox,另一個(gè)ASCX有一個(gè)ImageButton,用戶點(diǎn)一點(diǎn)這個(gè)銨鈕,把用戶在TextBox輸入的文字創(chuàng)建為一個(gè)圖片,ASCX的ImageButton的ImageUrl重新指向這剛產(chǎn)生的圖片。

為了傳值,寫一個(gè)接口,返回aspx的TextBox函數(shù):

復(fù)制代碼 代碼如下:


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,并實(shí)現(xiàn)接口。

復(fù)制代碼 代碼如下:


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接入頁(yè)面。

復(fù)制代碼 代碼如下:


<%@ 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:

復(fù)制代碼 代碼如下:


<%@ 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:

復(fù)制代碼 代碼如下:


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"); //默認(rèn)值。
}
protected void imgBmp_Click(object sender, ImageClickEventArgs e)
{
Itransmitable textbox = (Itransmitable)this.Page; //把page轉(zhuǎn)換為接口。
this.imgBmp.ImageUrl = GetImagePath(textbox.GetTextBoxControl().Text.Trim());
}
//創(chuàng)建圖片
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;
}
}


運(yùn)行效果:
ASPX如何向ASCX傳值以及文本創(chuàng)建圖片 

以上是“ASPX如何向ASCX傳值以及文本創(chuàng)建圖片”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI