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



The MeasureText method requires that the text is drawn on a single line.so iftcaption.Textis multi-line that might be related? I'm not well versed in theTextRendererobject though