测量字符串的尺寸(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?

原文地址:https://www.cnblogs.com/jintan/p/1308364.html