MeasureString和的DrawString差异差异、MeasureString、DrawString

由网友(无论你的多正经)分享简介:为什么我有21%增加 MeasureString()结果宽度size.Width = size.Width * 1.21f; 逃避自动换行的DrawString()?why do i have to increase MeasureString() result width by 21%size.Width =...

为什么我有21%增加 MeasureString()结果宽度 size.Width = size.Width * 1.21f; 逃避自动换行的DrawString()

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.

同样的字体,相同的StringFormat,在这两个函数中使用相同的文字。

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

这是由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);

我的code。

My code.

推荐答案

MeasureString()方法有一些问题,借鉴非ASCII字符时尤其如此。请尝试TextRenderer.MeasureText()来代替。

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

阅读全文

相关推荐

最新文章