首页 > 解决方案 > 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# 的整个图像绘制部分并不是很熟悉,所以如果有更好的方法来绘制我的文本(可以根据许多变量而改变),我很乐意尝试其他方法。

标签: c#drawstring

解决方案


选项 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):

24 像素宽 J

更改后(仍然使用 Arial 10 pt):

180 像素宽 J

请注意,以厘米为单位的大小不会改变。所以它仍然会打印得很好。


推荐阅读