ASPX transfers values to ASCX and creates an image using text (with source code)

Source: Internet
Author: User

The webpage ASPX has a TextBox, And the other ASCX has an ImageButton. You can click this button to create an image from the text entered in the TextBox, the image URL of ImageButton of ASCX points to the generated image again.

To pass the value, write an interface and return the TextBox function of aspx:
Copy codeThe Code is 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 implement the interface.
Copy codeThe 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: access the user control B. ascx to the page.
Copy codeThe Code is as follows:
<% @ 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:
Copy codeThe Code is 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 codeThe 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 value.
}
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 an image
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:
 
Demo code download (. NET 4.5)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.