• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

How to Create Text Image on the fly with ASP.NET

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

摘至: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)
{
   if (Request.HttpMethod == "POST")
   {
    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)
    {
     width  = (fontSize * textLength) - ((textLength * fontSize)/3);
     height = fontSize + 20;
    }
    else
    {
     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 privateFontCollection = new PrivateFontCollection();
    privateFontCollection.AddFontFile(Server.MapPath("./") + fontName);
    FontFamily 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();
   }
   else
   {
    Response.ContentType = "text/html";
    Response.Write("<html><body>");
    Response.Write("fonter.net v1.0 <br> Create Text Images On-the-Fly <br>");
    Response.Write("(C)2003-onwards, Zeddy Iskandar");
    Response.Write("</body></html>");
   }
}
</script></PRE>


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NETMVC5+EF6+EasyUI后台管理系统(5)-EF增删改查发布时间:2022-07-10
下一篇:
asp.net动态生成按钮Button控件发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap