测量字符串的尺寸(C #)
Measure String Size In Pixels (C #)
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?
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 深入理解Mybatis分库分表执行原理
· 使用 Dify + LLM 构建精确任务处理应用