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

时间:2017-04-06 12:47:37

标签: c# .net bitmap gdi+ system.drawing

我使用以下代码将文本绘制到位图

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%的差异?

我做错了什么?请指教。

更新:

我想在Bitmap上绘制一个Text和一个Image,所以为了适应我们创建像这样的Bitmap

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

然后我像这样

从Bitmap创建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添加到imageposition x坐标以纠正测量问题。

希望我的更新能够为问题提供更多启示......我做错了什么? 请指教..

1 个答案:

答案 0 :(得分:1)

而不是使用TextRenderer使用GraphicsPath:

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

以下是样本结果:

enter image description here enter image description here enter image description here