Can't get correct size of DrawnText using TextRenderer

Im drawing Text using the following code in 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 am using TextRenderer

to measure the size of a string.

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

      

But this does not give the correct size for the drawn line; is there a 20-30% difference from the actual size of the hand drawn line?

What am I doing wrong? Please advice.

UPDATE:

I want to draw text and image onto a bitmap, so for placement, both I create a Bitmap and

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 a Graphics object from Bitmap as follows

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

      

@Hans Passant

I also tried Graphics.MeasureString

as an alternative to TextRenderer

Now I am setting the position of the text and the image - I need to draw the image in the upper 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 am drawing the image as follows

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

      

As you saw, I need to add an offset 10

to the imageposition x coordinate to fix the measurement problem.

Hope my update sheds more light on the question ... what am I doing wrong? Please advice.

+3


source to share


1 answer


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 some sample code that generates a text-sized image:



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

      

Below are examples of results:

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

+1


source







All Articles