c# - Graphis.DrawString 始终使用默认字体
问题描述
我有一个项目,我在其中创建了一个带有围绕一个不可见圆圈旋转的文本的图像。
绘图本身工作得很好。但是,似乎无论我使用哪种字体,我总是得到相同的结果,这是我假设一些低质量的默认字体。
这是代码:
Bitmap objBmpImage = new Bitmap(1000, 1000);
System.Drawing.Text.InstalledFontCollection installedFontCollection = new System.Drawing.Text.InstalledFontCollection();
FontFamily[] fontFamilies = installedFontCollection.Families;
System.Drawing.Font objFont = new System.Drawing.Font(fontFamilies.Where(x => x.Name == "Arial").FirstOrDefault(),10);
Graphics objGraphics = Graphics.FromImage(objBmpImage);
objGraphics.Clear(Color.Transparent);
float angle = (float)360.0 / (float)competences.Count();
objGraphics.TranslateTransform(500, 450);
objGraphics.RotateTransform(-90 - (angle / 3));
int nbComptetence = competences.Count();
int indexCompetence = 0;
foreach (T_Ref_Competence competence in competences)
{
byte r, g, b;
HexToInt(competence.T_Ref_CompetenceNiveau2.T_Ref_CompetenceNiveau1.Couleur, out r, out g, out b);
Brush brush = new System.Drawing.SolidBrush(Color.FromArgb(255,r,g,b));
if (indexCompetence * 2 < nbComptetence)
{
objGraphics.DrawString(competence.Nom, objFont, brush, 255, 0);
objGraphics.RotateTransform(angle);
}
else
{
objGraphics.RotateTransform(180);
objGraphics.RotateTransform(angle/2);
float textSize = objGraphics.MeasureString(competence.Nom, objFont).Width;
objGraphics.DrawString(competence.Nom, objFont, brush, -253 - textSize, 0);
objGraphics.RotateTransform(angle);
objGraphics.RotateTransform(-180);
objGraphics.RotateTransform(-angle / 2);
}
indexCompetence++;
}
我使用这样的已安装系列获得字体
System.Drawing.Text.InstalledFontCollection installedFontCollection = new System.Drawing.Text.InstalledFontCollection();
FontFamily[] fontFamilies = installedFontCollection.Families;
System.Drawing.Font objFont = new System.Drawing.Font(fontFamilies.Where(x => x.Name == "Arial").FirstOrDefault(),10);
我尝试使用其他字体,但结果始终相同。有什么我想念的吗?如果不是,可能是什么原因?
谢谢,
编辑:要回答这个问题,我到底想要什么,请考虑一下:
这张图片是我正在制作的网站的截图。中间的图表是使用charts.js 生成的,但它的局限性迫使我将文本绘制为背景图像。它实际上占据了我的大部分屏幕,所以它真的不能比这大得多。如您所见,文本字体非常模糊,我只是希望它更易于阅读。我虽然字体是问题,但我真的不知道。
我对 C# 的整个图像绘制部分并不是很熟悉,所以如果有更好的方法来绘制我的文本(可以根据许多变量而改变),我很乐意尝试其他方法。
解决方案
选项 1:更改文本渲染
objGraphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel
选项2:更改抗锯齿模式
objGraphics.InterpolationMode=InterpolationMode.NearestNeighbor;
选项 3:更改图像的 DPI
如果您缩放输入图像然后以更高的 DPI 绘制文本,您将获得最佳结果。
位图的默认 DPI 为 96。可能是使用该设置导出的 JS 库。
如果你想要更平滑的字体渲染,你需要增加 DPI,例如
objBmpImage.SetResolution(1200,1200);
如果这样做,您可能需要增加位图的像素数。
如果“难看”的文字刚好适合 1000x1000 图片,那么现在需要 1000*1200/96=12500 像素。
更改前(使用 Arial 10 pt):
更改后(仍然使用 Arial 10 pt):
请注意,以厘米为单位的大小不会改变。所以它仍然会打印得很好。
推荐阅读
- javascript - 在使用之前在 Angular 中准备好服务
- azure - 如何使用 powershell 脚本将新的后端池添加到现有的 azure Front Door
- angular - 如何在角度或打字稿中迭代后端 api 映射响应
- javascript - Chrome 调试器 React.js 应用程序在刷新时显示白屏和加载图标(标签栏)
- c# - 从另一个泛型类调用子变量,泛型是父类
- mysql - 排序规则的非法混合错误 - 所有表和列都是相同的集合
- ssh - 如何将 SSH 密钥从 Windows localhost 推送到 GCP 云外壳?
- android - 如何在通过android应用程序(使用phonegap/cordova创建)对服务器进行休息调用时删除原点:file://?
- css - 手机背景图不一样
- typescript - 尝试在 Outlook Online 上放弃更改新约会时出现此错误