2010-11-17 9 views
12

Tengo el siguiente código. ¿Hay una manera fácil de poner un bosquejo en el texto que estoy escribiendo?Texto del esquema con System.Drawing?

var imageEncoder = Encoder.Quality; 
var imageEncoderParameters = new EncoderParameters(1); 
imageEncoderParameters.Param[0] = new EncoderParameter(imageEncoder, 100L); 

var productImage = GetImageFromByteArray(myViewModel.ProductImage.DatabaseFile.FileContents); 
var graphics = Graphics.FromImage(productImage); 

var font = new Font("Segoe Script", 24); 
var brush = Brushes.Orange; 

var container = new Rectangle(myViewModel.ContainerX, myViewModel.ContainerY,          myViewModel.ContainerWidth,            myViewModel.ContainerHeight); 

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

graphics.DrawString(customizationText, font, brush, container, stringFormat); 

Respuesta

23

Sí. En lugar de DrawString, utilice la siguiente secuencia de llamadas:

Si es necesario utilizar GraphicsPath.AddString junto Graphics.DrawString, es necesario convertir los tamaños de fuente, becau se Graphics.DrawString espera "tamaño de punto" mientras que GraphicsPath.AddString espera "tamaño de em". La fórmula de conversión es simplemente emSize = g.DpiY * pointSize/72.

Aquí está un ejemplo de código:

// assuming g is the Graphics object on which you want to draw the text 
GraphicsPath p = new GraphicsPath(); 
p.AddString(
    "My Text String",    // text to draw 
    FontFamily.GenericSansSerif, // or any other font family 
    (int) FontStyle.Regular,  // font style (bold, italic, etc.) 
    g.DpiY * fontSize/72,  // em size 
    new Point(0, 0),    // location where to draw text 
    new StringFormat());   // set options here (e.g. center alignment) 
g.DrawPath(Pens.Black, p); 
// + g.FillPath if you want it filled as well 
+0

Muchas gracias por la respuesta, pero me parece que no puede conseguir que esto funcione. No estoy seguro de qué sobrecarga (s) usar para estos métodos. ¿La única línea que saco de mi programa actual será la línea graphics.DrawString()? –

+0

[Sí.] (Http://meta.stackexchange.com/questions/700/) – Timwi

+0

Gracias por esta información, hice justo lo que dijiste ... Pero, por supuesto, me di cuenta de que no hay anti-aliasing por lo el texto se ve bastante horrible. :(No estoy seguro de qué hacer al respecto todavía. –

Cuestiones relacionadas