I have a method which generates images using DrawString(). If I call this method on every machine I tested from any program with an UI or from Console-App every thing is fine. The image shows the right font.
But if I call this method from windows service or from IIS website GDI (?) automatically changes to Microsoft Sans Serif without any exception.
It was working for years on Windows 7. After change to Windows Server 2019 the problem occurs first time. Now I have 2 developer machines with Windows 10 Build 20H2 and the same .NET versions on both. On one machine image generation works well, on the other machine the font changes. Any idea, what I can do, to get it work on every machine?
public void GenerateBitmap()
{
string drawString = "Sample Text 1234567890";
using (Bitmap bmp = new Bitmap(5000, 200, PixelFormat.Format32bppArgb))
{
bmp.SetResolution(300, 300);
using (Graphics grfx = Graphics.FromImage(bmp))
{
using (Brush brush = new SolidBrush(Color.Black))
{
Font f = new Font("MyFontName", 22F);
grfx.DrawString(drawString, f, brush, new Point(0, 0));
}
}
bmp.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);
bmp.Dispose();
}
}