c# - 如何在图像上的文字下生成阴影

标签 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 对象将相对路径传输到物理路径?干杯,

最佳答案

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

示例:

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

关于c# - 如何在图像上的文字下生成阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12575429/

相关文章:

c# - 处理 servicestack Html5ModeFeature 插件中的任何默认文档类型

asp.net - 导航样式 - 仅选择子 li

c# - rendersection 的这段代码是什么意思?

c# - 防止在 Visual Studio 中加载符号

c# - 有条件地忽略属性序列化

c# - int num = new int();当这条线执行时会发生什么?

matlab - 我的处女帖 : Having trouble with ind2rgb function in matlab

matlab - 如何提高Matlab中SVM训练和分类的准确性?

python - 如何像在Photoshop上一样使用python调整图像颜色直方图

c# - 'var' 的目的是什么?