The page aspx has a textbox, another ascx has a ImageButton, the user points to the Ammonium button, creates the text entered by the user in the textbox as a picture, and the ImageUrl of the Ascx ImageButton again points to the newly generated image.
To pass the value, write an interface that returns the TextBox function for aspx:
Copy Code code as follows:
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 and implements the interface.
Copy Code code as follows:
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, the user control B.ascx access to the page.
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "A.aspx.cs" inherits= "A"%>
<%@ Register src= "B.ascx" tagname= "B" tagprefix= "uc1"%>
<! DOCTYPE html>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:textbox id= "Tbhid" runat= "Server"/>
<uc1:b id= "B1" runat= "Server"/>
</div>
</form>
</body>
B.ascx:
Copy Code code as follows:
<%@ 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:
Copy Code code as follows:
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"); The default value.
}
protected void Imgbmp_click (object sender, ImageClickEventArgs e)
{
itransmitable textbox = (itransmitable) this. Page; Converts the page to an interface.
THIS.IMGBMP.IMAGEURL = Getimagepath (textbox. Gettextboxcontrol (). Text.trim ());
}
Create a picture
private string Getimagepath (String _text)
{
Bitmap Bitmap = new Bitmap (1, 1);
Font font = new Font ("Arial", 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;
}
}
Operation Effect:
Demo code Download (. NET 4.5)