2012-01-29 38 views
4

如果我有一個System.Drawing.Bitmap具有相同的尺寸,例如100×100,50×50,我想利用這個代碼的單個字符畫:c#system.drawing計算一個字符的字體大小以填充一個正方形圖像

StringFormat stringFormat = new StringFormat(); 
stringFormat.Alignment = StringAlignment.Center; 
stringFormat.LineAlignment = StringAlignment.Center; 

font = new Font(_fontName, _fontSize, _fontStyle); 

GraphicsPath path = new GraphicsPath(); 

path.AddString("A", font.FontFamily, (int)font.Style, font.Size, rect, stringFormat); 

我怎麼會去計算_fontSize值,使其緊密配合圖像的尺寸內?

+0

您'll必須使用固定寬度的字體爲,可能比計算字符寬度更容易。 – Shai 2012-01-29 07:38:22

回答

-1

它會幫助ü:

float fontsizeem = height; 
    int charfits=0;int line=2; 
    for (; charfits < captchastring.Length || line < 1 ; fontsizeem--)//font support style correct // color fix 
    { 
     graphic.MeasureString(captchastring, new Font(fontFamily, fontsizeem, fontstyle), new SizeF((float)width*0.6f, (float)height*0.6f), format, out charfits, out line); 

    } 

    fontsizeem *= graphic.DpiY /72 ; // this unit converting fucks me out :(((((((

    path.AddString(captchastring, fontFamily,(int) fontstyle,fontsizeem,rectangle, format);// -- size 
相關問題