I'm trying to create an image by drawing a text using a TTF font. I'm using .net core 2.1.500 on MacOS, and have mono-libgdiplus v5.6 (installed via brew).
The TTF file is Neo Sans Medium and this is my code (I tried with different TTF fonts, always with the same result)
using (var bmp = new Bitmap(1024, 512))
{
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
var rectf = new RectangleF(0, 40, 1024, 90);
// If I specify a font that it doesn't know, it defaults to Verdana
var defaultFont = new Font("DefaultFont", 55);
Console.WriteLine($"defaultFont: {defaultFont.Name}"); // => defaultFont: Verdana
g.DrawString("Text with default", defaultFont, Brushes.Black, rectf);
var privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile("/path/to/Neo_Sans_Medium.ttf");
var fontFamilies = privateFontCollection.Families;
var family = fontFamilies[0];
var familyHasRegular = family.IsStyleAvailable(FontStyle.Regular);
Console.WriteLine($"familyHasRegular: {familyHasRegular}"); // => familyHasRegular: true
var textFont = new Font(family, 55);
Console.WriteLine($"textFont: {textFont.Name}"); // => textFont: Neo Sans
var rectText = new RectangleF(0, 140, 1024 - 50 - 50, 1024 - 50 - 10);
g.DrawString("Text with loaded font", textFont, Brushes.Black, rectText);
g.Flush();
g.Save();
bmp.Save("test.png", ImageFormat.Png);
}
Since it printed textFont: Neo Sans
, it seems to me that it was properly loaded (otherwise, it would have defaulted to Verdana
).
However, the output I'm getting is this one:
Am I missing something?
I was able to do it using SixLabors.ImageSharp
. I also installed SixLabors.Fonts
and SixLabors.ImageSharp.Drawing
.
With this code:
using (Image<Rgba32> image = new Image<Rgba32>(1024, 512))
{
FontCollection fonts = new FontCollection();
fonts.Install("/path/to/Neo_Sans_Medium.ttf");
var verdana = SystemFonts.CreateFont("Verdana", 55);
var neoSans = fonts.CreateFont("Neo Sans", 55);
image.Mutate(x => x
.BackgroundColor(Rgba32.White)
.DrawText("Text with default", verdana, Rgba32.Black, new PointF(0, 0))
.DrawText("Text with loaded font", neoSans, Rgba32.Black, new PointF(0, 65))
);
image.Save("out.png");
}
I was able to get this: