国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASPX向ASCX傳值以及文本創建圖片(附源碼)

2020-01-18 00:05:57
字體:
來源:轉載
供稿:網友
網頁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)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灌阳县| 阿鲁科尔沁旗| 佳木斯市| 万州区| 庐江县| 伊宁县| 循化| 建始县| 龙岩市| 鹿泉市| 辽源市| 合肥市| 方山县| 武清区| 石棉县| 南昌市| 读书| 九寨沟县| 茂名市| 长阳| 河北省| 长兴县| 孟州市| 京山县| 江达县| 宜宾县| 博白县| 定结县| 多伦县| 巴林右旗| 闸北区| 峨眉山市| 明溪县| 洛浦县| 泗水县| 梁河县| 西乌珠穆沁旗| 鹤山市| 农安县| 西丰县| 达拉特旗|