c# system.drawing 计算 1 个字符的字体大小以填充方形图像

标签 c# .net system.drawing

如果我有一个尺寸相等的 System.Drawing.Bitmap,例如100x100、50x50,我想使用此代码绘制单个字符:

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

font = new Font(_fontName, _fontSize, _fontStyle);

GraphicsPath path = new GraphicsPath();

path.AddString("A", font.FontFamily, (int)font.Style, font.Size, rect, stringFormat);

我将如何计算 _fontSize 的值以使其紧密适合图像的尺寸?

最佳答案

Graphics.MeasureString 函数返回给定字体的字符串的大小。在一个循环中,您可以确定最适合的字体大小。

http://msdn.microsoft.com/en-us/library/403ezxd2.aspx

关于c# system.drawing 计算 1 个字符的字体大小以填充方形图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9051647/

相关文章:

.net - 在 IIS 中部署 .PDB 文件。有什么好处吗?

c# - 如何检查 COM 组件(EXE/DLL 文件)是否已注册(使用 .NET)?

c# - 将 c# Rijndael 函数转换为 php 函数

c# - CheckedListBox 中不可勾选的项目?

c# - 测试依赖于企业库的代码,即使它不提供接口(interface)?

c# - ASP.NET Core 中的服务器端图形

c# - .NET Custom UserControl 的内容在调整父级大小时消失

c# - 单例查找表需要锁吗?

.net - 从 RichTextBox 复制文本及其格式

c# - 绘制旋转矩形