2009-06-09 24 views
10

Qual è il modo migliore per disegnare Stringa al centro di un rettangolo? La dimensione del carattere del testo può essere ridotta per adattarla. Nella maggior parte dei casi il testo è troppo grande per adattarsi a un determinato font, quindi è necessario ridurlo.Disegna il testo al centro

risposta

3

Si sta lavorando per me sapere. Questo è quello che ho fatto

Size textSize = TextRenderer.MeasureText(Text, Font); 
float presentFontSize = Font.Size; 
Font newFont = new Font(Font.FontFamily, presentFontSize, Font.Style); 
while ((textSize.Width>textBoundary.Width || textSize.Height > textBoundary.Height) && presentFontSize-0.2F>0) 
{ 
    presentFontSize -= 0.2F; 
    newFont = new Font(Font.FontFamily,presentFontSize,Font.Style); 
    textSize = TextRenderer.MeasureText(ButtonText, newFont); 
} 
stringFormat sf; 
sf.Alignment = StringAlignment.Center; 
sf.LineAlignment = StringAlignment.Center; 
e.Graphics.DrawString(Text,newFont,Brushes.Black,textBoundary, sf); 
+0

Ho visto che hai pubblicato la tua soluzione di lavoro quando ho postato la mia versione, ma ho lasciato il mio perché è più efficiente (ottiene la dimensione del font senza loop) e penso che il tuo codice possa perdere memoria, creando molti oggetti font ma non chiamare Dispose su di loro. –

+1

Qual è il valore del parametro "textBoundary"? Si prega di dichiararlo e inizializzarlo. –

+0

OKAY I GOT IT. textBoundary viene creato come sotto String imgsrc = "PERCORSO DEL FILE DI IMMAGINE SU DISCO"; Bitmap bmp = new Bitmap (System.Drawing.Image.FromFile (imgsrc)); RectangleF textBoundary = new Rectangle (0, 0, bmp.Width, bmp.Height - 30); –

0

Determinare la dimensione del testo da disegnare e quindi determinare l'offset per l'inizio della stringa dal centro del rettangoloF e disegnarlo.

0

Ottieni larghezza/2 e altezza/2 del rettangolo, quindi utilizzando System.Graphics.MeasureString per ottenere le dimensioni della stringa, ancora a metà e sottraendo dai valori di larghezza/altezza precedenti e si finisce con la X , Coordinata Y per disegnare la stringa in modo che sia centrata.

16

Questo codice centra il testo orizzontalmente e verticalmente:

stringFormat sf; 
sf.Alignment = StringAlignment.Center; 
sf.LineAlignment = StringAlignment.Center; 
grp.DrawString(text, font, Brushes.Black, rectf, sf); 
+0

come ridurre la dimensione del carattere? – Prithis

10

ho giocato intorno con esso un po 'e ho trovato questa soluzione (assumendo che il RectangleF rect e string text sono già definite):

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

using (Graphics g = this.CreateGraphics()) 
{ 
    SizeF s = g.MeasureString(text, this.Font); 
    float fontScale = Math.Max(s.Width/rect.Width, s.Height/rect.Height); 
    using (Font font = new Font(this.Font.FontFamily, this.Font.SizeInPoints/fontScale, GraphicsUnit.Point)) 
    { 
     g.DrawString(text, font, Brushes.Black, rect, stringFormat); 
    } 
} 
0

Facile da usare :)

public static void DrawStringCenter(Image image, string s, Font font, Color color, RectangleF layoutRectangle) 
    { 
     var graphics = Graphics.FromImage(image); 

     var brush = new SolidBrush(color); 

     var format = new StringFormat 
     { 
      Alignment = StringAlignment.Center, 
      LineAlignment = StringAlignment.Center 
     }; 

     graphics.DrawString(s, font, brush, layoutRectangle, format); 
    } 
Problemi correlati