2012-09-25 10 views
5
public static System.Drawing.Image GenerateGiftCard(String text, Font font, Color textColor) 
{ 
    System.Drawing.Image img = Bitmap.FromFile(@"G:\xxx\images\gift-card.jpg"); 
    Graphics drawing = Graphics.FromImage(img); 

    //measure the string to see how big the image needs to be 
    SizeF textSize = drawing.MeasureString(text, font); 

    //create a brush for the text 
    Brush textBrush = new SolidBrush(textColor); 

    float x, y; 

    x = img.Width/2 - textSize.Width/2; 
    y = img.Height/2 - textSize.Height/2; 

    drawing.DrawString(text, font, textBrush, x, y); 

    drawing.Save(); 

    textBrush.Dispose(); 
    drawing.Dispose(); 

    return img; 
} 

Pero el texto generado por este código es "simple", no dimensional y sin sombra debajo de él.Cómo generar sombra debajo de las palabras en una imagen

Este es el estilo de fuente Quiero:

Beautiful characters

¿Hay algo que pueda hacer para generar el mismo estilo a través de mi código?

¿Alguien sabe cómo usar los objetos SiteMapPath o ResolveURL para transferir una ruta relativa a una física? vivas,

+1

[Shadow] (http://msdn.microsoft.com/en-us/library/xeawz664 (v = vs.80) .aspx) - Para crear la sombra , el texto se dibuja dos veces. La primera vez que está en gris y offset. La segunda vez es en negro. – adatapost

+0

@AVD buena idea, lo intentaré ahora y le haré saber el resultado. – Franva

+0

@AVD, pero ¿cómo hacer el estilo de fuente? – Franva

Respuesta

6

Primero renderice la sombra dibujando el texto con un pincel más oscuro, opcionalmente translúcido, en un desplazamiento. Después de que se represente la sombra, superponga el texto normal.

Ejemplo:

public static System.Drawing.Image GenerateGiftCard(String text, Font font, Color textColor, Color shadowColor, SizeF shadowOffset) 
{ 
    System.Drawing.Image img = Bitmap.FromFile(@"G:\xxxx\images\gift-card.jpg"); 
    Graphics drawing = Graphics.FromImage(img); 

    //measure the string to see how big the image needs to be 
    SizeF textSize = drawing.MeasureString(text, font); 

    //create a brush for the text 
    Brush shadowBrush = new SolidBrush(shadowColor); // <-- Here 
    Brush textBrush = new SolidBrush(textColor); 

    float x, y; 

    x = img.Width/2 - textSize.Width/2; 
    y = img.Height/2 - textSize.Height/2; 

    drawing.DrawString(text, font, shadowBrush, x + shadowOffset.Width, y + shadowOffset.Height); // <-- Here 
    drawing.DrawString(text, font, textBrush, x, y); 

    drawing.Save(); 

    textBrush.Dispose(); 
    drawing.Dispose(); 

    return img; 
} 
+0

thx por su respuesta. ¿Tienes alguna idea sobre el estilo de fuente? Además, ¿sabe cómo usar los objetos SiteMapPath o ResolveURL para transferir una ruta relativa a una ruta física? cheers, – Franva

+0

No sé sobre el estilo de fuente. No lo reconozco Si desea resolver una URL, estoy seguro de que hay una pregunta de desbordamiento de pila solo para eso, pero si no desea buscarla, intente con 'Server.MapPath (" ~/ruta/archivo.ext '') '. – Dan

+0

finalmente, el Server.MapPath() es lo que estaba buscando y funciona. Un gran gracias :) – Franva

Cuestiones relacionadas