Get the App
SLTechnology News&Howtos  ›  Development  › 

How ASPX passes values and text to ASCX to create pictures

Shulou Source: shulou.com Published: 2022-06-03 06:18:25 11月12日 Update

This article mainly shows you "ASPX how to send values to ASCX and text to create pictures", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to send values from ASPX to ASCX and text to create pictures" this article.

The web page ASPX has a TextBox, and another ASCX has an ImageButton. The user clicks this button to create an image of the text entered by the user in TextBox, and the ImageUrl of ASCX's ImageButton points back to the newly generated image.

To pass a value, write an interface and return the TextBox function of aspx:

The copy code is as follows:

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 and implement the interface.

The copy code is 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, connect the user control B.ascx to the page.

The copy code is as follows:

B.ascx:

The copy code is as follows:

B.ascx.cs:

The copy code is 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"); / / default.

}

Protected void imgBmp_Click (object sender, ImageClickEventArgs e)

{

Itransmitable textbox = (Itransmitable) this.Page; / / converts 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", 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

}

}

Running effect:

The above is all the content of the article "how ASPX passes values to ASCX and text creates pictures". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Code picture text content interface user article learning help function pointing control effect text easy to understand more organization knowledge ribbon web page Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi MySQL vpn macOS