在矩形内填充文本
我正在使用 GDI+ 在 Graphics 对象上绘制字符串。
我希望字符串适合预定义的矩形(不破坏任何行)
除了在循环中使用 TextRenderer.MeasureString() 直到返回所需的大小之外,还有其他方法吗?
像这样的东西:
DrawScaledString(Graphics g, string myString, Rectangle rect)
I'm using GDI+ to draw a string on a Graphics object.
I want the string to fit inside a pre-defined rectangle (without breaking any lines)
Is there's anyway of doing this besides using TextRenderer.MeasureString() in a loop until the desirable size is returned?
something like:
DrawScaledString(Graphics g, string myString, Rectangle rect)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 ScaleTransform
但您可能会得到一些别名效果。
编辑:
但如果你想更改字体大小,我想你可以更改上面代码中使用
scale
来设置字体大小,而不是使用缩放变换。尝试两者并比较结果的质量。You can use the ScaleTransform
But you might get some alias effects.
Edit:
But if you want to change the font size instead I guess you can change the font size with
scale
in the code above instead of using the scale transform. Try both and compare the quality of the result.这是该问题的另一个解决方案,它有点密集,因为它需要相当多的字体创建和销毁,但可能效果更好,具体取决于您的环境和需求:
要使用它,只需创建一个新类并调用 Render() 即可。请注意,这是专门为呈现到表单而编写的。
由于 RenderInBox 对象的密集创建性质,您应该预先创建它。因此它并不适合所有需求。
Here's another solution to the problem, it's a little intensive as it requires a fair bit of font creation and destruction, but may work better, depending on your circumstances and needs:
To use it simply create a new class and call Render(). Note this is specifically written for rendering to a form.
You should create the RenderInBox object upfront due to it's intensive creation nature. Therefore it's not suitable for every need.