删除图像上绘制的文本的顶部和底部填充

2024-04-18

我正在从指定的文本生成图像,但我面临一个问题:我无法删除生成的图像内绘制文本的顶部和底部填充。

我尝试在使用时更改字符串格式Graphics.DrawString(),但我只设法删除了左右填充。

private void button1_Click(object sender, EventArgs e)
{
    Font font = new Font("Arial", 52, FontStyle.Regular);
    Image i = GetTextAsImage(textBox1.Text,400, font, Color.Black, Color.LightGray);
    i.Save("myImage.jpeg", ImageFormat.Jpeg);
}

private Image GetTextAsImage(String text, int widthInPixel, Font textFont, Color textColor, Color backColor)
{
    //first, create a dummy bitmap just to get a graphics object
    Image img = new Bitmap(1, 1);
    Graphics drawing = Graphics.FromImage(img);

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

    //free up the dummy image and old graphics object
    img.Dispose();
    drawing.Dispose();

    //create a new image of the right size
    img = new Bitmap((int)textSize.Width, textFont.Height);

    drawing = Graphics.FromImage(img);
    drawing.SmoothingMode = SmoothingMode.AntiAlias;
    drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    //paint the background
    drawing.Clear(backColor);
    //create a brush for the text
    Brush textBrush = new SolidBrush(textColor);
    drawing.DrawString(text, textFont, textBrush, 0, 0,StringFormat.GenericTypographic);

    drawing.Save();
    textBrush.Dispose();
    drawing.Dispose();
    return img;
}

这是我得到的输出:

这是预期的输出:


我向您建议一种稍微不同的方法,使用图形路径 https://learn.microsoft.com/en-us/dotnet/api/system.drawing.drawing2d.graphicspath类来测量并在 Bitmap 对象上绘制文本。

优点是GraphicsPath类报告将绘制其包含的对象的实际坐标以及与特定字体相关的文本大小。
这些措施以RectagleF结构,调用GraphicsPath.GetBounds() https://learn.microsoft.com/en-us/dotnet/api/system.drawing.drawing2d.graphicspath.getbounds method.
基本构造函数假定 Pen 大小为 1 像素。

只需要注意一个(小)细节:GDI+ 位图对象仅接受以整数值表示的尺寸,而所有其他度量均以浮点值表示。
我们需要补偿舍入,但通常只是 ± 1 像素。

结果示例:

程序描述:

  • 定义字体系列和大小
  • 将文本字符串添加到GraphicsPath object
  • Get the GraphicsPath文本对象的边界矩形
  • 使用边界矩形 Size 构建 Bitmap 对象
  • 移动世界坐标,用图形.平移变换 https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.translatetransform,到边界矩形定义的坐标Y位置和笔大小,使用它们负值: 我们需要向后移动那个措施。
  • 绘制文本

另请参阅这些注释GraphicsPath和字体:
使用图形路径正确绘制文本 https://stackoverflow.com/a/53074638/7444103

示例代码:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;


string text = "This is my Text";
Font font = new Font("Arial", 52, FontStyle.Regular, GraphicsUnit.Point);
float penSize = 1f;

using (var path = new GraphicsPath()) {
    path.AddString(text, font.FontFamily, (int)font.Style, font.Size, Point.Empty, StringFormat.GenericTypographic);

    RectangleF textBounds = path.GetBounds();

    using (var bitmap = new Bitmap((int)textBounds.Width, (int)textBounds.Height, PixelFormat.Format32bppArgb))
    using (var g = Graphics.FromImage(bitmap)) 
    using (var brush = new SolidBrush(Color.LightGreen)) {
        g.SmoothingMode = SmoothingMode.AntiAlias;
        // When rendering without a GraphicsPath object
        //g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
        g.Clear(Color.Black);
        g.TranslateTransform(-(textBounds.X + penSize), -(textBounds.Y + penSize));
        g.FillPath(brush, path);
        
        bitmap.Save("[Image Path]", ImageFormat.Png);
        // Or: return (Bitmap)bitmap.Clone();
        // Or: return bitmap; declaring the bitmap without the using statement
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

删除图像上绘制的文本的顶部和底部填充 的相关文章

随机推荐