获得任何给定字符的像素宽度 [英] Obtaining the Width in Pixels of any given character

查看:234
本文介绍了获得任何给定字符的像素宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个真的很棒的想法,但不能确定在.NET Framework中是否有任何类(任何版本,最好是3.5或4.0),允许你传入一个字符,并获取像素的宽度该字符,无论使用哪种字体,字体大小或字体装饰。有人能指点我的方向吗?一个类/东西甚至存在这样的东西?

解决方案

检查 Graphics.MeasureString 方法。

链接:
$ b

  SizeF charSize = e.Graphics.MeasureString(X,new Font(Arial,16)); 
//做charSize的东西...

上面的例子假定你在Paint事件处理程序的函数体和Graphics对象已经为您创建并作为事件参数传入。如果你不想或不能在一个Paint处理程序中执行,你可以在方便的时候用 Control.CreateGraphics


I have this really awesome idea, but cannot find out if there are any classes in the .NET Framework (any version, preferrably 3.5 or 4.0) that allow you to pass in a character, and get back the width in pixels of that character, no matter which font, or font size or font-decoration is being used. Can someone please point me in the right direction? Does a class/something even exist for something like this?

解决方案

Check out the Graphics.MeasureString method.

Code sample adapted from the link:

SizeF charSize = e.Graphics.MeasureString("X", new Font("Arial", 16));
// do stuff with charSize ...

The sample above assumes you're in the function body of a Paint event handler and the Graphics object is already created for you and passed in as an event parameter. If you don't want or can't do it in a Paint handler you can create a graphics object at your convenience with Control.CreateGraphics.

这篇关于获得任何给定字符的像素宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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