如何在图像上的单词下生成阴影

时间:2012-09-25 02:24:32

标签: c# asp.net image-processing

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

但是这段代码生成的文字是“普通的”,不是维度,也不是阴影。

这是我想要的字体样式

Beautiful characters

我可以通过代码生成相同的样式吗?

有谁知道如何使用SiteMapPath或ResolveURL对象将相对路径传输到物理路径?欢呼声,

1 个答案:

答案 0 :(得分:6)

首先通过在偏移处使用较暗的,可选的半透明画笔绘制文本来渲染阴影。渲染阴影后,覆盖常规文本。

示例

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