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);

            }

我使用这样安装的系列获取字体


    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);

我尝试使用其他字体,但结果始终相同。有什么我想念的吗?如果不是,可能是什么原因?


天涯尽头无女友
浏览 115回答 1
1回答

慕雪6442864

选项 1:更改文本呈现objGraphics.TextRenderingHint&nbsp;=&nbsp;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):请注意,以厘米为单位的尺寸不会改变。所以它仍然会打印得很好。
打开App,查看更多内容
随时随地看视频慕课网APP