exact width heigth string

Sep 14 2009 3:49 AM

 
Some years ago i wrote this script to calculated the dimensions of a text.
I would like tot use character.rages instead of measure.string.
Is this possible in this script?
Groeten,
Adri
 
<%@ 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>

<script runat="server">
//-------------------------------------
// based on fonter.net v1.0
//-------------------------------------

public void  Page_Load(object sender, System.EventArgs e)
{
   if (Request.HttpMethod == "POST")
   {
    string text = Request.Form.Get("text"); 
    int fontSize = Convert.ToInt32(Request.Form.Get("fontsize"));    
    int antialias = Convert.ToInt32(Request.Form.Get("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);    
    
   
    // 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);
    // calculate size of the string.
    
         // INITIALIZE GRAPHICS
    Bitmap img = new Bitmap(1,1);
    Graphics g = Graphics.FromImage(img);
 g.SmoothingMode = SmoothingMode.AntiAlias;
    if (antialias == 1) g.TextRenderingHint = TextRenderingHint.AntiAlias; 
   
    // DETERMINE CANVAS WIDTH AND HEIGHT
    SizeF stringLength = g.MeasureString(text, font);
     int width = (int)stringLength.Width;  
    int height = (int)stringLength.Height;

Answers (1)