2011-07-19 35 views
5

Estoy escribiendo texto sobre una imagen usando el método Graphics DrawString, vinculando mi texto con un RectangleF. Aquí está mi código:C# Graphics.DrawString RectangleF Auto-Height: ¿Cómo encontrar esa altura?

//Write header2 
RectangleF header2Rect = new RectangleF(); 
header2Rect.Width = 600; 
header2Rect.Location = new Point(30, 105); 
graphicImage.DrawString(header2, new Font("Gotham Medium", 28, FontStyle.Bold),brush, header2Rect); 
//Write Description 
RectangleF descrRect = new RectangleF(); 
descrRect.Width = 600; 
int measurement = ((int)graphicImage.MeasureString(header2, new Font("Gotham Medium", 28, FontStyle.Bold)).Height); 
var yindex = int.Parse(105 + header2Rect.Height.ToString()); 
descrRect.Location = new Point(30, 105+measurement); 
graphicImage.DrawString(description.ToLower(), new Font("Gotham", 24, FontStyle.Italic), SystemBrushes.WindowText, descrRect); 

Esto funciona para algunos casos (es decir, cuando header2 es solamente 1 línea de longitud.), Pero mi measurement variable sólo mide la altura de la fuente, no el todo DrawString rectángulo. No quiero establecer una altura header2Rect estática porque las alturas cambian según ese texto.

yindex no funciona porque header2Rect.Height = 0. ¿Hay alguna manera de ver cuántas líneas tiene mi header2?

¿Solo necesito hacer MeasureString ancho y dividirlo por mi ancho de rectángulo de frontera, luego multiplicar por la altura MeasureString? Supongo que hay una mejor manera.

Gracias

[EDIT] Parece que la altura es en realidad 0, pero el texto simplemente se desborda fuera, pero la anchura todavía constriñe el ajuste de texto. Acabo de hacer algunos cálculos matemáticos para encontrar la altura, pero ojalá hubiera una mejor manera.

Respuesta

6

que nunca están fijando a sus alturas del rectángulo:

private void panel1_Paint(object sender, PaintEventArgs e) 
{ 
    string header2 = "This is a much, much longer Header"; 
    string description = "This is a description of the header."; 

    RectangleF header2Rect = new RectangleF(); 
    using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold)) 
    { 
    header2Rect.Location = new Point(30, 105); 
    header2Rect.Size = new Size(600, ((int)e.Graphics.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height)); 
    e.Graphics.DrawString(header2, useFont, Brushes.Black, header2Rect); 
    } 

    RectangleF descrRect = new RectangleF(); 
    using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Italic)) 
    { 
    descrRect.Location = new Point(30, (int)header2Rect.Bottom); 
    descrRect.Size = new Size(600, ((int)e.Graphics.MeasureString(description, useFont, 600, StringFormat.GenericTypographic).Height)); 
    e.Graphics.DrawString(description.ToLower(), useFont, SystemBrushes.WindowText, descrRect); 
    } 

} 
+0

Usted es increíble! ¡Esto es justo lo que estaba buscando! Gracias. –

+0

En mi proyecto de C++, solo 'StringFormat :: GenericDefault()' funciona correctamente en lugar de 'StringFormat :: GenericTypographic()'. No estoy seguro de cuál es la razón. –

Cuestiones relacionadas