Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate shadow under words on an image

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;
}

But the text generated by this code is "plain" not dimensional and no shadow beneath it.

This is the font style I want:

Beautiful characters

Is there anything I can do to generate the same style via my code?

Does anyone know how to use SiteMapPath or ResolveURL objects to transfer a relative path to a physical one? cheers,

like image 938
Franva Avatar asked Sep 25 '12 02:09

Franva


1 Answers

First render the shadow by drawing the text with a darker, optionally translucent brush at an offset. After the shadow is rendered, overlay the regular text.

Example:

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;
}
like image 77
Dan Avatar answered Oct 29 '22 01:10

Dan