ASPX向ASCX傳值以及文本創(chuàng)建圖片(附源碼)
更新時(shí)間:2013年03月22日 14:46:55 作者:
把用戶在TextBox輸入的文字創(chuàng)建為一個(gè)圖片,ASCX的ImageButton的ImageUrl重新指向這剛產(chǎn)生的圖片,接下來介紹下ASPX向ASCX傳值,感興趣的朋友可以參考下哈
網(wǎng)頁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ù):
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)接口。
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"); //默認(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)行效果:
Demo code download(.NET 4.5)
為了傳值,寫一個(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接入頁面。
復(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)行效果:
Demo code download(.NET 4.5)
相關(guān)文章
ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection)中篇
這篇文章主要為大家再一次介紹了ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
ASP.NET GridView 實(shí)現(xiàn)課程表顯示(動(dòng)態(tài)合并單元格)實(shí)現(xiàn)步驟
GridView,ASP.NET中很常用的數(shù)據(jù)顯示控件,這里,我將用這個(gè)控件來實(shí)現(xiàn)課程表的顯示。首先說說課程表的顯示與普通記錄的顯示有何不同?感興趣的朋友可以了解下,或許對(duì)你有所幫助2013-02-02
ASP.NET The system cannot find the file specified解決辦法
這篇文章主要介紹了ASP.NET The system cannot find the file specified解決辦法的相關(guān)資料,需要的朋友可以參考下2016-11-11
ASP.NET獲取MS SQL Server安裝實(shí)例實(shí)現(xiàn)思路及代碼
在演示中,是把找到的實(shí)例顯示于DropDownList控件中。首先在.aspx拉一個(gè)DropDownList控件,感興趣的朋友可以了解下哦,或許對(duì)你有所幫助2013-01-01
Asp.net的服務(wù)器推技術(shù) (Server Push)
在以往的和服務(wù)器端通信技術(shù)中,我們多數(shù)使用的是AJAX輪詢式訪問,也就是在Javascript中控制時(shí)間間隔,然后每隔一段時(shí)間就訪問一次服務(wù)器,然后獲得數(shù)據(jù)或通知。但是這種輪詢方式的訪問有90%是在做無用功。2010-01-01
用Fine Uploader+ASP.NET MVC實(shí)現(xiàn)ajax文件上傳[代碼示例]
Fine Uploader(http://fineuploader.com/)是一個(gè)實(shí)現(xiàn) ajax 上傳文件的 Javascript 組件2013-01-01
asp.net(c#)限制用戶輸入規(guī)定的字符和數(shù)字的代碼
這幾天在看到一個(gè)網(wǎng)站的注冊(cè)的時(shí)候,就只允許輸入規(guī)定的字符和數(shù)字。我就好奇的寫了一個(gè)校驗(yàn)的代碼。呵呵 不知道對(duì)大家有沒有用。如果有用的話可以保存。沒有用就當(dāng)是看看以下了。2010-10-10

