aspx to ascx values and text to create pictures (with source code) _ Practical Skills

Source: Internet
Author: User
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)

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.