如何在图像上的文字下生成阴影 [英] How to generate shadow under words on an image

查看:57
本文介绍了如何在图像上的文字下生成阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

这是我想要的字体样式:

我可以做些什么来通过我的代码生成相同的样式吗?

有人知道如何使用SiteMapPath或ResolveURL对象将相对路径转换为物理路径吗?欢呼,

推荐答案

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

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.

示例:

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

这篇关于如何在图像上的文字下生成阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆