zoukankan      html  css  js  c++  java
  • 测量字符串的尺寸(C #)

    Measure String Size In Pixels (C #)

    Posted by Shahar
     

    Sometimes we need to know the width of a given string in pixels, do you know how to compute it? Before writing some long code, please notice that the. NET framework class library provides such a method. When Googling about this issue, we find the Graphics.MeasureString Method, here is how to use it:

     Graphics graphics = this. CreateGraphics ();
     SizeF textSize = graphics.MeasureString ( "How long am I?", This. Font); 

    Nice isn’t it? Well, there is one little problem here, how is the Graphics object created? The written code is a Windows Forms code, so the this is the Form itself. You can’t create the Graphics object by simply allocating it because it has no public constructor. The Control class contains a method for creating Graphics object, so you can create it once and use it whenever you need it later on. But, what if we are not using Windows Forms? Do we have to allocate some control just for that purpose? Fortunately, there is no need for that, we can use the TextRenderer::MeasureText Method: Nice isn't it? Well, there is one little problem here, how is the Graphics object created? The written code is a Windows Forms code, so the this is the Form itself. You can't create the Graphics object by simply allocating it because it has no public constructor. The Control class contains a method for creating Graphics object, so you can create it once and use it whenever you need it later on. But, what if we are not using Windows Forms? Do we have to allocate some control just for that purpose? Fortunately, there is no need for that, we can use the TextRenderer:: MeasureText Method:

     Size textSize = TextRenderer.MeasureText ( "How long am I?", Font); 

    The MeasureText is a static method of the TextRenderer class so it looks perfect. We can use it everywhere as a generic method for measuring text length in pixels. But can you see the problem with that method comparing to the MeasureString? Look at the returned object… The width and height returned are rounded down because the Size object is built from integers, not floats. This could lead to missing character on display unless you increase the size of the rectangle being measured a bit. In general, TextRenderer is less accurate because it uses GDI and Graphics uses GDI+. The MeasureText is a static method of the TextRenderer class so it looks perfect. We can use it everywhere as a generic method for measuring text length in pixels. But can you see the problem with that method comparing to the MeasureString? Look at the returned object ... The width and height returned are rounded down because the Size object is built from integers, not floats. This could lead to missing character on display unless you increase the size of the rectangle being measured a bit. In general, TextRenderer is less accurate because it uses GDI and Graphics uses GDI +.

    I think that this is really a minor problem, but you can always use the first method, just don’t think that it is perfect, it has its own problems … What option would you use? I think that this is really a minor problem, but you can always use the first method, just don't think that it is perfect, it has its own problems ... What option would you use?

  • 相关阅读:
    Ubuntu下将python从2.7升级到3.5
    Python:IOError: image file is truncated 的解决办法
    Google Hack
    Python:将utf-8格式的文件转换成gbk格式的文件
    Python:字符编码详解
    IIS下使用appcmd批量搭建网站
    C#:注册机的实现【提供源代码下载】
    C#:实现快捷键自定义设置
    C#:如何解决WebBrowser.DocumentCompleted事件的多次调用
    C#:WebBrowser中伪造referer,为何对流量统计器无效?
  • 原文地址:https://www.cnblogs.com/jintan/p/1308364.html
Copyright © 2011-2022 走看看