How to create text image on the fly with ASP. NET

Source: Internet
Author: User

Abstract: http://www.codeproject.com/aspnet/createTextImage.asp
<PRE lang = cs id = pre0 style = "margin-top: 0px; display: block "> <% @ page Language =" C # "trace =" false "Explicit =" true "aspcompat =" true "DEBUG =" true "%>
<% @ Import namespace = "system" %>
<% @ Import namespace = "system. Text" %>
<% @ Import namespace = "system. Drawing" %>
<% @ Import namespace = "system. Drawing. Imaging" %>
<% @ Import namespace = "system. Drawing. Text" %>
<% @ Import namespace = "system. Drawing. drawing2d" %>

<SCRIPT runat = "server">
//-------------------------------------
// Fonter.net V1.0
//-------------------------------------
// Text images on the fly using ASP. NET
// Written in C # And GDI + Library
//-------------------------------------
// (C) zeddy Iskandar, 2003-onwards.
// Provided as-is, author is not
// Responsible for anything.
//-------------------------------------

Public void page_load (Object sender, system. eventargs e)
{< br> If (request. httpmethod = "Post")
{< br> string text = request. form. get ("text");
int textlength = text. length;
int fontsize = convert. toint32 (request. form. get ("fontsize");
int orientation = convert. toint32 (request. form. get ("orientation");
int antialias = convert. toint32 (request. form. get ("antialias");

// set canvas width & height
int width;
int height;
If (orientation = 1)
{< br> width = (fontsize * textlength)-(textlength * fontsize)/3);
Height = fontsize + 20;
}< br> else
{< br> width = fontsize + 20;
Height = (INT) (fontsize * textlength * 1.5 );
}

// Initialize graphics
Rectanglef rectf = new rectanglef (0, 0, width, height );
Bitmap PIC = new Bitmap (width, height, pixelformat. format24bpprgb );
Graphics G = graphics. fromimage (PIC );
G. smoothingmode = smoothingmode. antialias;
If (antialias = 1) g. textrenderinghint = textrenderinghint. antialias;

// Set colors
String fgcolor = request. Form. Get ("fontcolor ");
String bgcolor = request. Form. Get ("bgcolor ");
Color fontcolor = color. fromname (fgcolor );
Color rectcolor = color. fromname (bgcolor );
Solidbrush fgbrush = new solidbrush (fontcolor );
Solidbrush bgbrush = new solidbrush (rectcolor );

// Rectangle or ellipse?
Int bound = convert. toint32 (request. Form. Get ("Bound "));
If (bound = 1)
{
G. fillrectangle (bgbrush, rectf );
}
Else
{
G. fillrectangle (New solidbrush (color. White), rectf );
G. fillellipse (bgbrush, rectf );
}

// Load font
String fontname = request. Form. Get ("fontname") + ". TTF ";
Privatefontcollection = new privatefontcollection ();
Privatefontcollection. addfontfile (server. mappath ("./") + fontname );
Fontfamily = privatefontcollection. Families [0];

// Set Font Style
Int fontstyle = convert. toint32 (request. Form. Get ("fontstyle "));
Fontstyle style = fontstyle. Regular;
Switch (fontstyle)
{
Case 2:
Style = fontstyle. Bold;
Break;

Case 3:
Style = fontstyle. italic;
Break;

Case 4:
Style = (fontstyle. Bold) | (fontstyle. italic );
Break;

Case 5:
Style = fontstyle. Strikeout;
Break;

Case 6:
Style = fontstyle. Underline;
Break;
}
Font font = new font (fontfamily, fontsize, style, graphicsunit. pixel );

// Set Font Direction & alignment
Stringformat format = new stringformat ();
Int reverse = convert. toint32 (request. Form. Get ("reverse "));
If (reverse = 1 & Orientation = 1)
{
Format. formatflags = stringformatflags. directionrighttoleft;
}
Else if (reverse = 1 & Orientation> 1)
{
Stringbuilder temp = new stringbuilder ();
For (INT I = textLength-1; I> = 0; I --)
{
Temp. insert (textLength-1)-I, text [I]);
}
TEXT = temp. tostring ();
}
If (orientation> 1)
{
Rectf. x = width/4;
Rectf. width = fontsize-(fontsize/4 );
}
Int alignment = convert. toint32 (request. Form. Get ("alignment "));
If (alignment = 1)
{
Format. Alignment = stringalignment. Near;
}
Else if (alignment = 2)
{
Format. Alignment = stringalignment. Center;
}
Else
{
Format. Alignment = stringalignment. far;
}
Format. linealignment = stringalignment. Center;

// Draw any drop-shadow
Int dropshadow = convert. toint32 (request. Form. Get ("dropshadow "));
If (dropshadow> 0)
{
Color shadowcolor = color. fromname (request. Form. Get ("shadowcolor "));
Switch (dropshadow)
{
Case 1:
Rectf. offset (-3,-3 );
G. drawstring (text, Font, new solidbrush (shadowcolor), rectf, format );
Rectf. offset (+ 3, + 3 );
Break;

Case 2:
Rectf. offset (+ 3,-3 );
G. drawstring (text, Font, new solidbrush (shadowcolor), rectf, format );
Rectf. offset (-3, + 3 );
Break;

Case 3:
Rectf. offset (-3, + 3 );
G. drawstring (text, Font, new solidbrush (shadowcolor), rectf, format );
Rectf. offset (+ 3,-3 );
Break;

Case 4:
Rectf. offset (+ 3, + 3 );
G. drawstring (text, Font, new solidbrush (shadowcolor), rectf, format );
Rectf. offset (-3,-3 );
Break;
}
}

// Finally, draw the font
G. drawstring (text, Font, fgbrush, rectf, format );
Response. contenttype = "image/JPEG ";
PIC. Save (response. outputstream, imageformat. JPEG );

// dispose objects
pic. dispose ();
}< br> else
{< br> response. contenttype = "text/html";
response. write (" ");
response. write ("fonter.net V1.0
Create text images on-the-fly
");
response. write ("(c) 2003-onwards, zeddy Iskandar");
response. write (" ");
}< BR >}< br>

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.