使用TextRenderer无法获取正确的绘制文本大小

3

我正在使用以下代码将文本绘制到位图上:

GraphicsPath pth = new GraphicsPath();
var style = (int)myfont.Style;
pth.AddString(tcaption.Text, myfont.FontFamily, style, myfont.Size, point, StringFormat.GenericTypographic);
p = new Pen(new SolidBrush(bc), 2f);
mygraphics.DrawPath(p, pth);

我正在使用TextRenderer来测量字符串的大小。
int  Width = TextRenderer.MeasureText(tcaption.Text, myfont).Width;

但是这样并不能产生正确大小的绘制字符串;实际绘制字符串的大小与所需大小相差约20-30%?

我做错了什么?请给予建议。

更新:

我想在位图上绘制文本和图像,为了容纳两者,我创建了一个如下的位图:

intWidth = TextRenderer.MeasureText(tcaption.Text, cfont).Width + image.Width;
intHeight = TextRenderer.MeasureText(tcaption.Text, cfont).Height +image.Height;
tempimage= new Bitmap(intWidth, intHeight);

然后,我从位图中创建了Graphics对象,如下所示:
 using (Graphics newg = Graphics.FromImage(tempimage))

@Hans Passant

我也尝试过Graphics.MeasureString作为TextRenderer的替代方案。

现在,我已经设置了文本和图像的位置 - 我需要将图像绘制在左上角。

                imageposy = 0;
                imageposx = 10;                
                textposy = image.Height;                     
                textposx = 0;

然后我像这样绘制文本。
   po=new Point(textposx, textposy);
   newg.SmoothingMode = SmoothingMode.AntiAlias;                                      
   GraphicsPath pth = new GraphicsPath();
   var style = (int)myfont.Style;
   pth.AddString(tcaption.Text, myfont.FontFamily, style, myfont.Size, po, 
   StringFormat.GenericTypographic);
   newg.FillPath(new SolidBrush(fc), pth);

现在我像这样绘制图像。
 Rectangle nrect = new Rectangle(imageposx, imageposy, image.Width, 
 image.Height);
 objGraphics = Graphics.FromImage(tempimage);
 objGraphics.DrawImage(image, nrect);

正如您所看到的,我需要将偏移量10添加到图像位置的x坐标上以纠正测量问题。

希望我的更新能更好地解决问题...我错在哪里了?请给予建议...


你已经阅读了 TextRenderer.MeasureText() 的 MSDN 文档吗?它指出 MeasureText 方法要求文本在单行上绘制。所以如果 tcaption.Text 是多行的,那可能会有关系?不过我对 TextRenderer 对象并不是很熟悉。 - sab669
1
错误的文本渲染引擎,你必须使用Graphics.MeasureString()来获取大致值。 - Hans Passant
1
当然,在问题中应该提到这一点。您没有使用相同的字体高度,请使用FontFamily.GetEmHeight()。 - Hans Passant
1
MeasureString/Text 的目的不是像素完美测量,而是确保按照该测量方式排列的连续字符串不会接触或重叠。在创建多格式文本时,这一点非常重要。因此,是的,在文本周围留下几个像素的空白确实很正常。 - TaW
显示剩余22条评论
1个回答

2

使用GraphicsPath而不是TextRenderer:

var path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, size, new Point(0, 0), StringFormat.GenericTypographic);
var area = Rectangle.Round(path.GetBounds());

这里是生成文本大小图片的示例代码:

private Image DrawText(String text, Font font, int size, Color textColor, Color backColor)
{
    var path = new GraphicsPath();
    path.AddString(text, font.FontFamily, (int)font.Style, size, new Point(0, 0), StringFormat.GenericTypographic);
    var area = Rectangle.Round(path.GetBounds());

    Rectangle br = Rectangle.Round(path.GetBounds());
    var img = new Bitmap(br.Width, br.Height);

    var drawing = Graphics.FromImage(img);
    drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
    drawing.SmoothingMode = SmoothingMode.HighSpeed;
    drawing.Clear(backColor);

    drawing.TranslateTransform((img.Width - br.Width) / 2 - br.X, (img.Height - br.Height) / 2 - br.Y);
    drawing.FillPath(Brushes.Black, path);
    Brush textBrush = new SolidBrush(textColor);

    drawing.Save();

    textBrush.Dispose();
    drawing.Dispose();

    return img;
}

以下是示例结果:

输入图像描述 输入图像描述 输入图像描述


我曾尝试过这个方法,但它并不能达到预期的目的。 - techno
@techno请看一下我的更新答案。我能够获得文本的确切大小并创建包含该文本的图像。您可以更改“SmoothingMode”以获得不同的结果,但这应该是一个很好的开始。如果有什么问题,请在您的问题中发布更多代码,并可能说明所需的结果。 - Misiu
你的答案是正确的,但我改变了我的方法。我从绘制的位图中修剪了空格,因此将文本转换为图像。 - techno
@techno 很高兴听到您能够解决问题 :) 如果您愿意,可以将您的代码分享为答案,这样有类似问题的任何人都将拥有可行的解决方案。 - Misiu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接