2010-01-22 18 views
5

Ciao Sto usando il metodo TextRenderer.MeasureText() per misurare la larghezza del testo per un determinato font. Uso il carattere Arial Unicode MS per misurare la larghezza, che è un font Unicode contenente caratteri per tutte le lingue. Il metodo restituisce larghezze diverse su server diversi. Entrambe le macchine hanno Windows 2003 e .net 3.5 SP1 installato.problema con TextRenderer.MeasureText

Ecco il codice che abbiamo usato

using (Graphics g = Graphics.FromImage(new Bitmap(1, 1))) 
{     
    width = TextRenderer.MeasureText(g, word, textFont, new Size(5, 5), TextFormatFlags.NoPadding).Width; 
} 

Qualsiasi idea del perché questo accade?

Io uso C# 2.0

+0

Perché _what_ accade? Hai appena postato del codice e nessuna descrizione del problema. – Oded

+0

Ciao, il problema è che se si esegue il codice su macchine diverse, restituisce larghezze diverse, quindi se ho più server, ogni server restituirà una larghezza diversa, che non è accettabile ... E non tutte le macchine restituiscono valori diversi solo alcuni di loro ..! – Amit

risposta

11

MeasureText non è noto per essere precisi.

Heres un modo migliore:

protected int _MeasureDisplayStringWidth (Graphics graphics, string text, Font font) 
    { 
     if (text == "") 
      return 0; 

     StringFormat format = new StringFormat (StringFormat.GenericDefault); 
     RectangleF rect = new RectangleF (0, 0, 1000, 1000); 
     CharacterRange[] ranges = { new CharacterRange (0, text.Length) }; 
     Region[] regions = new Region[1]; 

     format.SetMeasurableCharacterRanges (ranges); 
     format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; 

     regions = graphics.MeasureCharacterRanges (text, font, rect, format); 
     rect = regions[0].GetBounds (graphics); 

     return (int)(rect.Right); 
    } 
+0

Hey Grazie per gli input ... Fammi provare la stessa cosa. – Amit

+0

Hey, funziona bene ..! Grazie mille .. – Amit

11
//-------------------------------------------------------------------------------------- 
// MeasureText always adds about 1/2 em width of white space on the right, 
// even when NoPadding is specified. It returns zero for an empty string. 
// To get the precise string width, measure the width of a string containing a 
// single period and subtract that from the width of our original string plus a period. 
//-------------------------------------------------------------------------------------- 

public static Size MeasureText(string Text, Font Font) { 
    TextFormatFlags flags 
    = TextFormatFlags.Left 
    | TextFormatFlags.Top 
    | TextFormatFlags.NoPadding 
    | TextFormatFlags.NoPrefix; 
    Size szProposed = new Size(int.MaxValue, int.MaxValue); 
    Size sz1 = TextRenderer.MeasureText(".", Font, szProposed, flags); 
    Size sz2 = TextRenderer.MeasureText(Text + ".", Font, szProposed, flags); 
    return new Size(sz2.Width - sz1.Width, sz2.Height); 
} 
+0

grazie per l'aggiornamento .... – Amit

+0

Ottima soluzione. MSDN deve avere avuto informazioni su questo, ma alla fine non lo ha fatto. –

+0

Grazie. Mi ha aiutato. Anche se la cosa con 1/2 em è molto imbarazzante in primo luogo. –

1

Abbiamo avuto un problema simile diversi anni fa. Nel nostro caso, per qualche ragione avevamo versioni diverse dello stesso font installate su due macchine diverse. La versione del sistema operativo era la stessa, ma il carattere era diverso.

Poiché normalmente non si distribuisce un font di sistema con la configurazione dell'applicazione, i risultati di misurazione e output possono variare da un computer all'altro, in base alla versione del font.

Dal momento che dici ...

E non tutte le macchine restituiscono valori diversi solo alcuni di essi ..!

... questo è qualcosa che controllerò.