MeasureString和的DrawString差异 [英] MeasureString and DrawString difference

查看:1049
本文介绍了MeasureString和的DrawString差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我有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.

这篇关于MeasureString和的DrawString差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆