System.Drawing.Common DrawString not rendering text properly in Linux

1k views Asked by At

So I'm not sure what the issue is and have exhausted just about every option I can think of. I am targeting dotnet core 3.0 on Ubuntu 18.04 LTS. On a windows environment it works fine but in this Ubuntu environment text just does not render the way it is support to and comes out looking something like this

How it is supposed to be displayed

How it is supposed to be displayed

How it is displaying on Ubuntu

How it is displaying on Ubuntu

I am using the font Arial and I have the MS TrueType Fonts package installed, I've tried Font families native to Ubuntu and still the same thing.

1

There are 1 answers

0
Daniel Jonsson On

I experienced the same issue on Fedora 34.

I used this example file to reproduce the problem:

using System.Drawing;

Bitmap bmp = new Bitmap(200, 100);
using var gfx = Graphics.FromImage(bmp);
gfx.Clear(Color.Navy);
Font fnt = new Font("Arial", 18);
gfx.DrawString("test123", fnt, Brushes.Yellow, 10, 10);
bmp.Save("test.bmp");

(Note that the code above compiles and runs with .NET 5 and C# 9.0 since it supports top-level statements, https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/top-level-statements.)

However, the result looked like this:

enter image description here

What I did to fix this, was to run the following terminal command:

$ dotnet add package system.drawing.common

After that, re-building and running the program yields this image instead:

enter image description here

With this change, my .csproj file now looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="system.drawing.common" Version="5.0.2" />
  </ItemGroup>

</Project>

Just for reference; the reason I stumbled upon this problem was when I tried to use ScottPlot, and I filed this bug about it: https://github.com/ScottPlot/ScottPlot/issues/1079