views:

39

answers:

3

Hello,

why do i have to increase MeasureString() result width by 21% size.Width = size.Width * 1.21f; to evade Word Wrap in DrawString()?

I need a solution to get the exact result.

Same font, same stringformat, same text used in both functions.


From answer by OP:

  SizeF size = graphics.MeasureString(element.Currency, Currencyfont, new PointF(0, 0), strFormatLeft);
  size.Width = size.Width * 1.21f;
  int freespace = rect.Width - (int)size.Width;
  if (freespace < ImageSize) { if (freespace > 0) ImageSize = freespace; else ImageSize = 0; }
  int FlagY = y + (CurrencySize - ImageSize) / 2;
  int FlagX = (freespace - ImageSize) / 2;
  graphics.DrawImage(GetResourseImage(@"Flags." + element.Flag.ToUpper() + ".png"), 
         new Rectangle(FlagX, FlagY, ImageSize, ImageSize));
  graphics.DrawString(element.Currency, Currencyfont, Brushes.Black, 
       new Rectangle(FlagX + ImageSize, rect.Y, (int)(size.Width), CurrencySize), strFormatLeft);

My code.

+2  A: 

MeasureString() method had some issues, especially when drawing non-ASCII characters. Please try TextRenderer.MeasureText() instead.

Paweł Dyda
A: 

You likely need to add the following the the StringFormat flags:

StringFormatFlags.FitBlackBox
leppie