×
您的位置: 首页 > 编程笔记

MeasureString和的DrawString差异(MeasureString and DrawString difference)

DotNET Core 时间:2020-06-19  查看:767   收藏

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.

解决方案

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

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

我需要一个解决方案得到确切的结果。

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


这是由OP答案:

 的SizeF大小= graphics.MeasureString(element.Currency,Currencyfont,新的PointF(0,0),strFormatLeft);
  size.Width = size.Width * 1.21f;
  INT可用空间= rect.Width  - (INT)size.Width;
  如果(可用空间&所述; IMAGESIZE){如果(可用空间大于0)IMAGESIZE =自由空间;别的IMAGESIZE = 0; }
  INT FlagY = Y +(CurrencySize  -  IMAGESIZE)/ 2;
  INT FlagX =(自由空间 -  IMAGESIZE)/ 2;
  graphics.DrawImage(GetResourseImage(@标志。+ element.Flag.ToUpper()+巴纽),
         新的矩形(FlagX,FlagY,IMAGESIZE,IMAGESIZE));
  graphics.DrawString(element.Currency,Currencyfont,Brushes.Black,
       新的矩形(FlagX + IMAGESIZE,rect.Y,(INT)(size.Width),CurrencySize),strFormatLeft);

我的code。

解决方案

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

 

0% (0)
0% (0)