简体   繁体   English

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

[英]Unable to get correct Size of DrawnText using TextRenderer

Im drawing Text using the following code onto a Bitmap 我使用以下代码将文本绘制到位图上

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

I'm using the TextRenderer to measure the size of the string.. 我正在使用TextRenderer来测量字符串的大小..

int  Width = TextRenderer.MeasureText(tcaption.Text, myfont).Width;

But this does not produce the correct size of the drawn string; 但这不会产生绘制字符串的正确大小; there is around 20-30% difference from the actual size of the drawn string? 与绘制的字符串的实际大小有大约20-30%的差异?

What im i doing wrong? 我做错了什么? Please advice. 请指教。

UPDATE: 更新:

I want to draw a Text and an Image onto a Bitmap,so inorder to accommodate both i'm creating an Bitmap like this 我想在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);

Then i create Graphics object from the Bitmap like this 然后我像这样从Bitmap创建Graphics对象

 using (Graphics newg = Graphics.FromImage(tempimage))

@Hans Passant @Hans Passant

I have also tried the Graphics.MeasureString as an alternative to TextRenderer 我还尝试使用Graphics.MeasureString作为TextRenderer的替代品

Now i set the position of the text and image-I need to draw the image at the top left corner .. so 现在我设置文本和图像的位置 - 我需要在左上角绘制图像..所以

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

Then i draw the text like this 然后我画这样的文字

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

Now i draw the image like this 现在我画这样的图像

 Rectangle nrect = new Rectangle(imageposx, imageposy, image.Width, 
 image.Height);
 objGraphics = Graphics.FromImage(tempimage);
 objGraphics.DrawImage(image, nrect);

As you have seen i need to add the offset 10 to imageposition x coordinate to correct the measurement issue. 如您所见,我需要将偏移量10添加到imageposition x坐标以纠正测量问题。

Hope my update throws more light into the question... what im i doing wrong? 希望我的更新能够为问题提供更多启示......我做错了什么? Please advice.. 请指教..

instead of using TextRenderer use GraphicsPath: 而不是使用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());

Here is sample code that generates image with size of text: 以下是生成具有文本大小的图像的示例代码:

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

Here are sample results: 以下是示例结果:

在此输入图像描述 在此输入图像描述 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM