C# 无法使用TextRenderer获取DrawnText的正确大小

C# 无法使用TextRenderer获取DrawnText的正确大小,c#,.net,bitmap,gdi+,system.drawing,C#,.net,Bitmap,Gdi+,System.drawing,Im使用以下代码在位图上绘制文本 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.DrawPa

Im使用以下代码在位图上绘制文本

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);
我正在使用
textdrenderer
来测量字符串的大小

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);
 using (Graphics newg = Graphics.FromImage(tempimage))
   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);
然后我从位图创建图形对象,如下所示

intWidth = TextRenderer.MeasureText(tcaption.Text, cfont).Width + image.Width;
intHeight = TextRenderer.MeasureText(tcaption.Text, cfont).Height +image.Height;
tempimage= new Bitmap(intWidth, intHeight);
 using (Graphics newg = Graphics.FromImage(tempimage))
   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);
@汉斯·帕桑

我还尝试了
Graphics.MeasureString
作为TextRenderer的替代品

现在我设置了文本和图像的位置,我需要在左上角绘制图像。。所以

                imageposy = 0;
                imageposx = 10;                
                textposy = image.Height;                     
                textposx = 0;
然后我像这样画文本

intWidth = TextRenderer.MeasureText(tcaption.Text, cfont).Width + image.Width;
intHeight = TextRenderer.MeasureText(tcaption.Text, cfont).Height +image.Height;
tempimage= new Bitmap(intWidth, intHeight);
 using (Graphics newg = Graphics.FromImage(tempimage))
   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);
现在我画了这样的图像

intWidth = TextRenderer.MeasureText(tcaption.Text, cfont).Width + image.Width;
intHeight = TextRenderer.MeasureText(tcaption.Text, cfont).Height +image.Height;
tempimage= new Bitmap(intWidth, intHeight);
 using (Graphics newg = Graphics.FromImage(tempimage))
   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);
如您所见,我需要向imageposition x坐标添加偏移量
10
,以纠正测量问题

希望我的更新能为这个问题提供更多信息。。。我做错了什么?
请给我一些建议

使用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;
}
以下是示例结果:


您已经看过MSDN文档了吗?它注意到,MeasureText方法要求将文本绘制在一行上。因此,如果
t选项。文本是否为可能相关的多行?我不太熟悉
textrender
对象思想什么是我的图形?请阅读以下内容:,否则可能有错误的TextFormatFlags。或者,您可能想尝试此方法:这可能也是您感兴趣的,这是关于在textrenderer中填充的:错误的文本渲染引擎,您必须使用Graphics.MeasureString()才能进入大致范围。当然,您应该在问题中提到这一点。您使用的字体高度不同,请使用FontFamily.GetEmHeight()。我已经尝试过这个。。但它并没有达到预期的目的。@techno请看一下我的最新答案。我能够得到文本的精确大小,并创建包含该文本的图像。您可以更改
SmoothingMode
以获得不同的结果,但这应该是一个良好的开端。如果有些东西不起作用,请在你的问题中再贴一些代码,也许可以说明你想要的结果。你的答案是正确的。。但我改变了我的方法。我将绘制的位图中的空白部分修剪掉,从而将文本转换为图像。@techno很高兴听到您能够解决您的问题:)如果您愿意,您可以将代码作为答案共享,以便任何有类似问题的人都能找到有效的解决方案。