Search code examples
c#.netbitmapgdi+system.drawing

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..

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?

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

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

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

@Hans Passant

I have also tried the Graphics.MeasureString as an alternative to 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.

Hope my update throws more light into the question... what im i doing wrong? Please advice..


Solution

  • instead of using TextRenderer use 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:

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