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?

  • 相关阅读:
    JavaScript定时器及相关面试题
    单线程JavaScript
    webpack基础入门
    SQL Server 服务器器信息备份(二)--权限备份
    SQL Server 服务器器信息备份(一)--login新建脚本备份
    Raid与DAN、SAN、NAS基础
    AlwaysOn可用性组功能测试(三)--其他测试
    AlwaysOn可用性组功能测试(二)--SQL Server群集故障转移对AlwaysOn可用性组的影响
    AlwaysOn可用性组功能测试(一)--AlwaysOn故障转移测试
    AlwaysOn可用性组测试环境安装与配置(一)--SQL群集环境搭建
  • 原文地址:https://www.cnblogs.com/jintan/p/1308364.html
Copyright © 2011-2022 走看看