3

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

27
  • Have you read the TextRenderer.MeasureText() MSDN doc already? It notes The MeasureText method requires that the text is drawn on a single line. so if tcaption.Text is multi-line that might be related? I'm not well versed in the TextRenderer object though Commented Apr 6, 2017 at 12:52
  • What is mygraphics? read this: stackoverflow.com/a/6705023/891715, or there may be wrong TextFormatFlags. Alternatively, you may want to try this method instead: msdn.microsoft.com/en-us/library/… Commented Apr 6, 2017 at 12:53
  • 1
    Wrong text rendering engine, you must use Graphics.MeasureString() to get in the ballpark. Commented Apr 6, 2017 at 15:34
  • 1
    You should mention that in the question of course. You are not using the same font height, use FontFamily.GetEmHeight(). Commented Apr 6, 2017 at 15:42
  • 1
    The purpose of MeasureString/Text are not a pixel-perfect measurement but results that make sure the consecutive strings place by that measurement to not touch or even overlap. This important when creating multiformat text. So, yes a few pixels whitespace around the text are indeed normal. Commented Apr 8, 2017 at 17:09

1 Answer 1

2
+50

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

Sign up to request clarification or add additional context in comments.

4 Comments

I had tried this .. But it does not work for the intended purpose.
@techno please take a look at my updated answer. I was able to get exact size of my text and create image containing that text. You can change SmoothingMode to get different result, but this should be a good start. If something isn't working please post some more code in Your question and maybe illustrate desired result.
Your answer is correct .. but i changed my approach.I trimmed the whitespace off the drawn bitmap and thus converted the text to image..
@techno good to hear You were able to solve Your problem :) If You want You can share Your code as answer so that anyone that has similar problem will have working solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.